Reddy Reddy - 5 months ago 13x
Java Question

Unmarshall part of xml to an object

I am trying to un-marshall an xml, which is turning out to be nightmare with lot of issues.
So I got an idea to unmarshall part of this xml to an object which is necessary for me.

Original xml:

now what I need from this xml is:

I have classes and other related info of both xmls.

If I manually give this data to unmarshall, i am getting StandardError object. But can i give original xml directly to get this object out of it.

ps: Using JAXB

Stack trace of original xml parsing:
Class file of original xml class which I am failing to unmarshall. No source control on this

StandardError class:
Main xml class:



I would parser the XML using a StAX XMLStreamReader, and advance the XMLStreamReader to the element that you wish to unmarshal. Then I would have JAXB unmarshal the XMLStreamReader. Something like the following:


package bgc.objects.rosy.standarderror.v3;

import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        XMLInputFactory xif = XMLInputFactory.newFactory();
        StreamSource xml = new StreamSource("src/bgc/objects/rosy/standarderror/v3/input.xml");
        XMLStreamReader xsr = xif.createXMLStreamReader(xml);
        while(xsr.hasNext()) {
            if(xsr.isStartElement() && xsr.getLocalName().equals("StandardError")) {

        JAXBContext jc = JAXBContext.newInstance(StandardError.class);
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        JAXBElement<StandardError> jb = unmarshaller.unmarshal(xsr, StandardError.class);
        StandardError response = jb.getValue();


        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(jb, System.out);



Below is the StandardError class

package bgc.objects.rosy.standarderror.v3;

//Compiled from (version 1.5 : 49.0, super bit)
@javax.xml.bind.annotation.XmlAccessorType(value = javax.xml.bind.annotation.XmlAccessType.FIELD)
@javax.xml.bind.annotation.XmlType(name = "", propOrder = { "code", "category",
        "severity", "description", "environment", "correlationId" })
@javax.xml.bind.annotation.XmlRootElement(name = "StandardError")
public class StandardError {

    // Field descriptor #15 J
    protected long code;

    // Field descriptor #17 Ljava/lang/String;
    @javax.xml.bind.annotation.XmlElement(required = true)
    protected java.lang.String category;

    // Field descriptor #17 Ljava/lang/String;
    @javax.xml.bind.annotation.XmlElement(required = true)
    protected java.lang.String severity;

    // Field descriptor #24 Ljava/util/List;
    // Signature: Ljava/util/List<Ljava/lang/String;>;
    @javax.xml.bind.annotation.XmlElement(required = true)
    protected java.util.List description;

    // Field descriptor #17 Ljava/lang/String;
    @javax.xml.bind.annotation.XmlElement(required = true)
    protected java.lang.String environment;

    // Field descriptor #17 Ljava/lang/String;
    @javax.xml.bind.annotation.XmlElement(required = true)
    protected java.lang.String correlationId;

    // Field descriptor #17 Ljava/lang/String;
    protected java.lang.String version;



I will use the @XmlSchema annotation on a package-info class to specify the namespace qualification for the classes in the bgc.objects.rosy.standarderror.v3 package.

@XmlSchema(namespace="urn:v3.standarderror.vss.objects.bgc", elementFormDefault=XmlNsForm.QUALIFIED)
package bgc.objects.rosy.standarderror.v3;

import javax.xml.bind.annotation.*;


Below is the output from running the demo code:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<StandardError xmlns="urn:v3.standarderror.vss.objects.bgc">
    <description xmlns:ns10="urn:v3.standarderror.vss.objects.bgc">APC operation AUDIT on position L02LAB00075:1-1-1-5
                returned an error : APC audit operation failed Caused by:
                unsupported.dslam.message Caused by: Unsupported DSLAM. Internal
                rollback of failed action : success.</description>

For More Information