This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
So for "someNode.getChildNodes().getLength()" I will get 7 instead of three.
What is the best practice here? I tried a loop with a counter which will be increased if an nodeof type Element will be found, which works, but aint there better ways? I also dont want to use XSLT as I will have then to maintain two different technologies.
Would it be better to remove the white spaces at the beginning of the code and then work with a "correct" element? If yes, what is the best way here?
Max J. Power
Max J. Power
Joined: Oct 19, 2011
I think I found a solution. The following method will remove the white spaces from a Dom Node (a Node can be an Element or a Document):
After this transformation, the Node.getChildNodes().getLength() method returns the right value.
If those text nodes are that much hated, rather than using some helper to convert the element and byto array back-and-forth (which I find "ridiculous", sorry!), you can do that loop serving both purposes of cleaning them out once for all thereafter and counting the element nodes as child of it.
From there onward, the someNode will contain only element node as its child.
For this matter, I just want to point out in the DocumentBuilderFactory, there is a settable feature on IgnoringElementContentWhitespace. Unfortunately, there is some divided interpretation on the recommendations or some grossly overlook in the building of jdk 1.6, there is a regression of bug/feature in 1.6 to before 1.5. But, that's how it is for the moment.