欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

实现分解器把订单分解为单独的商品

程序员文章站 2022-01-20 10:05:41
...
输入的订单为:

<?xml version="1.0" encoding="UTF-8"?>
<order>
<date>23/6/2009</date>
<ordernumber>20090623</ordernumber>
<customer>
<id>12345</id>
<name>chenyuan</name>
</customer>
<orderitems>
<item>
<quantity>3.0</quantity>
<itemno>w1234</itemno>
<description>A widget</description>
</item>
<item>
<quantity>2.0</quantity>
<itemno>G2345</itemno>
<description>A Gadget</description>
</item>
</orderitems>
</order>


实现分解的代码(自己写的,感觉不是很好,有写的不错的请分享一下):


package org.xml.filterExam;

import java.io.*;

import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.*;

public class XMLSpliter {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
try
{
TransformerFactory tff=TransformerFactory.newInstance();

Transformer tf=tff.newTransformer();

DocumentBuilder db=dbf.newDocumentBuilder();
Document doc=db.parse(new File("NewFile3.xml"));

Element eleorder=doc.getDocumentElement();

NodeList nlItem=eleorder.getElementsByTagName("item");
int len=nlItem.getLength();
for(int i=0;i<len;i++)
{
Document doc1=db.newDocument();

Element eleOrderItem=doc1.createElement("orderitem");

Element eleDate=doc1.createElement("date");
Element eleOrderNumber=doc1.createElement("ordernumber");
Element eleID=doc1.createElement("customerid");
Element eleQuantity=doc1.createElement("quantity");
Element eleItemno=doc1.createElement("itemno");
Element eleDescription=doc1.createElement("description");


Node eledate=eleorder.getElementsByTagName("date").item(0);
Text textDate=doc1.createTextNode(eledate.getFirstChild().getNodeValue());
eleDate.appendChild(textDate);
eleOrderItem.appendChild(eleDate);

Node eleordernumber=eleorder.getElementsByTagName("ordernumber").item(0);
Text textOrdernumber=doc1.createTextNode(eleordernumber.getFirstChild().getNodeValue());
eleOrderNumber.appendChild(textOrdernumber);
eleOrderItem.appendChild(eleOrderNumber);

NodeList nlcustomer=eleorder.getElementsByTagName("customer");
Element elecustomer=(Element)nlcustomer.item(0);
Node eleid=elecustomer.getElementsByTagName("id").item(0);
Text textId=doc1.createTextNode(eleid.getFirstChild().getNodeValue());
eleID.appendChild(textId);
eleOrderItem.appendChild(eleID);

Element eleitem=(Element)nlItem.item(i);
Node elequantity=eleitem.getElementsByTagName("quantity").item(0);
Node eleitemno=eleitem.getElementsByTagName("itemno").item(0);
Node eledescription=eleitem.getElementsByTagName("description").item(0);

Text textQuantity=doc1.createTextNode(elequantity.getFirstChild().getNodeValue());
Text textItemno=doc1.createTextNode(eleitemno.getFirstChild().getNodeValue());
Text textDescription=doc1.createTextNode(eledescription.getFirstChild().getNodeValue());

eleQuantity.appendChild(textQuantity);
eleItemno.appendChild(textItemno);
eleDescription.appendChild(textDescription);

eleOrderItem.appendChild(eleQuantity);
eleOrderItem.appendChild(eleItemno);
eleOrderItem.appendChild(eleDescription);

doc1.appendChild(eleOrderItem);

DOMSource source=new DOMSource(doc1);

StreamResult result=new StreamResult(new File("File"+i+".xml"));

tf.transform(source, result);
}

}
catch(Exception e)
{
System.out.println("XMLSpliter has a problem:"+e.toString());
}
}

}



输出到两个XML文件中其中File1.xml为:

<?xml version="1.0" encoding="UTF-8" ?>
- <orderitem>
<date>23/6/2009</date>
<ordernumber>20090623</ordernumber>
<customerid>12345</customerid>
<quantity>3.0</quantity>
<itemno>w1234</itemno>
<description>A widget</description>
</orderitem>

File2.xml为:

<?xml version="1.0" encoding="UTF-8" ?>
- <orderitem>
<date>23/6/2009</date>
<ordernumber>20090623</ordernumber>
<customerid>12345</customerid>
<quantity>2.0</quantity>
<itemno>G2345</itemno>
<description>A Gadget</description>
</orderitem>
相关标签: XML