I'm trying to traverse a simple XML document with Java, but for some reason whitespace is being counted as nodes. For example, I have this:
factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
DOMImplementation domImpl = builder.getDOMImplementation();
factory.setIgnoringComments(true);
factory.setIgnoringElementContentWhitespace(true);
DOMImplementationLS ls = (DOMImplementationLS) domImpl.getFeature("LS", "3.0");
LSInput in = ls.createLSInput();
in.setByteStream(is);
LSParser parser = ls.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, "http://www.w3.org/2001/XMLSchema");
document = parser.parse(in);
document.getDocumentElement().getFirstChild()
So for the following XML, the first child returned is some combination of whitespace.
<?xml version="1.0"?>
<opendap>
<root url="http://localhost/" filter=".*" />
<rewrite>
<var name="altitude" type="enum" resAttr="getNodeName" profattr="profattr"/>
</rewrite>
<constants>
<catalogURL>http://google.com</catalogURL>
</constants>
<resAttr>
<Publishers>person1</Publishers>
<Publishers>person2</Publishers>
</resAttr>
</opendap>
How do I fix this?
Edit: I've kind of fixed it by doing this (resattr is Element representing ). Unfortunately, the setValidating didn't work.
for (Node child = this.resAttr.getFirstChild(); child != null; child = child.getNextSibling()){
if (child.getFirstChild() != null && child.getFirstChild().getNodeValue() != null){
String nodename = child.getNodeName();
String nodevalue = child.getFirstChild().getNodeValue();