August 12, 2012

Handle the Middle of a XML Document with JAXB and StAX

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("return");
        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>

Further Reading

If you enjoyed this post then you may also be interested in: 

9 comments:

  1. During marhsalling shouldnt root QName declaration look like this -
    QName root = new QName("return");

    ?

    ReplyDelete
    Replies
    1. You are correct it should have be "return" and not "response". I have made the correction.

      Thanks,
      Blaise

      Delete
  2. Thanks dude, really helped a lot - was pulling my hair out trying to communicate with an old SOAP-service not implementing the standard properly. I'm glad we've all moved to REST, JSON and JAX-B by now ;)

    ReplyDelete
  3. Thanks.. It helped me a lot for long debug

    ReplyDelete
  4. Thanks for the code - saved my time

    ReplyDelete
  5. Really appreciate. This is very helped mee..

    ReplyDelete
  6. Hi Blaise,

    Wonderful guide! Thanks!

    Observations:

    1. XMLOutputFactory method newFactory seems to have changed to newInstance. Method newFactory does not exist anymore.

    2. In testing with a ByteArrayOutputStream instead of System.out, I get the following exception:


    javax.xml.bind.MarshalException
    - with linked exception:
    [javax.xml.stream.XMLStreamException: Can not output XML declaration, after other output has already been done.]
    at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:330)
    at com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:178)

    Here is my code snippet:

    OutputStream output = new ByteArrayOutputStream();
    XMLOutputFactory xof = XMLOutputFactory.newInstance();
    XMLStreamWriter xsw = xof.createXMLStreamWriter(output);


    QName root = new QName("return");
    JAXBElement je = new JAXBElement(root, Customer.class, customer);

    xsw.writeStartDocument();
    xsw.setDefaultNamespace("S");
    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/");

    context = JAXBContext.newInstance(Customer.class);
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.marshal(je, xsw);
    xsw.writeEndDocument();
    xsw.close();
    if(output != null)
    return output.toString();


    Do you have any ideas why this occurs? Thanks in advance

    ReplyDelete
    Replies
    1. 1) XMLOutputFactory.newInstance() was deprecated and replaced with XMLOuputFactory.newFactory(). If you aren't seeing it then you are using an older version of XMLOutputFactory.
      - http://docs.oracle.com/javase/7/docs/api/javax/xml/stream/XMLOutputFactory.html#newFactory%28%29

      2) You need to set the Marshaller.JAXB_FRAGMENT property on the Marshaller when you are marshalling to the middle of an XML document.
      - http://stackoverflow.com/a/19493143/383861

      Delete

Note: Only a member of this blog may post a comment.