0

I'm using jaxb to make xml file and store it in my pc, and i want when making another file to store it in addition to the first file in the same xml file that is on my pc, for example this is the first file:

<File>
   <thing1>
   </thing1>
</file>

And i want the second file to be like that :

 <File>
       <thing1>
       </thing1>
       <thin2g>
       </thing2>
    </file>

And that's the Java code i use for marshaling:

public ConfList(Object obj){
        this.obj = obj;
    }

    public void addToLXML() throws IOException, JAXBException {

        File file = new File(fileName);
        JAXBContext jaxbContext = JAXBContext.newInstance(Xml.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        jaxbMarshaller.marshal(obj, file);
        jaxbMarshaller.marshal(obj, System.out);

    }

1 Answer 1

1

If I understand your question correctly, you want to be able to add new elements to an existing XML file? So the first time you create the file, the "thing1" element would be added, and the second time the program is run, the "thing2" element would be added?

Assuming this is the case, the best way to do this would be to unmarshal the existing file into an object that contains an annotated list of "thing" elements (assuming they are the same type or share a common parent class). Then simply add your new "thing" to the list, and marshal it back to file.

If this didn't address your question, please clarify what you are looking for, and I'll see what I can do to help!

Steve

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.