下载地址: http://www.dom4j.org/dom4j-1.6.1/ 点击download now就能够下载了。html
首先,在下载的文件中 docs/index.html中有详细的解释说明,能够参考学习。java
SAXReader saxReader = new SAXReader(); Document document = saxReader.read(new File("four.xml"));
// iterate through child elements of root for ( Iterator i = root.elementIterator(); i.hasNext(); ) { Element element = (Element) i.next(); // do something } // iterate through child elements of root with element name "foo" for ( Iterator i = root.elementIterator( "foo" ); i.hasNext(); ) { Element foo = (Element) i.next(); // do something } // iterate through attributes of root for ( Iterator i = root.attributeIterator(); i.hasNext(); ) { Attribute attribute = (Attribute) i.next(); // do something }
public void treeWalk(Element element) { for ( int i = 0, size = element.nodeCount(); i < size; i++ ) { Node node = element.node(i); if ( node instanceof Element ) { treeWalk( (Element) node ); } else { // do something.... } } }
1.读取XML文件,得到document对象 AXReader reader = new SAXReader(); Document document = reader.read(new File("csdn.xml")); 2.解析XML形式的文本,获得document对象. String text = "<csdn></csdn>"; Document document = DocumentHelper.parseText(text); 3.主动建立document对象. Document document = DocumentHelper.createDocument(); Element root = document.addElement("csdn");
String text = document.asXML();
第一种:简易格式:(不考虑编码,不考虑格式) XMLWriter xmlWriter = new XMLWriter(new FileWriter("four.xml"); xmlWriter.write(document); xmlWriter.close(); 第二种:精确格式:(考虑编码,考虑格式) OutputFormat of = OutputFormat.createPrettyPrint(); //OutputFormat of = new OutputFormat(); of.setEncoding("utf-8"); XMLWriter xmlWriter = new XMLWriter(new FileWriter("four.xml"), of); xmlWriter.write(document); xmlWriter.close();
注意:一般咱们设置了输出文件的字符格式,仍要主要java文件的字符格式是什么,若是二者不一致,依然会产生中文乱码错误。node
java文件的编码一般为GBK,调整在window->preference下:spring
1.获取文档的根节点. Element root = document.getRootElement(); 2.取得某个节点的子节点. Element element=node.element(“四大名著"); 3.取得节点的文字 String text=node.getText(); 4.取得某节点下全部名为“csdn”的子节点,并进行遍历. List nodes = rootElm.elements("csdn"); for (Iterator it = nodes.iterator(); it.hasNext();) { Element elm = (Element) it.next(); // do something } 5.对某节点下的全部子节点进行遍历. for(Iterator it=root.elementIterator();it.hasNext();){ Element element = (Element) it.next(); // do something } 6.在某节点下添加子节点 Element elm = newElm.addElement("朝代"); 7.设置节点文字. elm.setText("明朝"); 8.删除某节点.//childElement是待删除的节点,parentElement是其父节点 parentElement.remove(childElment); 9.添加一个CDATA节点.Element contentElm = infoElm.addElement("content");contentElm.addCDATA(“cdata区域”); 来自 <http://blog.csdn.net/redarmy_chen/article/details/12969219>
1.取得某节点下的某属性 Element root=document.getRootElement(); //属性名name Attribute attribute=root.attribute("id"); 2.取得属性的文字 String text=attribute.getText(); 3.删除某属性 Attribute attribute=root.attribute("size"); root.remove(attribute); 4.遍历某节点的全部属性 Element root=document.getRootElement(); for(Iterator it=root.attributeIterator();it.hasNext();){ Attribute attribute = (Attribute) it.next(); String text=attribute.getText(); System.out.println(text); } 5.设置某节点的属性和文字. newMemberElm.addAttribute("name", "sitinspring"); 6.设置属性的文字 Attribute attribute=root.attribute("name"); attribute.setText("csdn"); 来自 <http://blog.csdn.net/redarmy_chen/article/details/12969219>