Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading xml data

 
wayne morton
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a small program which creates an xml file



Now i wish to call an element of that file and display it.e.g. the username.

After searching seemingly endlessly for a way to do this with the basic java libraries the only answer i seem to get is use an additional open source library. I would prefer to know how to do it with the existing libraries if only for learning sake but eventually giving up on finding reasonable info on doing so i chose XOM as it is used in a book i am using and am now trying figure out how that works and as per usual most 'tutorials' are designed for people who know quite a bit about what their doing, which i obviously don't...

This is about the closest to my current understanding of how it should work

import java.io.*;
import nu.xom.*;



root shows properties as expected, name shows null?
What argument am i meant to give to retrieve that element so the readout would be "rcade"?

Or preferably how do you retrieve it using the basic libraries?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49405
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to the XML forum because this is too difficult for “beginning”.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wayne morton wrote:...
Now i wish to call an element of that file and display it.e.g. the username.

After searching seemingly endlessly for a way to do this with the basic java libraries the only answer i seem to get is use an additional open source library.


Since you are using Properties to store the file, it would seem rather unlikely that you couldn't use Properties to load it back in. Have you tried Properties#loadFromXML()

...root shows properties as expected, name shows null?
What argument am i meant to give to retrieve that element so the readout would be "rcade"?

Or preferably how do you retrieve it using the basic libraries?


If the loadFromXML() doesn't work you will have to explain why, and you should also show the XML file that gets created (and maybe a link to the third party tool you are using)
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just as a followup, at least in Java 6 & 7, Java does ship with full DOM, SAX, and StaX libraries. XOM appears to be a DOM implementation, so if you want to continue that route, see this part of the Java Tutorials for how to use the JAXP implementation that is part of standard Java: http://docs.oracle.com/javase/tutorial/jaxp/dom/index.html

Because everything I said in the last paragraph might look like alphabet soup, here is some explanation:
DOM = Document Object Model. It reads an XML file in and stores it in memory in a model that lets you repeatedly navigate through it, and look at parent-child relationships. To me it is the easiest to understand but can be kinda slow or memory intensive for large XML files
SAX = Simple API for XML. It reads the XML file in tag by tag and uses events to callback and tell your application that it reached particular tags or events. Can be efficient because it doesn't hold anything in memory, but is harder to work with because you have to listen for the parts of the XML you want.
StAX = Streaming API for XML. Sort of a combination between DOM and SAX if I understand it. It uses event driven parsing but lets you pill the info from the file, and lets you navigate in both directions.
JAXP = Java API for XML Processing. A particular implementation of these above models for reading XML in Java.
 
wayne morton
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all your help Steve.
Initial testing seems to work as required in regard to simply importing from an XML file. As per usual it's knowing which direction to look in is the hard part

I really appreciate the followup and I'll certainly look more into those libraries that ship with java as (for learning purposes at least) i would rather learn with the basic tools first and then maybe move onto external tools once i have the basics figured out in case for some reason in the future i can't use an external tool and i always have the basic tools to fall back on if needs be.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic