[英]Pulling XML child inner node from XML to Java Object using JAXB
I have been trying to pull a child node from an XML which I received from a SOAP as string. 我一直在尝试从我从SOAP接收的XML字符串中提取一个子节点。 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 (ie id,name and shortname) using JAXB. 现在我的问题是如何使用JAXB提取子内容(即id,name和shortname)。 I created class Response and Stuff and made stuff a list inside response but when ever I run the code it throws null exception. 我创建了Response和Stuff类,并在response中创建了一个列表,但是每当我运行代码时,它将引发null异常。
PS: Please remember the XML is not a file its a string and am using StringReader
class for the JAXB.unmarshal
PS:请记住,XML不是一个字符串文件,并且正在使用StringReader
类作为JAXB.unmarshal
JAXBContext jcontext= JAXBContext.newInstance(); JAXBContext jcontext = JAXBContext.newInstance();
unmarshaller um=jcontext.createUnmarshaller(); unmarshaller um = jcontext.createUnmarshaller();
JAXBElement je=um.unmarshal(new File("pass xml file location")); JAXBElement je = um.unmarshal(new File(“ pass xml file location”));
response r= getValue(); 响应r = getValue();
r.getlist().getcart().getstuff().getid().getValue(); r.getlist()getcart()getstuff()的getId()的getValue()。。
try this one it may work 试试这个可能会起作用
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 有了这个,我就很容易编组和解组
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.