准备数据
输出是一个文件,它简单地列出每个订单、依据客户信用限额来确定的订单处理情况,以及 customerid。
<?xml version="1.0" encoding="UTF-8"?>
<processedOrders>
<order>
<status>PROCESSED</status>
<customerid>2341</customerid>
<amount>874.00</amount>
</order>
<order>
<status>REJECTED</status>
<customerid>251222</customerid>
<amount>200.00</amount>
</order>
</processedOrders>
应用程序首先创建要输出的 Document 对象。为方便起见,可以使用创建原先的 Document 的相同 DocumentBuilder 来创建新的 Document 对象。
...
public static void main (String args[]) {
File docFile = new File("orders.xml");
Document doc = null;
Document newdoc = null;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(docFile);
newdoc = db.newDocument();
} catch (Exception e) {
System.out.print("Problem parsing the file: "+e.getMessage());
}
...
thisOrder.insertBefore(totalElement, thisOrder.getFirstChild());
}
Element newRoot = newdoc.createElement("processedOrders");
NodeList processOrders = doc.getElementsByTagName("order");
for (int orderNum = 0;
orderNum < processOrders.getLength();
orderNum++) {
Element thisOrder = (Element)processOrders.item(orderNum);
Element customerid =
(Element)thisOrder.getElementsByTagName("customerid")
.item(0);
String limit = customerid.getAttributeNode("limit").getNodeValue();
String total = thisOrder.getElementsByTagName("total").item(0)
.getFirstChild().getNodeValue();
double limitDbl = new Double(limit).doubleValue();
double totalDbl = new Double(total).doubleValue();
Element newOrder = newdoc.createElement("order");
Element newStatus = newdoc.createElement("status");
if (totalDbl > limitDbl) {
newStatus.appendChild(newdoc.createTextNode("REJECTED"));
} else {
newStatus.appendChild(newdoc.createTextNode("PROCESSED"));
}
Element newCustomer = newdoc.createElement("customerid");
String oldCustomer = customerid.getFirstChild().getNodeValue();
newCustomer.appendChild(newdoc.createTextNode(oldCustomer));
Element newTotal = newdoc.createElement("total");
newTotal.appendChild(newdoc.createTextNode(total));
newOrder.appendChild(newStatus);
newOrder.appendChild(newCustomer);
newOrder.appendChild(newTotal);
newRoot.appendChild(newOrder);
}
newdoc.appendChild(newRoot);
System.out.print(newRoot.toString());
...
在处理 orders.xml之后,应用程序创建了一个新的元素 processedOrders,这个新的元素最终将成为新文档的根元素。然后它遍历每个订单。对于每个订单,它都提取其 total 和 limit 信息。
接下来,应用程序为订单创建新元素:order、status、customerid 和 amount。它根据汇总款项是否超过客户的信用限额来填充status,然后相应地填充其他元素。
一旦应用程序为订单创建了元素,它就必须将这些元素整合起来。它首先向新的 order 元素添加状态、客户信息和汇总款项。然后它把新的 order 添加到newRoot元素。
虽然发生了所有这一切,但是 newRoot 元素并没有实际连接到某个父节点。当应用程序完成所有订单的处理时,newRoot就被追加到新文档。
应用程序将 newRoot 转换为一个 String,并简单地将它发送到 System.out,从而输出数据。
创建 XML 文件
现在应用程序已经创建了新的信息,将这个信息输出到某个文件是很简单的。
对数据也使用了相同的逻辑,只不过应用程序不是将它输出到屏幕,而是将它输出到一个文件。
要注意的一件重要事情是,由于 XML 数据不过就是文本,因此可以通过任何方式来对它进行格式化。例如,您可以创建 stepThroughAll() 的某种变化形式,它将创建预期的或打印完美的版本。只需记住这样将创建额外的空白(文本)节点即可。
...
import java.io.FileWriter;
...
try
{
File newFile = new File("processedOrders.xml");
FileWriter newFileStream = new FileWriter(newFile);
newFileStream.write("<?xml version=/"1.0/"?>");
newFileStream.write("<!DOCTYPE
"+doc.getDoctype().getName()+" ");
if (doc.getDoctype().getSystemId() != null) {
newFileStream.write(" SYSTEM ");
newFileStream.write(doc.getDoctype().getSystemId());
}
if (doc.getDoctype().getPublicId() != null) {
newFileStream.write(" PUBLIC ");
newFileStream.write(doc.getDoctype().getPublicId());
}
newFileStream.write(">");
newFileStream.write(newRoot.toString());
newFileStream.close();
} catch (IOException e) {
System.out.println("Can't write new file.");
}
...
本文介绍了一种基于XML的订单处理方法,通过比较订单总额与客户信用限额来决定订单的状态(处理或拒绝),并详细展示了如何用Java创建和输出XML文档。
1534

被折叠的 条评论
为什么被折叠?



