Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

how to ignore empty text element while using DOM to parse xml??

843834Nov 12 2002 — edited Dec 27 2002
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

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 24 2003
Added on Nov 12 2002
4 comments
820 views