转自本人博客: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文件,依照公司、即属性comp_name不同,分别分成两个xml文件。并把子节点中的全部属性提取出来放到元素中。
原始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和IBM.xml 例如以下:
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>
要实现这一功能。须要用到的操作有下面几部:
1、创建文档Document类型对象。读入原始文件ipo.xml。
2、找到其根节点,并递归遍历其整个树,而且把实用的信息提取出来。
3、分别输出两个分好类的文件。
具体的代码可以直接看实现过程,里面有非常具体的凝视,看完之后可以对DOM4J主要的处理思路有所了解:
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"); } }