Can a piece of XML be distributed using JAXB (or JAXB + StAX)?

I have an XML structure that is very huge. I want to update parts of this XML by parsing one element and then applying business logic.

I can unbind a child in a POJO. I want to modify this POJO in Java and then update it to XML in the same place.

Is this possible in JAXB? Or using the JAXB + StAX combination.

Structure example:

 <folder id="c5718b36-bab1-4c08-8f75-8e2f9aee42c5" name="Folder-1">
        <description> folder Desc</description>
        <createdBy>User2</createdBy>
        <hidden>false</hidden>

        <file id="4f2efb42-0604-4878-9e1e-ae90d66fb836" name="File-1">
            <description>file desc</description>
            <createdBy>User1</createdBy>
            <hidden>false</hidden>
        </file>
 </folder>

      

In the above example, I can unbind the "file" element in the POJO. I want to make changes to this POJO and then update them in the XML file in the correct location.

How can i do this?

Please help me. Thank.

+3


source to share


2 answers


You can do the following with JAXB and StAX:



import javax.xml.bind.*;
import javax.xml.stream.*;
import javax.xml.stream.events.XMLEvent;
import java.io.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        XMLInputFactory xif = XMLInputFactory.newFactory();
        XMLOutputFactory xof = XMLOutputFactory.newFactory();

        try(
           FileInputStream in = new FileInputStream("in.xml");
           FileOutputStream out = new FileOutputStream("out.xml");
        ) {
            XMLEventReader xer = xif.createXMLEventReader(in);
            XMLEventWriter xew = xof.createXMLEventWriter(out);

            JAXBContext jc = JAXBContext.newInstance(File.class);
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

            while(xer.hasNext()) {
                if(xer.peek().isStartElement() && xer.peek().asStartElement().getName().getLocalPart().equals("file")) {
                    // Unmarshal the File object from the XMLEventReader
                    File file = (File) unmarshaller.unmarshal(xer);

                    // Modify the File object
                    file.description = "NEW DESCRIPTION";

                    // Marshal the File object to the XMLEventWriter
                    marshaller.marshal(file, xew);
                } else {
                    // Copy node from reader to writer
                    xew.add(xer.nextEvent());
                }
            }
        }
    }

}

      

+3


source


Try Binder , see tutorial .

If you want StAX see methods:

  • javax.xml.bind.Unmarshaller.unmarshal(XMLStreamReader, Class<T>)

  • javax.xml.bind.Marshaller.marshal(Object, XMLStreamWriter)



You can decouple the known class from the stream, process it, and return the marshal. This can even be done in a "filtration" process. I.e:.

  • You are parsing your XML through StAX.
  • You check upcoming events until you see what you want to handle.
  • Unacceptable events are passed on.
  • When you receive the appropriate event, you unmount your class with JAXB, handle the pojo, and send it back to the author of the stream.
+3


source







All Articles