DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How To Get the Comments From a DOCX Document in Java
  • Extracting Data From Very Large XML Files With X-definition
  • How to Detect Spam Content in Documents Using C#
  • Responsible AI Is an Engineering Problem, not a Policy Document

Trending

  • You Don't Get to Retrofit Trust: Why API Security Must Be Designed In, Not Bolted On
  • The Network Attach Problem Nobody Warns You About
  • How SaaS Architectures Break at Scale — and the Engineering Decisions That Prevent It
  • Alternative Structured Concurrency
  1. DZone
  2. Coding
  3. Languages
  4. Handle the Middle of a XML Document with JAXB and StAX

Handle the Middle of a XML Document with JAXB and StAX

By 
Blaise Doughan user avatar
Blaise Doughan
·
Aug. 27, 12 · Interview
Likes (0)
Comment
Save
Tweet
Share
20.1K Views

Join the DZone community and get the full member experience.

Join For Free
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>
 
XML Document StAX

Published at DZone with permission of Blaise Doughan. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How To Get the Comments From a DOCX Document in Java
  • Extracting Data From Very Large XML Files With X-definition
  • How to Detect Spam Content in Documents Using C#
  • Responsible AI Is an Engineering Problem, not a Policy Document

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook