0
votes

I need a help in the following scenario. I have got a XML String response. In that XML response I need to extract three values. I could not achieve it. I have specified the XML response and the value I need. Also I created set of .java file using pojo one line tool.

   My XML Response:
<?xml version="1.0" encoding="UTF-8"?>
<response>
  <control>
        <status>success</status>
        <senderid>XXXXX</senderid>
        <controlid>ControlIdHere</controlid>
        <uniqueid>false</uniqueid>
        <dtdversion>3.0</dtdversion>
  </control>
  <operation>
        <authentication>
              <status>success</status>
              <userid>XXXXX1</userid>
              <companyid>XXXXX</companyid>
              <sessiontimestamp>2014-08-22T07:12:37-07:00</sessiontimestamp>
        </authentication>
        <result>
              <status>success</status>
              <function>readByQuery</function>
              <controlid>testControlId</controlid>
              <data listtype="customer" count="100" totalcount="5142" numremaining="5042">
                    <customer>
                     <name>A</name>
         <id>12</id>
                    </customer>
                    <customer>
                     <name>A</name>
         <id>12</id>
                    </customer>
                    <customer>
                     <name>A</name>
         <id>12</id>
                    </customer>
              </data>
        </result>
  </operation>

In this XML response I would need the following values

Like count=100, totalcount=5142

My object class is like this

public class Data {
private String totalcount;
private Sodocument[] sodocument;
private String numremaining;
private String count;
private String listtype;
public String getTotalcount ()
{
    return totalcount;
}

public void setTotalcount (String totalcount)
{
    this.totalcount = totalcount;
}

public Sodocument[] getSodocument ()
{
    return sodocument;
}

public void setSodocument (Sodocument[] sodocument)
{
    this.sodocument = sodocument;
}

public String getNumremaining ()
{
    return numremaining;
}

public void setNumremaining (String numremaining)
{
    this.numremaining = numremaining;
}

public String getCount ()
{
    return count;
}
}


  XMLInputFactory xif = XMLInputFactory.newFactory();
  Reader reader = new StringReader(response.toString());
  XMLStreamReader xsr = xif.createXMLStreamReader(reader);
  JAXBContext jc = JAXBContext.newInstance(Data.class);
  Unmarshaller unmarshaller = jc.createUnmarshaller();
  Data jb = unmarshaller.unmarshal(xsr,Data.class).getValue();
  System.out.println(jb.getCount());

This is my JAXB class. For the value getCount gives me null response. Can somebody help me on fixing this?

2
I believe your Data class needs to use the appropriate annotations (@XmlRootElement, @XmlElement, @XmlAttribute) in order to be parsed correctly. If you have a schema for your xml (there are tools that will generate the xsd for you) then you can use xjc to generate your Data class with the appropriate annotations. - headlikearock
I have tried @XMLRootElement and (@XMLElement and @XMLAttribute) with "data" as the value.Am I doing something wrong here.What is the right way of doing it. With this what is the correct one I need to give in the annotation - Karthi

2 Answers

0
votes

The generated class is too long for me to paste here but here is what you can try:

1) Use freeformatter.com/xsd-generator.html to generate an xsd from your xml. Copy and paste the generated xsd to a file.

2) Use xjc (comes with jdk installation) to generate the java class. It is a command line tool and you just have to run "xjc generated.xsd" and it will generate the annotated version of your class. The generated file will be for the entire xml response - so your class will really be Response.java, not Data.java and you then can drill into the Data element and it's attributes.

0
votes

Add these annotations:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Data {
    @XmlAttribute
    private String totalcount;
    @XmlElement
    private Sodocument[] sodocument;
    @XmlAttribute
    private String numremaining;
    @XmlAttribute
    private String count;
    @XmlAttribute
    private String listtype;
    ...

But note that Sodocument[] sodocument (which should be a List!) does not match <customer>. Not sure what the model is - could it be another element as well (depending on listtype!)