SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: Parse XML files

  1. #1
    Barefoot on the Moon! silver trophy
    Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,516
    Mentioned
    51 Post(s)
    Tagged
    1 Thread(s)

    Parse XML files

    I'm looking to work with java XML parsing, but I'm not quite sure to begin. The most popular parsers to work with are DOM and SAX, right? Which would be reasonably easier to use and parse fairly quick?

  2. #2
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    If all you want to do, at this point, is read an XML file, SAX should get you moving.
    On the other hand, if you need to read and edit the XML, you'll need to go with DOM.

  3. #3
    SitePoint Guru
    Join Date
    Feb 2006
    Location
    Pittsburgh, Los Angeles
    Posts
    706
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For java I like to use XOM...I think it has a nicer model than that used in DOM, you can get it at:

    http://www.xom.nu

    Its licensed under the LGPL, so you can use it in any sort of project.

  4. #4
    SitePoint Wizard rozner's Avatar
    Join Date
    Oct 2002
    Location
    Paris
    Posts
    1,058
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I like JDom, http://www.jdom.org , very easy to use

  5. #5
    Barefoot on the Moon! silver trophy
    Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,516
    Mentioned
    51 Post(s)
    Tagged
    1 Thread(s)
    Hey cool...XOM and JDOM look reasonably easy to work with in terms of building XML files. The parsing I couldn't make much sense of at a glance, though.

    Looking at both briefly, I'm not quite sure which is better over the other in terms of building XML files and parsing them. Any opinions on using one over the other?

  6. #6
    SitePoint Guru
    Join Date
    Feb 2006
    Location
    Pittsburgh, Los Angeles
    Posts
    706
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Of course I will say XOM...here is how things work with XOM:
    Take the simple xml:
    <person>
    <firstname>Bob</firstname>
    <lastname>Allen</lastname>
    </person>

    The code below would parse the file and print "Bob", "Allen" to standard out.

    Code:
    Builder builder = new Builder(true); // boolean indicates whether you want validation or not
    File file = new File("yourxmlfile.xml");
    Document document = builder.build(file);
    Element root = document.getRootElement();
    Elements persons = root.getChildElements("person");
    Element person = persons.get(0);
    Elements children = person.getChildElements();
    for(int i = 0; i<children.size(); i++){
        System.out.println(children.get(i).getValue());
    }

  7. #7
    Barefoot on the Moon! silver trophy
    Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,516
    Mentioned
    51 Post(s)
    Tagged
    1 Thread(s)
    hey, that makes sense

    Thanks, Snaily

  8. #8
    throw me a bone ... now bonefry's Avatar
    Join Date
    Nov 2004
    Location
    Romania
    Posts
    848
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use JAXP because it is a standard Java library and I am lazy enough not to want a third-party library.

  9. #9
    SitePoint Zealot Mr. Ram's Avatar
    Join Date
    Nov 2005
    Posts
    139
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)

    Post sample program on DOM parser

    Check the attachment.
    Attached Files Attached Files


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •