转自本人博客:http://www.xgezhang.com/dom4j_xml_separata.html
dom4j是一个Java的XML API,类似于jdom,用来读写XML文件的。dom4j是一个十分优秀的Java XML API,具有性能优异、功能强大和极其易使用的特点,同时它也是一个开放源代码的软件,可以在SourceForge上找到它。在IBM developerWorks上面还可以找到一篇文章,对主流的Java XML API进行的性能、功能和易用性的评测,所以可以知道dom4j无论在哪个方面都是非常出色的。
相比于Dom,don4j的使用更加有”Java”的感觉,也更加方便。只要知道xml文件的树形结构,便可以对节点进行添加、修改、删除等工作<?xml version="1.0"?>
<!-- edited with XMLSPY v2004 rel. 4 U (http://www.xmlspy.com) by Mr. Nobody (Altova GmbH) -->
<purchaseOrders>
<purchaseOrder comp_name="ABC">
<shipTo export-code="1" type="EU-Address">
<name>Helen Zoe</name>
<street>47 Eden Street</street>
<city>Cambridge</city>
<postcode>126</postcode>
</shipTo>
<item partNum="128-UL">
<productName>Jade earring</productName>
<quantity>5</quantity>
<price>179.90</price>
<shipDate>2000-02-14</shipDate>
</item>
</Items>
</purchaseOrder>
<purchaseOrder comp_name="IBM">
<shipTo export-code="3" type="US-Address">
<name>Google</name>
<street>47 Eden Street</street>
<city>Pal alto</city>
<postcode>95126</postcode>
</shipTo>
<item partNum="1289-UL">
<productName>Jade earring</productName>
<quantity>5</quantity>
<price>179.90</price>
<shipDate>2000-02-14</shipDate>
</item>
</Items>
</purchaseOrder>
<purchaseOrder comp_name="ABC">
<shipTo export-code="4" type="EU-Address">
<name>Zou</name>
<street>47 Eden Street</street>
<city>Cambridge</city>
<postcode>126</postcode>
</shipTo>
<billTo type="US-Address">
<name>Robert Smith</name>
<street>8 Oak Avenue</street>
<city>X Town</city>
<state>AK</state>
<zip>95819</zip>
</billTo>
<Items>
<item partNum="101AA">
<productName>Lapis necklace</productName>
<quantity>4</quantity>
<price>199.95</price>
<comment>The Gifts for the holidays!</comment>
<shipDate>1999-12-05</shipDate>
</item>
<item partNum="1281-UL">
<productName>Jade earring</productName>
<quantity>5</quantity>
<price>179.90</price>
<shipDate>2000-02-14</shipDate>
</item>
</Items>
</purchaseOrder>
</purchaseOrders>
ABC:<?xml version="1.0" encoding="UTF-8"?>
<purchaseOrders>
<ABC_COMP>
<purchaseOrder>
<item>
<partNum>229-OB</partNum>
<productName>Pearl necklace</productName>
<quantity>1</quantity>
<price>4879.00</price>
<shipDate>1999-12-05</shipDate>
</item>
<item>
<partNum>128-UL</partNum>
<productName>Jade earring</productName>
<quantity>5</quantity>
<price>179.90</price>
<shipDate>2000-02-14</shipDate>
</item>
</purchaseOrder>
<purchaseOrder/>
<purchaseOrder>
<shipTo>
<export-code>4</export-code>
<type>EU-Address</type>
<name>Zou</name>
<street>47 Eden Street</street>
<city>Cambridge</city>
<postcode>126</postcode>
</shipTo>
</purchaseOrder>
</ABC_COMP>
</purchaseOrders>
IBM:
<?xml version="1.0" encoding="UTF-8"?>
<purchaseOrders>
<IBM_COMP>
<purchaseOrder/>
<purchaseOrder>
<shipTo>
<export-code>3</export-code>
<type>US-Address</type>
<name>Google</name>
<street>47 Eden Street</street>
<city>Pal alto</city>
<postcode>95126</postcode>
</shipTo>
<item>
<partNum>1289-UL</partNum>
<productName>Jade earring</productName>
<quantity>5</quantity>
<price>179.90</price>
<shipDate>2000-02-14</shipDate>
</item>
</purchaseOrder>
<purchaseOrder/>
</IBM_COMP>
</purchaseOrders>
3、分别输出两个分好类的文件。
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.List;
import javax.jws.WebParam.Mode;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class dom4j {
static String compMode = ""; //静态全局变量,对应按不同公司分类的方式
/**
*
* @param node 原始文件节点,Element类型
* @param compNode 输出文件节点
*/
public static void separate(Element node,Element compNode){ //递归遍历函数
//System.out.println(node.getName());
List<Attribute> attrList = node.attributes(); //获取节点所有的属性元素
for (Attribute attr : attrList){
//System.out.println(attr.getValue());
if (node.getName().equals("purchaseOrder")){ //要实现的功能是选出属性名为IBM或ABC的purchaseOrder节点
if (!attr.getValue().equals(compMode)) return;
} else {
compNode.addElement(attr.getName().toString()); //addElement为添加节点
compNode.element(attr.getName().toString()).setText(attr.getValue().toString());//设置子节点的值为当前属性值
}
}
if (!(node.getTextTrim().equals(""))) {
compNode.setText(node.getText());
}
Iterator<Element> it = node.elementIterator(); //迭代器循环获取所有的子节点,并递归调用
while (it.hasNext()) {
Element e = it.next();
//if (node.getName().equals("purchaseOrders")) separate(e,compNode);
// if (!node.getName().equals("purchaseOrders")) {
Element nextCompNode = compNode.addElement(e.getName().toString());
separate(e,nextCompNode);
//}
}
}
public static void writer(Document document,String filename) throws Exception{ //输出 xml文件
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8"); //设置编码方式
format.setIndent(true);
format.setIndent(" ");//设置缩进
XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(new File(filename)), "UTF-8"), format);
writer.write(document); //写入文件
writer.flush();
writer.close();
}
public static void main(String[] args) throws Exception{
SAXReader reader = new SAXReader(); //使用SAXReader 读入文件结构
Document document = reader.read(new File("ipo.xml")); //创建Document对象
Element node = document.getRootElement(); //获取根节点node
Document docABC = DocumentHelper.createDocument();
Document docIBM = DocumentHelper.createDocument();
Element ABCElement = docABC.addElement("purchaseOrders");
ABCElement = ABCElement.addElement("ABC_COMP");
Element IBMElement = docIBM.addElement("purchaseOrders");
IBMElement = IBMElement.addElement("IBM_COMP");
System.out.println(ABCElement.getName());
compMode = "ABC";
separate(node,ABCElement);
compMode = "IBM";
separate(node,IBMElement);
writer(docABC,"ABC_COMP_dom4j.xml");
writer(docIBM,"IBM_COMP_dom4j.xml");
}
}