Pulling XML child inner node from XML to Java Object using JAXB

2.2k views Asked by At

I have been trying to pull a child node from an XML which I received from a SOAP as string. I can retrieve the parent node but couldn't retrieve the child node. I also surf the internet to see if I can get an answer but all to no avail. I saw some examples with direct child pulling, but that didn't solve my problem.

<response>
  <code>1</code>
  <list available="2">
     <cart id="2" name="egg">
        <stuff>
           <id>001</id>
           <name>Crew</name>
           <shortname>C</shortname>
        </stuff>
     </cart>
     <cart id="4" name="bread">
        <stuff>
           <id>004</id>
           <name>Bread</name>
           <shortname>B</shortname>
        </stuff>
     </cart>
  </list>
</response>

Response Class

public class Response {
   private String code;
   private String list;
   private String cart;
   private Response.Stuff stuffs;


   @XmlElement(name="code")
   public String getCode() {
     return code;
   }

   public void setCode(String code) {
     this.code = code;
   }
   .
   .
  @XmlElement
  public Response.Stuff getStuff() {
    return this.stuffs;
  }

  public void setStuff(Response.Cart stuff) {
    this.stuffs = stuff;
  }

 public static class Stuff {
   private List<Stuff> stuff;

   public List<Stuff> getStuff() {
     if (stuff == null) {
        stuff = new ArrayList<Stuff>();
     }
   return stuff;
  }
}

}

Stuff Classs

public class Stuff {
  private String id;
  private String crew;

 @XmlElement
 public String getId() {
  return this.id;
 }

 public void setId(String id) {
  this.id = id;
 }
 .
 .

}

Now my problem is how to pull child stuff content (i.e id,name and shortname) using JAXB. I created class Response and Stuff and made stuff a list inside response but when ever I run the code it throws null exception.

PS: Please remember the XML is not a file its a string and am using StringReader class for the JAXB.unmarshal

2

There are 2 answers

0
Prodigy On BEST ANSWER

I finally solved my problem by restructuring the above class and creating a new one. See below the updated:

public class Response {
   private String code;
   private List list;

   @XmlElement(name="code")
   public String getCode() {
     return code;
   }

  public void setCode(String code) {
    this.code = code;
  }

  @XmlElement(name= "list")
  public List getlist() {
    return this.list;
  }

  public void setList(List list) {
    this.list = list;
  }

}

Class List

@XmlRootElement(name= "list")
public class List {
  private List.Cart cart;

  @XmlElement(name= "cart")
  public List<Cart> getCart() {
    return this.cart;
  }

  public void setCart(Cart cart) {
    this.cart = cart;
  }


@XmlRootElement(name= "cart")
public static class Cart {
   private List<Stuff> stuff;

   private List<Stuff> getStuff() {
     if (stuff == null) {
       stuff = new ArrayList();
     }
     return stuff;
  }
}

}

With this, it was easy for me to marshal and unmarshal

1
Sannith Patlolla On

JAXBContext jcontext= JAXBContext.newInstance();

unmarshaller um=jcontext.createUnmarshaller();

JAXBElement je=um.unmarshal(new File("pass xml file location"));

response r= getValue();

r.getlist().getcart().getstuff().getid().getValue();

try this one it may work