转自本人博客:http://www.xgezhang.com/jdom_xml_separate.html
JDOM是两位著名的 Java 开发人员兼作者,Brett Mclaughlin 和 Jason Hunter 的创作成果, 2000 年初在类似于Apache协议的许可下,JDOM作为一个开放源代码项目正式开始研发了。它已成长为包含来自广泛的 Java 开发人员的投稿、集中反馈及错误修复的系统,并致力于建立一个完整的基于 Java 平台的解决方案,通过 Java 代码来访问、操作并输出 XML 数据。
JDOM在对xml文件的操作上面,比DOM更方便快速,并且能够解决SAX模型不能对文件进行修改操作的弊端。
这里要实现的功能和http://www.xgezhang.com/dom4j_xml_separata.html里描述的一样,只是选用的包不同,有些具体的细节需要注意,代码如下:
package project1; import java.io.FileOutputStream; import java.util.Iterator; import java.util.List; import org.jdom.Attribute; import org.jdom.Document; import org.jdom.Element; import org.jdom.input.SAXBuilder; import org.jdom.output.Format; import org.jdom.output.XMLOutputter; public class jdom { static String compMode = ""; //静态全局变量,对应按不同公司分类的方式 static Document docABC = new Document(); static Document docIBM = new Document(); public static void outputer(Document document,String filename) throws Exception{ //输出 xml文件 XMLOutputter out = new XMLOutputter(); Format format = Format.getPrettyFormat(); format.setIndent(" ") ; out.setFormat(format); out.output(document, new FileOutputStream(filename)); } public static void separate(Element node,Element compNode) throws Exception{ //递归遍历函数 List<Attribute> attrList = node.getAttributes(); //获取节点所有的属性元素 int attrListSize = attrList.size(); for (int j=0; j<attrListSize; j++) { Attribute attr = (Attribute) attrList.get(j); if (node.getName().equals("purchaseOrder")){ //要实现的功能是选出属性名为IBM或ABC的purchaseOrder节点 if (!attr.getValue().equals(compMode)) return; } else { Element newAttrElement = new Element(attr.getName().toString()); newAttrElement.setText(attr.getValue().toString()); compNode.addContent(newAttrElement); //addContent为添加节点,这里是将属性转化为节点 //System.out.println("当前公司添加的子节点为:"+compNode.getChild(attr.getName()).getName()); } } if (!(node.getTextTrim().equals(""))) { compNode.setText(node.getText()); //System.out.println("公司对象添加了"+node.getText()); } List <Element> nodeChildren = node.getChildren(); //获取子节点的list集合 int listsize = nodeChildren.size(); for (int i=0; i<listsize; i++) { Element child = (Element) nodeChildren.get(i).clone(); //注意这里一定是子节点的拷贝 if (node.getName().equals("purchaseOrders")) separate(child,compNode); else { //System.out.println("当前子节点为"+child.getName()); compNode.addContent(new Element(child.getName().toString())); int lastChild = compNode.getChildren().size(); Element nextCompNode = (Element) compNode.getChildren().get(lastChild-1); //getChildren函数是获取所有子节点,当前调用最后一个 separate(child,nextCompNode); } } } public static void main(String[] args) throws Exception { SAXBuilder builder = new SAXBuilder(false); Document doc = builder.build("/home/workspace/xmlWebservice-Project1/asset/ipo.xml"); Element node = doc.getRootElement(); //初始文件的读取 Element ABCRoot = new Element("purchaseOrders"); docABC.addContent(ABCRoot); ABCRoot.addContent(new Element("ABC_COMP"));//初始化设置ABC_COMP的开始元素 Element ABCNode = ABCRoot.getChild("ABC_COMP"); ABCNode.addContent(new Element("purchaseOrder")); ABCNode = ABCNode.getChild("purchaseOrder"); Element IBMRoot = new Element("purchaseOrders"); docIBM.addContent(IBMRoot); IBMRoot.addContent(new Element("IBM_COMP"));//初始化设置IBM_COMP的开始元素 Element IBMNode = IBMRoot.getChild("IBM_COMP"); IBMNode.addContent(new Element("purchaseOrder")); IBMNode = IBMNode.getChild("purchaseOrder"); compMode = "ABC"; separate(node, ABCNode); outputer(docABC,"/home/workspace/xmlWebservice-Project1/asset/ABC_COMP_jdom.xml"); //输出文件 compMode = "IBM"; separate(node, IBMNode); outputer(docIBM,"/home/workspace/xmlWebservice-Project1/asset/IBM_COMP_jdom.xml"); } }
原文:http://blog.csdn.net/u014237185/article/details/41651299