Click to See Complete Forum and Search --> : Why does white space becomes a node?


JSchwarz
11-01-2006, 04:17 PM
I have a simple XML file which looks like this:

<Lawyers>
<lawyer>
<Country>Singapore</Country>
<Company>Allen &amp; Gledhill</Company>
</lawyer>
<lawyer>
<Country>Singapore</Country>
<Company>Allen &amp; Gledhill</Company>
</lawyer>
...
</Lawyers>I traverse this tree by first getting a list of all <lawyer>:

NodeList nlLawyers = doc.getElementsByTagName("lawyer");Then, I want to get all the child nodes of lawyer (Country, Company), which I do like this:

Element elLawyer = (Element) nlLawyers.item(i);
NodeList columns = elLawyer.getChildNodes();By my reckoning, columns should be a node list with two nodes, Country and Company. However, it really has five nodes. There are three nodes with a NodeName #text (in other words, columns.item(i).getNodeName() contains '#text'). All three of the nodes have a value which seems to be a line feed character.

These three extra lines are apparently the whitespace between the Country and Company nodes (also before and after those nodes).

Why is that happening and how do I process this code to ignore the whitespace text?

Thanks,
-Jeff

Charles
11-01-2006, 04:53 PM
By definition in XML whitespace is not ignored.