📄 orderprocessor.java~24~
字号:
package xmltest;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.*;
public class OrderProcessor {
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());
}
Element root = doc.getDocumentElement();
changeOrder(root, "status", "processing");
NodeList orders = root.getElementsByTagName("status");
for (int orderNum = 0;
orderNum < orders.getLength();
orderNum++) {
System.out.println(orders.item(orderNum)
.getFirstChild().getNodeValue());
}
//////////////////////////////
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());
File newFile = new File("processedOrders.xml");
try {
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 ex) {
ex.printStackTrace();
}
// Element root = doc.getDocumentElement();
// stepThroughAll(root);
// changeOrder(root, "status", "processing");
// NodeList orders = root.getElementsByTagName("status");
// for (int orderNum = 0;
// orderNum < orders.getLength();
// orderNum++) {
//System.out.println(orders.item(orderNum)
// .getFirstChild().getNodeValue());
//}
}
private static void stepThrough(Node start) {
System.out.println(start.getNodeName() + " = " + start.getNodeValue());
for (Node child = start.getFirstChild();
child != null;
child = child.getNextSibling()) {
stepThrough(child);
}
}
private static void stepThroughAll(Node start) {
System.out.println(start.getNodeName() + " = " + start.getNodeValue());
if (start.getNodeType() == start.ELEMENT_NODE) {
NamedNodeMap startAttr = start.getAttributes();
for (int i = 0;
i < startAttr.getLength();
i++) {
Node attr = startAttr.item(i);
System.out.println(" Attribute: " + attr.getNodeName()
+ " = " + attr.getNodeValue());
}
}
for (Node child = start.getFirstChild();
child != null;
child = child.getNextSibling()) {
stepThroughAll(child);
}
}
private static void changeOrder(Node start,
String elemName,
String elemValue) {
if (start.getNodeName().equals(elemName)) {
start.getFirstChild().setNodeValue(elemValue);
}
for (Node child = start.getFirstChild();
child != null;
child = child.getNextSibling()) {
changeOrder(child, elemName, elemValue);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -