my dog learned polymorphism*
The moose likes XML and Related Technologies and the fly likes xml files 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 "xml files" Watch "xml files" New topic
Author

xml files

john s murray
Ranch Hand

Joined: Apr 26, 2009
Posts: 30
hi, i would like to seek some help. i'm able to parse and xml file with something like the below:


Doing the above, how would print all the elements within each customer?

thanks in advance.
john s murray
Ranch Hand

Joined: Apr 26, 2009
Posts: 30
adding more info to the question. the example has 3 nodes with "customer". how would i getLength() on each of the nodes and then loop thru and only print each element of that node?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
This sort of basic operation is covered in Harold's free online book.

Bill
john s murray
Ranch Hand

Joined: Apr 26, 2009
Posts: 30
thanks for the link. there's alot of info there. anyway, im able to loop each of the "customer" node and print out each element and their values now. is there a to only print out the values of the 2 node? let's say the the example has an id or name:


can you provide an example of how check for name=2 (or id=2) and only print that nodes elements?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I would use XPath to find the relevant nodes directly, instead of writing tedious DOM-bashing code to navigate through the document.
john s murray
Ranch Hand

Joined: Apr 26, 2009
Posts: 30
ok, i'm able to check the Attributes of the customer node if i loop thru all 3 nodes. this doesn't seem efficient. is there a way only read and print the node2 base on the attribute without have to loop thru all 3 nodes?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

As I said, I would use XPath. But not because I believed it would be more "efficient" (whatever you had in mind by that); it's still going to have to plough through the whole document the same way your code would if you wrote it. I would use XPath because then I wouldn't have to write specialized code ploughing through the document.
john s murray
Ranch Hand

Joined: Apr 26, 2009
Posts: 30
thanks. i got dom and xpath working. are you saying xpath reads the document from top to bottom each time too?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

No. I'm saying it works by programming, not by magic. If you have a specific XPath expression in mind, it isn't hard to write some DOM code which works better than the generic XPath classes. It's just that you probably don't want to waste your time doing that.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
I found that XPath took 20 times longer than using DOM api methods in a simple task, BUT I completely agree with Paul, going the DOM route will:

1. be harder to understand when you come back to the code in a year
2. save only milliseconds with simple XML like yours
3. be a pain to debug

Bill
 
 
subject: xml files