Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML to POJO JAVA

Tags:

java

xml

I have read other posts related to this issue but I could not fix my problem. I try to convert the following XML String to a JAVA class but when I try to access param1 using getParam1() method it returns null and I am not sure why.

The XML String:

<?xml version="1.0" encoding="utf-8"?>
<REQUERYTRXRESPONSE xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
 <param1>3gbahtJf1y85Oks4HrPLkqTQZV8Yg8pIhdXOrZ8pLGJP3FLwqKlIzIl/GgUpGvFaw4MC4SV+4pCudmVq+apIMIJJS4PrVyUx4T0ZO/Tsui4ZqCn62dLAG0DVhBVz2ZasF4yr7CRYnk47FWS0RywXmA==</param1>
 <param2>lO4ismiJwsvBiHQGW/UwCA==</param2>
 <param3 />
</REQUERYTRXRESPONSE>

The Java class:

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(namespace = "http://tempuri.org/", name =  "REQUERYTRXRESPONSE")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class REQUERYTRXRESPONSE {
private String param1;
private String param2;
private String param3;

@XmlElement(required = true, name = "param1")
public String getParam1() {
    return param1;
}
public void setParam1(String param1) {
    this.param1 = param1;
}

@XmlElement(required = true, name = "param2")
public String getParam2() {
    return param2;
}
public void setParam2(String param2) {
    this.param2 = param2;
}

@XmlElement(required = true, name = "param3")
public String getParam3() {
    return param3;
}
public void setParam3(String param3) {
    this.param3 = param3;
}
}

The XML to Java class code:

HttpRequest httpRequest = HttpRequest.get();

    if (httpRequest.ok()) {
        String response = httpRequest.body();

        System.out.println(response);

        JAXBContext jaxbContext = JAXBContext.newInstance(REQUERYTRXRESPONSE.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        REQUERYTRXRESPONSE requerytrxresponse = (REQUERYTRXRESPONSE) unmarshaller.unmarshal(new StringReader(response));


        System.out.println((String) requerytrxresponse.getParam1()); // returns null
    }   
like image 713
R.A Avatar asked Dec 12 '25 07:12

R.A


1 Answers

Managed to figure it out.

@XmlRootElement(name = "REQUERYTRXRESPONSE")
@XmlAccessorType(XmlAccessType.FIELD)
public class Response {

   private String param1;
   private String param2;
   private String param3;

   public String getParam1() {
      return param1;
   }

   public void setParam1(String param1) {
      this.param1 = param1;
   }

   public String getParam2() {
      return param2;
   }

   public void setParam2(String param2) {
      this.param2 = param2;
   }

   public String getParam3() {
      return param3;
   }

   public void setParam3(String param3) {
      this.param3 = param3;
   }

}

You don't need to specify the @XmlElement when you do the @XxmlAccessorType unless you wanted the required=true part.

What I changed is that I moved the namespace from @XmlRootElement in a package-info.java class like so:

@javax.xml.bind.annotation.XmlSchema(namespace = "http://tempuri.org/",
        elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package com.sfatandrei.soplayground.model;

My main test method includes:

  final InputStream resourceAsStream = SoPlaygroundApplication.class.getClassLoader().getResourceAsStream("test.xml");
  JAXBContext jaxbContext = JAXBContext.newInstance(Response.class);
  Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
  Response response = (Response) unmarshaller.unmarshal(resourceAsStream);
  System.out.println(response);
like image 56
Andrei Sfat Avatar answered Dec 13 '25 19:12

Andrei Sfat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!