• 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

Reading XML

 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, this may be a stupid question, but guess what, I don't care.
From my limited knowledge of the various API's for reading/writing XML files in Java it seems overly complicated in the fact that I have to write my own special classes to read and write an XML file. So I have a lot of custom parsing and what not if I am using my own XML files that I created.
Now, it could be that I just need to supply my XML API with the DTD of the XML file and all of a sudden things get easier, but I don't know. That's kind of what I'm asking.
Basically, in my make believe ideal world, an API to read/write XML files should be as simple as providing the API my XML file, maybe a DTD then I can just read/write whatever object I want. For example

And to read this might look something like

And the output of that would of course be

Maybe this is possible and I am just missing something. Any hints, tips, suggestions, or simply telling me I'm an idiot will suffice.
Thanks.
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The standard JAXP API includes classes for parsing XML documents into a DOM document tree, which you can then interrogate for details a bit like in your make-believe example.
It goes like this:
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gregg Bolinger:
...or simply telling me I'm an idiot will suffice.


Damn. I almost missed this.
You're an idiot
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more thing. Check out JavaAlmanac.com and search for "dom" or "xml".
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Element usernameElem = user.getElementsByNodeName("username").item(0);
Why am I getting item(0)? What is item(0)?
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I might be starting to get the hang of this. So the value between a specific element is the NodeValue, <username>gdboling</username>, gdboling is the NodeValue and the NodeName is the name of the element which would be username in that case.
I'm still not clear as to why I have to get item(0). In what case might I get say item(1)? I realize I probably don't have an item(1) in my user.xml file but what would I add so that it does?
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gregg Bolinger:
Ok, I might be starting to get the hang of this. So the value between a specific element is the NodeValue, <username>gdboling</username>, gdboling is the NodeValue and the NodeName is the name of the element which would be username in that case.


In DOM, everything is a node (org.w3c.dom.Node). In your "user" document, each of the elements are nodes (of type org.w3c.dom.Element) and each piece of plain text is a node (or type org.w3c.dom.Text). The javadocs for org.w3c.dom.Node explains what is the NodeValue for each type of node. For Elements it's null, for Text nodes it's the text. Note that even though <username>gdboling</username> would intuitively mean that the "username" element has one child node of type Text with value "gdboling", it might have two child nodes: one could be a Text node with value "gdb" and the other with value "oling".

Originally posted by Gregg Bolinger:
I'm still not clear as to why I have to get item(0). In what case might I get say item(1)? I realize I probably don't have an item(1) in my user.xml file but what would I add so that it does?

The item(int) method needs to be called because the getElementsByName(String) methods return a list of nodes that match the given name. In my example, I knew that the only child with the name "username" is the username I want so I just took the first (0-based index) elements off the list.
 
reply
    Bookmark Topic Watch Topic
  • New Topic