[Tutorial] XML parsing tutorial with JDOM

Discussion in 'Resources' started by skipperguy12, May 28, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    skipperguy12

    Hey guys, today I'm going to show you how to parse XML with Java, this is really useful for a lot of things like saving locations in XML, you could put them in maps like of a mini game and instead of having a huge config, have easy to manage XML in the maps folder!

    What will we use to parse the XML? If you guessed DOM or SAX, I'm sorry but I'm not going to teach you those, they seem to complicated for me, they're confusing... We're going to use JDOM 2!

    Before we begin, I just want to say that I only recently understood how to use JDOM, and it's thanks to this page by Mkyong:
    http://www.mkyong.com/java/how-to-read-xml-file-in-java-jdom-example/
    He's got the same stuff I'm showing, but less explaining and more code He's also got an entire XML example file.

    How to get your project ready for JDOM 2:
    JDOM is not like SAX or DOM, which bundled in JDK. To use JDOM, you need to download the library manually.
    Get JDOM from JDOM official site or declares following dependency if you are using Maven.
    <dependency>
    <groupId>org.jdom</groupId>
    <artifactId>jdom</artifactId>
    <version>2.0.5</version>
    </dependency>

    Okay, so now we can make use of the JDOM library!

    First, I'll be showing you how to read an XML file

    Reading an XML file using JDOM:
    First, we'll create a SAXBuilder object, like so:
    Code:
      SAXBuilder builder = new SAXBuilder();
    Then, we'll need some file to work with, I'll pretend we've got one in the root of our C drive called file.xml, let's go ahead and define it so we can use it:
    Code:
     File xmlFile = new File("c:\\file.xml");
    Great, so now we're ready to get to the actual parsing, lets go ahead and create a Document out of our xmlFile like so:
    Code:
    Document document = (Document) builder.build(xmlFile);
    Now, Document can do many things, here are the JavaDocs for JDOM in general: http://www.jdom.org/docs/apidocs/

    With a Document, you can get the root element using getRootElement() (note: you can set them too using setRootElement()), here's an example:
    Code:
    Element rootNode = document.getRootElement();
    Now, we've approached something called an Element. What is an Element? An element is basically a Tag in xml, here's what one looks like: <xmltag>. Now, Elements can have something called an Attribute, which would look like so: <xmltag attribute="this is an attribute">.
    So, now we've covered what a Document is, and what an Element is. So, how can i get all the Elements that are inside of the rootNode? These Elements are called children, to get all of a Document or Elements children, use getChildren() or getChildren("specific").

    Here's me getting all the children Nodes of "staff" and putting them into a List:
    Code:
    List list = rootNode.getChildren("staff");
    Now, we can easily loop through this like so:

    What is getChildText("child name"), and what does it do? It scans for a specific child, and gets the text inside of it's tag, like this:
    <firstname>yong</firstname>
    <lastname>mook kim</lastname>
    <nickname>mkyong</nickname>
    <salary>100000</salary>

    So our output for above would be:
    First Name : yong
    Last Name : mook kim
    Nick Name : mkyong
    Salary : 100000

    Now, let's just try out what we've learned with Attributes:
    Code:
    <firstname>yong</firstname>
            <lastname attribute="lol">mook kim</lastname>
            <nickname>mkyong</nickname>
            <salary>100000</salary>
    Using this code:
    Code:
    System.out.println("First Name Attribute : " + node.getAttributeValue("attribute"));
    Would give us: First Name Attribute : lol


    If anyone wants, they can make an XML parser that you can throw any XML Document at, and it will parse the whole thing, retrieving everything. I'll put it on OP if anyone happens to make it (I've made a simple one, but it was very messy!).

    If you're wondering why I made this tutorial, when I suck at explaining, it's because I was stuck on XML for a couple months, until I heard of JDOM, and I really wanted to share it with you all. If anyone spots mistakes in this thread, please tell me, I made it in a rush! Good luck!

    Notice: The code here may be from Mkyongs tutorial, I did not have time to make my own, so I simply decided to take an already made tutorial and make it easier to understand with more explaining.
     
  2. Offline

    microgeek

  3. Offline

    skipperguy12

    microgeek
    Yeah, but your code is using SAX. For me, SAX is extremely confusing :(

    If anyone would make a tutorial on that, it'd be great.
     
Thread Status:
Not open for further replies.

Share This Page