• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

xml files

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
I brought this back from the farm where they grow the tiny ads:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic