Problem with whitespaces when parsing with Xerces

This should cover W3C XML Schema, Relax NG and DTD related problems.

Problem with whitespaces when parsing with Xerces

Tue Dec 14, 2004 3:05 pm


I'm using Xerces DOM Parser as below:

import org.apache.xerces.parsers.DOMParser;

DOMParser parser = new DOMParser();
parser.setFeature("", true);
parser.setErrorHandler(new errHandler());

The xml file being parsed looks like this:

<CCphysical xmlns:xsi="" xsi:noNamespaceSchemaLocation="MySchema.xsd">
<root name="Name">
<directory name="doc" />
<directory name="doc2" />
<directory name="XML" />

Extract from the schema:

<xsd:element name="root">
<xsd:element maxOccurs="unbounded" minOccurs="0" ref="directory" />
<xsd:attribute name="name" type="xsd:String" use="optional" />

When parsing the file I don't get 3 childNodes for root but 6 because the parser also counts the whitespaces. Even when setting the feature
it does not work. What's wrong and what do I have to do so that whitespeces are ignored?

Thanks for your help,
Posts: 5221


Tue Dec 14, 2004 6:55 pm

Yes, indeed the root node has as children besides the directory nodes the line break text nodes.
The safe thing to do (and that would work for any xml file no matter how many white spaces are between tags) would be that when iterating through the root's children to add this condition for each node:

Code: Select all

n.getNodeType() == Node.ELEMENT_NODE
and only take into consideration the element nodes.

Return to “XML Schemas”

Who is online

Users browsing this forum: No registered users and 1 guest