I seem to be having problems doing some basic parsing of an XML document in Java. I am trying to retrieve a list of tags based on a specific namespace. Unfortunately, the list of tags returned is empty. Can anyone enlighten me to what I am doing wrong? Thanks.
Example Java
DocumentBuilder bob = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document template = bob.parse( new InputSource( new FileReader( xmlFile ) ) );
NodeList tags = template.getElementsByTagNameNS( "http://www.example.com/schema/v1_0_0", "*" );
Example xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ex="http://www.example.com/schema/v1_0_0">
<head><title>Test</title></head>
<body>
<h1>Test</h1>
<p>Hello, World!</p>
<p><ex:test>Text</ex:test></p>
</body>
</html>
You need to create your DocumentBuilder such that it is aware of namespaces prior to trying to parsing things by namespace. i.e.: