XML 读写-JDOM和DOM4j

一、JDOM的XML读写

1.JDOM的XML 读

 1 import java.io.File;
 2 import java.io.IOException;
 3 import java.util.Iterator;
 4 import java.util.List;
 5 import org.jdom.Document;
 6 import org.jdom.Element;
 7 import org.jdom.JDOMException;
 8 import org.jdom.input.SAXBuilder;
 9 public class JdomReadXml {
10  /**
11   * @param args
12   */
13  public static void main(String[] args) {
14   /**
15    * <?xml version="1.0" encoding="UTF-8"?> <actions m="001"><action
16    * path="/test" class="com.mystruts.demo.LoginAction"><forward
17    * name="success" url="test.jsp" /><forward name="failure"
18    * url="failure.jsp" /></action><action path="/user"
19    * class="com.mystruts.demo.UserAction"><forward name="success"
20    * url="test.jsp" /><forward name="failure" url="failure.jsp" /></action></actions>
21    */
22   SAXBuilder sax = new SAXBuilder();
23   Document doc;
24   try {
25    try {
26     doc = sax.build(new File("mystruts.xml"));
27     Element root = doc.getRootElement();
28     List actions = root.getChildren();
29     // 遍历获取根节点下的一级子节点,并作为入参传入递归方法
30     for (Iterator i = actions.iterator(); i.hasNext();) {
31      Element action = (Element) i.next();
32      System.out.println(action.getAttributeValue("path"));
33      System.out.println(action.getAttributeValue("class"));
34      List forwards = action.getChildren();
35      for (Iterator j = forwards.iterator(); j.hasNext();) {
36       Element forward = (Element) j.next();
37       System.out.println(forward.getAttributeValue("name"));
38       System.out.println(forward.getAttributeValue("url"));
39      }
40     }
41    } catch (IOException e) {
42     // TODO Auto-generated catch block
43     e.printStackTrace();
44    }
45   } catch (JDOMException e) {
46    e.printStackTrace();
47   }
48  }
49 }

2.1.JDOM的XML 写

import java.io.File;
import java.io.FileWriter;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
public class JdomWriteXml {
 /**
  * @param args
  */
 public static void main(String[] args) {
  SAXBuilder sb = new SAXBuilder();
  Element actions = new Element("actions");
  Document document = new Document(actions);
  Element action1 = new Element("action");
  actions.addContent(action1);
  Attribute path_atbt1 = new Attribute("path", "/test");
  Attribute class_atbt1 = new Attribute("class",
    "com.mystruts.demo.LoginAction");
  action1.setAttribute(path_atbt1);
  action1.setAttribute(class_atbt1);
  Element action1_forward1 = new Element("forward");
  action1.addContent(action1_forward1);
  Attribute action1_forward1_name_atbt1 = new Attribute("name", "success");
  Attribute action1_forward1_url_atbt1 = new Attribute("url", "test.jsp");
  action1_forward1.setAttribute(action1_forward1_name_atbt1);
  action1_forward1.setAttribute(action1_forward1_url_atbt1);
  Element action1_forward2 = new Element("forward");
  action1.addContent(action1_forward2);
  Attribute action1_forward1_name_atbt2 = new Attribute("name", "failure");
  Attribute action1_forward1_url_atbt2 = new Attribute("url",
    "failure.jsp");
action1_forward2.setAttribute(action1_forward1_name_atbt2); action1_forward2.setAttribute(action1_forward1_url_atbt2); Element action2
= new Element("action"); actions.addContent(action2); Attribute path_atbt2 = new Attribute("path", "/user"); Attribute class_atbt2 = new Attribute("class", "com.mystruts.demo.UserAction"); action2.setAttribute(path_atbt2); action2.setAttribute(class_atbt2); Element action2_forward1 = new Element("forward"); action2.addContent(action2_forward1); Attribute action2_forward1_name_atbt1 = new Attribute("name", "success"); Attribute action2_forward1_url_atbt1 = new Attribute("url", "test.jsp"); action2_forward1.setAttribute(action2_forward1_name_atbt1); action2_forward1.setAttribute(action2_forward1_url_atbt1); Element action2_forward2 = new Element("forward"); action2.addContent(action2_forward2); Attribute action2_forward1_name_atbt2 = new Attribute("name", "failure"); Attribute action2_forward1_url_atbt2 = new Attribute("url", "failure.jsp"); action2_forward2.setAttribute(action2_forward1_name_atbt2); action2_forward2.setAttribute(action2_forward1_url_atbt2); Attribute root_atbt1 = new Attribute("m", "001"); actions.setAttribute(root_atbt1); try { File f1 = new File("mystruts.xml"); // XMLOutputter xo=new XMLOutputter(" ",true,"GB2312"); XMLOutputter xo = new XMLOutputter(); FileWriter fw = new FileWriter(f1); xo.output(document, fw); fw.close(); } catch (Exception e) { e.printStackTrace(); } // System.out.println(document.toString()); } }

二DOM4j读写Xml

1.DOM4j读Xml

import java.io.File;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Dm4jReadXml {
 /**
  * @param args
  */
 public static void main(String[] args) {
  // dom4j读xml,解析xml
  /**
   * <?xml version="1.0" encoding="UTF-8"?> <actions><action path="/test"
   * class="com.mystruts.demo.LoginAction"><forward name="success"
   * url="hello.jsp"/><forward name="fail" url="fail.jsp"/></action><action
   * path="/user" class="com.mystruts.demo.UserAction"><forward
   * name="success" url="list.jsp"/><forward name="fail" url="fail.jsp"/></action></actions>
   */
  SAXReader saxReader = new SAXReader();
  try {
   Document document = saxReader.read(new File("d://mystruts.xml"));
   Element root = document.getRootElement();
   List actions = root.elements("action");
   for (int i = 0; i < actions.size(); i++) {
    Element action = (Element) actions.get(i);
    System.out.println("action.path=="
      + action.attributeValue("path"));
    System.out.println("action.class=="
      + action.attributeValue("class"));
    List forwards = action.elements("forward");
    for (int j = 0; j < forwards.size(); j++) {
     Element forward = (Element) forwards.get(j);
     System.out.println("forward.name=="
       + forward.attributeValue("name"));
     System.out.println("forward.url=="
       + forward.attributeValue("url"));
    }
   }
  } catch (DocumentException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}

2.DOM4j写Xml

import java.io.File;
import java.io.FileWriter;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;
public class DOM4j {
 public static void main(String[] args) {
  // dom4j解析xml测试
  // 1. dom4j写xml
  /*
   * <?xml version="1.0" encoding="UTF-8"?> <actions> <action path="/test"
   * class="com.mystruts.demo.LoginAction"> <forward name="success"
   * url="hello.jsp"/> <forward name="fail" url="fail.jsp"/> </action>
   * <action path="/user" class="com.mystruts.demo.UserAction"> <forward
   * name="success" url="list.jsp"/> <forward name="fail" url="fail.jsp"/>
   * </action> </actions>
   */
  Document document = DocumentHelper.createDocument();
  Element actionsElement = document.addElement("actions");
  Element actionElement1 = actionsElement.addElement("action");
  actionElement1.addAttribute("path", "/test");
  actionElement1.addAttribute("class", "com.mystruts.demo.LoginAction");
  Element forwardElement1 = actionElement1.addElement("forward");
  forwardElement1.addAttribute("name", "success");
  forwardElement1.addAttribute("url", "hello.jsp");
  Element forwardElement2 = actionElement1.addElement("forward");
  forwardElement2.addAttribute("name", "fail");
  forwardElement2.addAttribute("url", "fail.jsp");
  Element actionElement2 = actionsElement.addElement("action");
  actionElement2.addAttribute("path", "/user");
  actionElement2.addAttribute("class", "com.mystruts.demo.UserAction");
  Element forwardElement21 = actionElement2.addElement("forward");
  forwardElement21.addAttribute("name", "success");
  forwardElement21.addAttribute("url", "list.jsp");
  Element forwardElement22 = actionElement2.addElement("forward");
  forwardElement22.addAttribute("name", "fail");
  forwardElement22.addAttribute("url", "fail.jsp");
  try {
   /** 将document中的内容写入文件中 */
   XMLWriter writer = new XMLWriter(new FileWriter(new File(
     "d://mystruts.xml")));
   writer.write(document);
   writer.close();
   /** 执行成功,需返回1 */
  } catch (Exception ex) {
   ex.printStackTrace();
  }
  System.out.println(document.asXML());
 }
}
原文地址:https://www.cnblogs.com/harry335/p/3939275.html