hi everyone,
i am using DOM to parse an xml file. But i dont know how to cinfig the DocumentBuilderFactory to ignore empty text elements.
For example, i have an xml file like this:
<?xml version="1.0" encoding="UTF-8" ?>
<root>
<child>Tom</child>
<child>Jerry</child>
</root>
I used the following codes to parse:
String fname = "Tom-and-Jerry.xml";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setIgnoringElementContentWhitespace(true);
factory.setIgnoringComments(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
// Generate a DOM tree from the DOM builder.
org.w3c.dom.Document dom = builder.parse(new File(fname));
org.w3c.dom.NodeList list = dom.getChildNodes();
for (int i=0; i<list.getLength(); i++) {
System.out.println("Child No."+i);
System.out.println("NodeName="+list.item(i).getNodeName());
System.out.println("NodeType="+getType(list.item(i).getNodeType()));
System.out.println("NodeValue="+list.item(i).getNodeValue());
System.out.println();
}
The result is not exactly what i want ---- there are 5 children in list!! The 1st, 3rd and 5th are #text and their values are all empty. Only the 2nd and the 4th are the child that i expect.
It is really troublesome to get all these silly empty texts as sub elements. I tried to get rid of them, but i failed. I just dont understand why factory.setIgnoringElementContentWhitespace(true) did not work.
Anyone can help me? thanks.
Heavy ZHENG