Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

xml files

 
john s murray
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This sort of basic operation is covered in Harold's free online book.

Bill
 
john s murray
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks. i got dom and xpath working. are you saying xpath reads the document from top to bottom each time too?
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic