??
<?xml version="1.0" encoding="UTF-8"?> <!--dom4j 示例用法--><?my dom4j demo?> <ACCOUNTS author="zhangwei" size="2"> <TAXACCOUNT> <NAME>*三</NAME> <USERID>2088123456789</USERID> <LOGINID>zha***@163.com</LOGINID> <IS_CERTIFY/> <MEMO><![CDATA[<TEST DOM4J>]]></MEMO> </TAXACCOUNT> <TAXACCOUNT> <NAME>*四</NAME> <USERID>2088123456790</USERID> <LOGINID>lis***@163.com</LOGINID> <MEMO><![CDATA[<TEST DOM4J>]]></MEMO> <IS_CERTIFY/> </TAXACCOUNT> </ACCOUNTS>?
/** * * @return */ private Document buildDocument() { /** * 写数据 */ Document doc = DocumentFactory.getInstance().createDocument(); // 设置XML的字符编码 doc.setXMLEncoding("UTF-8"); // 增加注释 doc.addComment("dom4j 示例用法"); doc.addProcessingInstruction("my dom4j", "demo"); // 增加节点元素 Element root = doc.addElement("ACCOUNTS"); //设置属性 root.addAttribute("author", "zhangwei"); root.addAttribute("size", "2"); // 增加子节点,并设置节点值 Element taxAccount = root.addElement("TAXACCOUNT"); taxAccount.addElement("NAME").setText("*三"); taxAccount.addElement("USERID").setText("2088123456789"); taxAccount.addElement("LOGINID").setText("zha***@163.com"); taxAccount.addElement("IS_CERTIFY"); // 设置一个<![CDATA[]]>数据 taxAccount.addElement("MEMO").addCDATA("<TEST DOM4J>"); Element second = root.addElement("TAXACCOUNT"); second.addElement("NAME").setText("*四"); second.addElement("USERID").setText("2088123456790"); second.addElement("LOGINID").setText("lis***@163.com"); // 设置一个<![CDATA[]]>数据 second.addElement("MEMO").addCDATA("<TEST DOM4J>"); second.addElement("IS_CERTIFY"); return doc; }?将documnet写入到文件中
private void writeXml2File(String fileName, Document document) { XMLWriter writer = null; try { OutputFormat format = OutputFormat.createPrettyPrint(); writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(fileName), "UTF-8"), format); writer.write(document); } catch (Exception e) { } finally { try { if (writer != null) { writer.close(); } } catch (Exception e) { } } }??使用访问者模式读取XML
@Test public void visitorXml() throws Exception { SAXReader reader = new SAXReader(); Document document = reader.read("D://dom4j.test.xml"); document.accept(new VisitorSupport() { /** * @see org.dom4j.VisitorSupport#visit(org.dom4j.Attribute) */ @Override public void visit(Attribute node) { System.out.println("attribute:" + node.getName() + ":" + node.getData()); } /** * @see org.dom4j.VisitorSupport#visit(org.dom4j.CDATA) */ @Override public void visit(CDATA node) { System.out.println("cdata " + node.getStringValue()); } /** * @see org.dom4j.VisitorSupport#visit(org.dom4j.Comment) */ @Override public void visit(Comment node) { System.out.println("comment:" + node.getStringValue()); } /** * @see org.dom4j.VisitorSupport#visit(org.dom4j.Document) */ @Override public void visit(Document document) { super.visit(document); } /** * @see org.dom4j.VisitorSupport#visit(org.dom4j.Element) */ @Override public void visit(Element node) { System.out.println("node " + node.getName() + ":" + node.getStringValue()); } /** * @see org.dom4j.VisitorSupport#visit(org.dom4j.Text) */ @Override public void visit(Text node) { System.out.println("text :" + node.getStringValue()); } }); }??标准JAVA迭代器模式读取XML
/** * 标准的JAVA迭代模式 * * @throws DocumentException */ @SuppressWarnings("unchecked") @Test public void readXml() throws DocumentException { /** * 读数据 */ SAXReader reader = new SAXReader(); Document document = reader.read("D://dom4j.test.xml"); // 获取根节点 Element root = document.getRootElement(); // 遍历根节点下的所有节点 for (Iterator<Element> it = root.elementIterator(); it.hasNext();) { Element e = it.next(); readElement(e); } } private void readElement(Element element) { @SuppressWarnings("unchecked") Iterator<Element> ieson = element.elementIterator(); if (ieson.hasNext()) { while (ieson.hasNext()) { Element elementSon = ieson.next(); readElement(elementSon); } } else { System.out.println("---read---" + element.getName() + ":" + element.getText()); } }?
原文:http://zhangwei-david.iteye.com/blog/2220780