Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB Unmarshalling issues with XML attributes

I am having an issue unmarshalling with JAXB for an XML file. There is an attribute on some of the xml elements called "display_value" which I need to obtain. Here is a small example of the XML file:

<unload>
  <change_request>
    <active>true</active>
    <approval>not requested</approval>
    <assigned_to display_value=""/>
    <alt_poc display_value="Tom Ford">056468745677484657</alt_poc>
    <poc display_value="Matt Ryan">56465148754878</poc>
   </change_request>
</unload>

I assume that in my ChangeRequest class I would simply annotate @XmlAttribute(name="display_value) on the fields that have the display_value that attribute, such as alt_poc but that doesn't seem to work. Here is an example of my ChangeRequest class.

@XmlAccessorType(XmlAccessType.FIELD)
public class ChangeRequest{
  String active;
  String approval;
  String assigned_to;
  String alt_poc;
  String poc;
}

I do have a class that contains a list of ChangeRequest objects, called ChangeRequests. This class is simple and looks like:

@XmlRootElement(name="unload")
public class ChangeRequests{
ArrayList<ChangeRequest> changeRequestList;

@XmlElement(name="change_request")
public ArrayList<ChangeRequest> getRecords(){
  return changeRequestList;
}

Finally I will show you the JAXB code where I do all of this

URL url = new URL("wwww.somethingInteresting.com/syz.xml");
try {
   JAXBConext jc = JAXBContext.newInstance(ChangeRequest.class, ChangeRequests.class);
   Unmarshaller un  = jc.createUnmarshaller();

   return (ChangeRequests) un.unmarshal(url);
} catch(JAXBException e){
    thow new RunTimeException(e);
}

Currently, all the code works, however I cannot get the display_value when I need it. Instead of the display_value I am getting the long number like 65484435487.

Any help anyone can provide would be great. Thank you!

like image 604
user1117891 Avatar asked Jul 27 '14 15:07

user1117891


People also ask

What is unmarshalling XML?

Unmarshalling is the process of reading XML documents into Java objects. The JAXBContext class provides the client's entry point to the JAXB API.

How do you Unmarshal XML string to Java object using JAXB?

To unmarshal an xml string into a JAXB object, you will need to create an Unmarshaller from the JAXBContext, then call the unmarshal() method with a source/reader and the expected root object.

What is JAXB unmarshalling?

The JAXB Unmarshaller interface is responsible for governing the process of deserializing the XML data to Java Objects. The unmarshalling to objects can be done to variety of input sources.

Is JAXB memory efficient?

Generally JAXB is quite efficient and you shouldn't care about memory issues unless your application handles XMLs of very large size.


1 Answers

You need to create separate classes for each element that has XML attributes, you cannot define display_value on the ChangeRequest class. Here is an example.

First I generated an XSD from your example XML:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="unload" type="unloadType"/>
  <xs:complexType name="alt_pocType">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute type="xs:string" name="display_value"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="change_requestType">
    <xs:sequence>
      <xs:element type="xs:string" name="active"/>
      <xs:element type="xs:string" name="approval"/>
      <xs:element type="assigned_toType" name="assigned_to"/>
      <xs:element type="alt_pocType" name="alt_poc"/>
      <xs:element type="pocType" name="poc"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="assigned_toType">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute type="xs:string" name="display_value"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="pocType">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute type="xs:string" name="display_value"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="unloadType">
    <xs:sequence>
      <xs:element type="change_requestType" name="change_request"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

Then I generated JAXB classes from this XSD. Here is the ChangeRequestType:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "change_requestType", propOrder = {
    "active",
    "approval",
    "assignedTo",
    "altPoc",
    "poc"
})
public class ChangeRequestType {

    @XmlElement(required = true)
    protected String active;
    @XmlElement(required = true)
    protected String approval;
    @XmlElement(name = "assigned_to", required = true)
    protected AssignedToType assignedTo;
    @XmlElement(name = "alt_poc", required = true)
    protected AltPocType altPoc;
    @XmlElement(required = true)
    protected PocType poc;

  // Getters and setters follow
}

And here is AssignedToType, for example. Notice display_value must be defined here:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "assigned_toType", propOrder = {
        "value"
    })
    public class AssignedToType {

        @XmlValue
        protected String value;
        @XmlAttribute(name = "display_value")
        protected String displayValue;

        // Getters and setters follow
}
like image 143
Khary Mendez Avatar answered Sep 21 '22 05:09

Khary Mendez