首先,我想指出的是您的例子不是一個XML文件:沒有根元素,我想這<ORDER="123">
和<ORDER_LINE="3">
應該是像<ORDER ID="123">
和<ORDER_LINE ID="3">
。
其次,如果我不得不創建一個XML文件,我會用JAXB library和註釋:
我已經創建了一個包含所有有關訂單的信息類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;
}
}
然後,我已經創建了一個包含所有關於ORDER_LINE信息類OrderLine
:
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>
希望這對我很有幫助。
向我們顯示您的代碼。它很難理解你。 – Jayamohan