Team lead for the TopLink/EclipseLink JAXB & SDO implementations, and the Oracle representative on those specifications. Blaise is a DZone MVB and is not an employee of DZone and has posted 44 posts at DZone. You can read more from them at their website. View Full User Profile

Handle the Middle of a XML Document with JAXB and StAX

08.26.2012
| 6098 views |
  • submit to reddit
Recently I have come across a lot of people asking how to read data from, or write data to the middle of an XML document.  In this post I will demonstrate how this can be done using JAXB with StAX.  Note:  JAXB (JSR-222) and StAX (JSR-173) implementations are included in the JDK/JRE since Java SE 6.

XML (input.xml)

We will be using a SOAP message as our sample XML.  The outer portions of the XML document represent information relevant to the Web Service and the inner portions (lines 5-8) represent the data we want to convert to our domain model.
<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body>
        <ns0:findCustomerResponse xmlns:ns0="http://service.jaxws.blog/">
            <return id="123">
                <firstName>Jane</firstName>
                <lastName>Doe</lastName>
            </return>
        </ns0:findCustomerResponse>
    </S:Body>
</S:Envelope>

Java Model 

Our Java model consists of a single domain class.  The concepts in this example also apply to larger domain models.
package blog.stax.middle;
 
import javax.xml.bind.annotation.*;
 
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
 
    @XmlAttribute
    int id;
     
    String firstName;
     
    String lastName;
     
}
Unmarshal Demo

To unmarshal from the middle of an XML document all we need to do is the following:
  1. Create an XMLStreamReader from the XML input (line 12).
  2. Advance the XMLStreamReader to the return element (lines 13-16).
  3. Unmarshal an instance of Customer from the XMLStreamReader (line 20)
    package blog.stax.middle;
     
    import javax.xml.bind.*;
    import javax.xml.stream.*;
    import javax.xml.transform.stream.StreamSource;
     
    public class UnmarshalDemo {
     
        public static void main(String[] args) throws Exception {
            XMLInputFactory xif = XMLInputFactory.newFactory();
            StreamSource xml = new StreamSource("src/blog/stax/middle/input.xml");
            XMLStreamReader xsr = xif.createXMLStreamReader(xml);
            xsr.nextTag();
            while(!xsr.getLocalName().equals("return")) {
                xsr.nextTag();
            }
     
            JAXBContext jc = JAXBContext.newInstance(Customer.class);
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            JAXBElement<Customer> jb = unmarshaller.unmarshal(xsr, Customer.class);
            xsr.close();
     
            Customer customer = jb.getValue();
            System.out.println(customer.id);
            System.out.println(customer.firstName);
            System.out.println(customer.lastName);
        }
     
    }
 
Output 

Below is the output from running the unmarshal demo.
123
Jane
Doe

Marshal Demo

To marshal to the middle of an XML document all we need to do is the following:
  1. Create an XMLStreamWriter for the XML output (line 18).
  2. Start the document and write the outer elements (lines 19-22).
  3. Set the Marshaller.JAXB_FRAGMENT property on the Marshaller (line 26) to prevent the XML declaration from being written.
  4. Marshal an instance of Customer to the XMLStreamWriter (line 27).
  5. End the document, this will close any elements that have been opened (line 29).
    package blog.stax.middle;
     
    import javax.xml.bind.*;
    import javax.xml.namespace.QName;
    import javax.xml.stream.*;
     
    public class MarshalDemo {
     
        public static void main(String[] args) throws Exception {
            Customer customer = new Customer();
            customer.id = 123;
            customer.firstName = "Jane";
            customer.lastName = "Doe";
            QName root = new QName("response");
            JAXBElement<Customer> je = new JAXBElement<Customer>(root, Customer.class, customer);
     
            XMLOutputFactory xof = XMLOutputFactory.newFactory();
            XMLStreamWriter xsw = xof.createXMLStreamWriter(System.out);
            xsw.writeStartDocument();
            xsw.writeStartElement("S", "Envelope", "http://schemas.xmlsoap.org/soap/envelope/");
            xsw.writeStartElement("S", "Body", "http://schemas.xmlsoap.org/soap/envelope/");
            xsw.writeStartElement("ns0", "findCustomerResponse", "http://service.jaxws.blog/");
     
            JAXBContext jc = JAXBContext.newInstance(Customer.class);
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
            marshaller.marshal(je, xsw);
             
            xsw.writeEndDocument();
            xsw.close();
        }
     
    }
 Output 

Below is the output from running the marshal demo.  Note that the output from running the demo code will appear on a single line, I have formatted the output here to make it easier to read.
<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body>
        <ns0:findCustomerResponse xmlns:ns0="http://service.jaxws.blog/">
            <return id="123">
                <firstName>Jane</firstName>
                <lastName>Doe</lastName>
            </return>
        </ns0:findCustomerResponse>
    </S:Body>
</S:Envelope>
 
Published at DZone with permission of Blaise Doughan, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)