File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes Dom and empty nodes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Dom and empty nodes" Watch "Dom and empty nodes" New topic
Author

Dom and empty nodes

John Landon
Ranch Hand

Joined: Sep 25, 2008
Posts: 227
Hi,
I am getting an xml String that has new lines and empty spaces between nodes. So for example if I am on root and I am saying root.getFirstChild() I am getting the text node that is new line and spaces instead of the real child.
I tried normalize on the root but it didn't help. What do I do?

thanks.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18989
    
    8

Stop calling getFirstChild() if you don't want the first child. Do something else which gets the child you are interested in.

That could involve getting all the children and examining them, or calling getElementsByTagName, or using XPath, or something else.

Edit: and don't call the child you are interested in the "real" child. All of them are real. Calling it that just leads you into denial. The structure of the document is what it is. Choose what you want and ignore what you don't want.
[ December 11, 2008: Message edited by: Paul Clapham ]
John Landon
Ranch Hand

Joined: Sep 25, 2008
Posts: 227
not helpful.
at the point of parsing I don't know any paths or names. I just know I need the "real" child
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18989
    
    8

Well, if you have no idea what you are looking for then you would be out of luck. Perhaps you should review your requirements. But I have a suspicion that you do know what you are looking for.

Let me guess: you're looking for an element?
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

Originally posted by John Landon:
not helpful.
at the point of parsing I don't know any paths or names. I just know I need the "real" child


You may not have read all of Paul's reply. He made another suggestion besides using paths or names:

Originally posted by Paul Clapham:
That could involve getting all the children and examining them, or...


Unfortunately

probably won't compile. But iterating through the NodeList until the current node is an instance of an Element would work.
[ December 11, 2008: Message edited by: Mark Vedder ]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

I suggest somebody move this to our JavaScript forum.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
John Landon
Ranch Hand

Joined: Sep 25, 2008
Posts: 227
Thanks guys.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Originally posted by Rob Prime:
I suggest somebody move this to our JavaScript forum.


Done
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Not exactly what you need, but should give you the basic idea:


Eric
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624


Originally posted by Rob Prime:
I suggest somebody move this to our JavaScript forum.


I do not think this is a JavaScript related question. I got the impression the original poster was talking about working with an XML DOM (not a JavaScript DOM) via Java and the org.w3c.dom.Document interface and API. Based on the subsequent posts, including those by the original poster, that seems to be the case.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18989
    
    8

Yes, I agree, Mark. But it looks like John is satisfied, at least he said "Thanks guys" which sounds like he's okay so far.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dom and empty nodes