Search code examples
javaxmljavabeansxmlbeansxmlmapper

Update partial XML mapping to bean


I need to map an xml file subset of nodes to a Java Bean.

For example map

<data>
    <field1>Value</field1>
    <field2>Value</field2>
    <field3>Value</field3>
    <field4>Value</field4>
    <field5>Value</field5>
</data>

to

public class DataBean {
    private String field2;
    private String field5;
    // ...getter/setter
}

then manipulate the bean and update the source xml file without loosing elements that are not mapped. How can I use to do it? What library?

Thanks for help, Maurizio


Solution

  • Note: I'm the EclipseLink JAXB (MOXy) lead an a member of the JAXB 2 (JSR-222) expert group.

    Below is how this can be done with MOXy's implementation of the JAXB Binder:

    DataBean

    package forum9988170;
    
    import javax.xml.bind.annotation.XmlRootElement;
    
    @XmlRootElement(name="data")
    public class DataBean {
    
        private String field2;
        private String field5;
    
        public String getField2() {
            return field2;
        }
    
        public void setField2(String field2) {
            this.field2 = field2;
        }
    
        public String getField5() {
            return field5;
        }
    
        public void setField5(String field5) {
            this.field5 = field5;
        }
    
    }
    

    jaxb.properties

    To specify MOXy as your JAXB provider you need to add a file named jaxb.properties in the same package as your domain classes with the following entry,

    javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
    

    Demo

    package forum9988170;
    
    import java.io.File;
    import javax.xml.bind.*;
    import javax.xml.parsers.*;
    import javax.xml.transform.*;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.stream.StreamResult;
    
    import org.w3c.dom.Document;
    import org.w3c.dom.Node;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(DataBean.class);
    
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            File xml = new File("src/forum9988170/input.xml");
            Document document = db.parse(xml);
    
            Binder<Node> binder = jc.createBinder();
            DataBean dataBean = (DataBean) binder.unmarshal(document);
            dataBean.setField2("NEW FIELD 2");
            dataBean.setField5("NEW FIELD 5");
            binder.updateXML(dataBean);
    
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer t = tf.newTransformer();
            DOMSource source = new DOMSource(document);
            StreamResult result = new StreamResult(System.out);
            t.transform(source, result);
        }
    
    }
    

    Output

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <data>
        <field1>Value</field1>
        <field2>NEW FIELD 2</field2>
        <field3>Value</field3>
        <field4>Value</field4>
        <field5>NEW FIELD 5</field5>
    </data>
    

    For More Information