Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Working with XML in Java

Options
  • 24-02-2014 2:19pm
    #1
    Registered Users Posts: 2,815 ✭✭✭


    I need to manipulate an XML file in a Java. Nothing too outlandish - just changing element values and inserting/deleting notes in a relatively flat structured, small XML file.

    Can someone please recommend an XML library for this purpose.

    Also, the user will have to make numerous changes to a single XML. What is the best methodology:

    - Read the entire contents of the original XML to a data structure, update the data structure's values as the user makes the changes to the contents via the front end, and then output the data structure to a new XML when the user is finished

    or

    - Output directly to a XML file each time a change is made

    Thanks


Comments

  • Registered Users Posts: 27,161 ✭✭✭✭GreeBo


    You have a couple of options, depending on the workflow you want to support.

    - JibX type implementation -> map XML to POJOs and back again when finished
    - SAX/STAX parsers -> parse the file and do things when you come across specific elements (change values, add things, remove things)
    - DOM parsers -> read the entire doc into memory and traverse it doing whatever you want

    - DOM is typically a little slower and much more memory intensive, but for small docs it shouldnt matter, just ensure that you wont be trying to manipulate lots of large docs.
    - SAX/STAX is fast but you dont have the whole doc to play with at once, so for your use case you might end up traversing the same doc multiple - times each time the user changes something.
    - Jibx might be a good fit, but if your XML is going to keep changing (user can add any time of node for example) then you are going to have problems creating the mapping and keeping it mapped to your POJO(s)

    linky DOM/SAX/STAX

    JibX linky


    /edit to answer your question! :)

    It depends on the workflow you want to support
    Do you want the user to be able to play and the "commit" their changes or just as they change you update the file?
    Will multiple users be updating the same file? Have you got any locking strategy if so?


  • Registered Users Posts: 2,815 ✭✭✭SimonTemplar


    Thanks for your comprehensive answer Greebo.

    I like the idea of Jibx. In terms of mapping it to POJOs, the structure of the XML will not change, but the user may add nodes to the original XML via the front end. For example, assume
     
    <movies>
     
        <movie>
            <title>Jaws</title>
            <year>1976</year>
        </movie>
     
    </movies>
     
    

    The functionality of the application would involve changing the year from 1976 to 1975, adding a new movie, and delecting existing movies. The XML schema won't change.

    There will not be multiple users editing a single file. For background on this, an automated daily batch generates the original XML file, which in turn is uploaded to a web service. However, this generated XML has data integrity issues due to problems with the source system, and the application I need to develop is to enable the users (non-technical) to edit the XML with a GUI before uploading it. It is simple input > process > output of a new XML file every day.


  • Registered Users Posts: 27,161 ✭✭✭✭GreeBo


    Thanks for your comprehensive answer Greebo.

    I like the idea of Jibx. In terms of mapping it to POJOs, the structure of the XML will not change, but the user may add nodes to the original XML via the front end. For example, assume
     
    <movies>
     
        <movie>
            <title>Jaws</title>
            <year>1976</year>
        </movie>
     
    </movies>
     
    

    The functionality of the application would involve changing the year from 1976 to 1975, adding a new movie, and delecting existing movies. The XML schema won't change.

    There will not be multiple users editing a single file. For background on this, an automated daily batch generates the original XML file, which in turn is uploaded to a web service. However, this generated XML has data integrity issues due to problems with the source system, and the application I need to develop is to enable the users (non-technical) to edit the XML with a GUI before uploading it. It is simple input > process > output of a new XML file every day.

    Jibx sounds fine then, you would just map repeating elements to lists /etc type structures
    so you would have a movies.java containing lists of movie objects, each movie object would have name, year etc members.

    Using annotations this becomes dare I say trivial, you annotate the pojos with the required XML, wire it up via Jibx and you are pretty much done!

    I would use logic to lock the file though...otherwise funtimes ahead guaranteed!


    /edit actually I think only JAXB only has the annotations
    good example from mykong


  • Registered Users Posts: 2,021 ✭✭✭ChRoMe


    Two excellent libs are BeanIO or http://simple.sourceforge.net/


Advertisement