下面是我的 xml 摘录:
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>Superb story of army veteran</description>
</book>
</catalog>
我想根据书拆分它,以便将其作为输出::
<Object-bean>
<child-bean>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
</child-bean>
<child-bean>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>Superb story of army veteran</description>
</book>
</child-bean>
</Object-bean>
子bean节点我需要通过注释与java类进行映射,以便将子bean节点列表作为字符串。这是我的java代码::
@XmlRootElement(name = "Child-bean")
@XmlAccessorType(XmlAccessType.NONE)
public class ChildBean {
@XmlAttribute(name="price")
private String price;
@XmlAttribute(name="country")
private String country;
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
这是我的对象 bean 类::
@XmlRootElement(name = "Object-bean")
@XmlAccessorType(XmlAccessType.FIELD)
public class ObjectBean {
@XmlAttribute(name = "name")
private String name;
@XmlAttribute
private String id;
@XmlElement(name="Child-bean")
private List<ChildBean> childList = new ArrayList<ChildBean>();
}