aspose file tools*
The moose likes XML and Related Technologies and the fly likes Reading xml data 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 "Reading xml data" Watch "Reading xml data" New topic
Author

Reading xml data

wayne morton
Greenhorn

Joined: May 17, 2012
Posts: 28
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

Joined: Oct 13, 2005
Posts: 38363
    
  23
Moving to the XML forum because this is too difficult for “beginning”.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4176
    
  21

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
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4176
    
  21

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

Joined: May 17, 2012
Posts: 28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading xml data