0
我在使用Apache Camel解析XML時遇到問題。我添加了所有必要的註釋,接下來我對多個根元素有一些問題,然後使用List創建了新類。現在,我告訴我的代碼如下:使用Apache Camel解析XML(打印null)
客戶
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public @Data class Customer {
private int id;
private String name;
private String address;
private String countryCode;
@XmlElementWrapper(name="products")
@XmlElement(name="product")
private List<Product> products;
}
產品
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public @Data class Product {
private int productId;
private String productName;
}
客戶(類,因爲多根問題創建)
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public @Data class Customers {
@XmlElementWrapper
@XmlElement(name = "customer")
private List<Customer> customers;
@Override
public String toString() {
return "Customers{" +
"customers=" + customers +
'}';
}
}
MyRouteBuilder
public class MyRouteBuilder extends RouteBuilder {
@Override
public void configure() throws Exception {
from("file:/home/tkaczmarek/usr/data/inbox?noop=true")
.bean(XmlToBean.class);
}
}
XmlToBean
public class XmlToBean {
public void transformXmlObject(Customers customers) {
System.out.println(customers);
}
}
xml文件
<?xml version="1.0" encoding="UTF-8"?>
<customers>
<customer>
<id>1</id>
<name>Tomasz Kaczmarek</name>
<address>Łódź, ul. Błotna 5/32</address>
<countryCode>1221</countryCode>
<products>
<product>
<productId>1</productId>
<productName>Bread</productName>
</product>
<product>
<productId>2</productId>
<productName>Meat</productName>
</product>
</products>
</customer>
</customers>
目前輸出
Customers{customers=null}
如何讓我的代碼正常工作?我不知道問題出在哪裏。
你在類路徑上有camel-jaxb嗎?你用什麼版本的駱駝?並且你能更好地解釋什麼是問題,它有點不清楚 –
你是什麼意思的多根問題? –