Why does white space becomes a node?

wxdqz

New Member
I have a simple XML file which looks like this:

<Lawyers>
<lawyer>
<Country>Singapore</Country>
<Company>Allen & Gledhill</Company>
</lawyer>
<lawyer>
<Country>Singapore</Country>
<Company>Allen & 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
 
Back
Top