首先,我想指出您的示例不是 xml 文件:没有根元素,我猜它应该类似于<ORDER="123">
and 。<ORDER_LINE="3">
<ORDER ID="123">
<ORDER_LINE ID="3">
其次,如果我必须创建一个 xml 文件,我会使用JAXB 库和注释:
我创建了一个Order
包含有关 ORDER 的所有信息的类(它引用了一个列表OrderLine
):
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
@XmlAccessorType(XmlAccessType.FIELD)
public class Order {
@XmlAttribute(name="ID")
private String id;
@XmlElement(name="ORDER_LINE")
private List<OrderLine> orderLineList = new ArrayList<OrderLine>();
public Order(String id) {
this.id = id;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public List<OrderLine> getOrderLineList() {
return orderLineList;
}
public void setOrderLineList(List<OrderLine> orderLineList) {
this.orderLineList = orderLineList;
}
public Order addOrderLine(OrderLine orderLine) {
orderLineList.add(orderLine);
return this;
}
}
然后我创建了一个OrderLine
包含有关 ORDER_LINE 的所有信息的类:
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
@XmlAccessorType(XmlAccessType.FIELD)
public class OrderLine {
@XmlAttribute(name="ID")
private String id;
@XmlAttribute(name="product")
private String product;
@XmlAttribute(name="QUANTITY")
private String quantity;
public OrderLine(String id, String product, String quantity) {
this.id = id;
this.product = product;
this.quantity = quantity;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getProduct() {
return product;
}
public void setProduct(String product) {
this.product = product;
}
public String getQuantity() {
return quantity;
}
public void setQuantity(String quantity) {
this.quantity = quantity;
}
}
最后我创建了一个类Orders
,它将成为我的 xml 文件的根元素:
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="ORDERS")
public class Orders {
@XmlElement(name="ORDER")
List<Order> orderList = new ArrayList<Order>();
public List<Order> getOrderList() {
return orderList;
}
public Orders addOrder(Order order) {
orderList.add(order);
return this;
}
public static void main(String[] args) {
Orders orders = new Orders()
.addOrder(new Order("123")
.addOrderLine(new OrderLine("1", "abc", "1"))
.addOrderLine(new OrderLine("2", "def", "2")))
.addOrder(new Order("456")
.addOrderLine(new OrderLine("3", "ghi", "3"))
.addOrderLine(new OrderLine("4", "jkl", "4"))
.addOrderLine(new OrderLine("5", "mno", "2")));
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Orders.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
// output pretty printed
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(orders, System.out);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
在 main 方法中,我只是创建一个实例Orders
并用所需的信息填充它。之后,我只使用 JAXB 库中的方法。输出如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ORDERS>
<ORDER ID="123">
<ORDER_LINE ID="1" product="abc" QUANTITY="1"/>
<ORDER_LINE ID="2" product="def" QUANTITY="2"/>
</ORDER>
<ORDER ID="456">
<ORDER_LINE ID="3" product="ghi" QUANTITY="3"/>
<ORDER_LINE ID="4" product="jkl" QUANTITY="4"/>
<ORDER_LINE ID="5" product="mno" QUANTITY="2"/>
</ORDER>
</ORDERS>
希望这会有所帮助。