JAXB Unmarshalling XML字符串 - 循环遍历所有标记 [英] JAXB Unmarshalling XML string - Looping through all tags
本文介绍了JAXB Unmarshalling XML字符串 - 循环遍历所有标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是Java编程的新手,我正在解组以下XML字符串。我的任务是在
这个字符串中获取客户的名字。我为一位顾客做过。我需要获得所有客户名称。我需要循环部分的帮助。这适用于一个客户
I am new to Java programming and I am doing Unmarshalling the following XML string. My task is get the names of the customers in this string. I have done it for one customer. I need to get all the customer names. I need help on the looping part. This works for one customer
我的Java代码:
XMLInputFactory xif = XMLInputFactory.newFactory();
Reader reader = new StringReader(response.toString());
XMLStreamReader xsr = xif.createXMLStreamReader(reader);
while(xsr.hasNext()) {
if(xsr.isStartElement() && xsr.getLocalName().equals("customer")) {
break;
}
xsr.next();
}
JAXBContext jc = JAXBContext.newInstance(Customer.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
JAXBElement<Customer> jb = unmarshaller.unmarshal(xsr,Customer.class);
Customer customer = jb.getValue();
System.out.println(customer.NAME);
客户类别:
@XmlRootElement(name = "customer")
public class Customer {
public String NAME;
public String getNAME ()
{
return NAME;
}
}
数据类:
@XmlRootElement(namespace = "data")
public class Data
{
@XmlElementWrapper(name = "data")
// XmlElement sets the name of the entities
@XmlElement(name = "customer")
{
private Customer[] customer;
public Customer[] getCustomer ()
{
return customer;
}
<data>
<customer>
<name>ABC</name>
<city>DEF</city>
</customer>
<customer>
<name>ABC</name>
<city>DEF</city>
</customer>
<customer>
<name>ABC</name>
<city>DEF</city>
</customer>
</data>
推荐答案
以下是Java类数据和客户的修订版,加上一些解组的代码:
Here is a revision of you Java classes Data and Customer, plus some code to unmarshal:
@XmlRootElement
public class Response {
@XmlElement
private Data data;
public Data getData(){ return data; }
public void setData( Data value ){ data = value; }
}
public class Data { // omitted namespace="data" as it isn't in the XML
@XmlElement(name = "customer")
private List<Customer> customer; // List is much better than array
public List<Customer> getCustomer (){
if( customer == null ){
customer = new ArrayList<>();
}
return customer;
}
}
@XmlType(name = "Customer")
public class Customer {
private String name; // stick to Java conventions: lower case
public String getName (){
return name;
}
public void setName( String value ){
name = value;
}
}
JAXBContext jc = JAXBContext.newInstance( Response.class );
Unmarshaller m = jc.createUnmarshaller();
Data data = null;
try{
// File source = new File( XMLIN );
StringReader source = new StringReader( stringWithXml ); // XML on a String
data = (Data)m.unmarshal( source );
for( Customer cust: data.getCustomer() ){
System.out.println( cust.getName() );
}
} catch( Exception e ){
System.out.println( "EXCEPTION: " + e.getMessage() );
e.printStackTrace();
}
不确定为什么使用XMLStreamReader,但如果你愿意,你可以改变它。
Not sure why you use an XMLStreamReader, but you can change this if you like.
这篇关于JAXB Unmarshalling XML字符串 - 循环遍历所有标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文