[转载]基于 JDOM 的 XML 操作

from:http://www.xjtuhx.com/bbs/dispbbs.asp?boardID=6&ID=32&page=1

基于 JDOM 的 XML 操作
创建XML
1)依靠的类:
org.jdom.Document;
org.jdom.Element;
org.jdom.output.XMLOutputter;
org.jdom.input.JDOMException;
java.io.IOException;

2)使用方法:

public void write(){

       //建立Document对象
  Document document=new Document();
       //建立Element对象
  Element root=new Element("beans");
       //建立根结点root
  document.setRootElement(root);
       //建立root下的结点,及值
       Element bean=new Element("bean");
  Element id=new Element("id");
  id.setText("1000");
  bean.addContent(id);
  Element name=new Element("name");
  name.setText("宝宝");
  bean.addContent(name);
  Element age=new Element("age");
  age.setText("26");
  bean.addContent(age);
        //获得JDOM默认格式
  Format format=Format.getPrettyFormat();
        //设置编码格式为”GB2312”
  format.setEncoding("GB2312");
  root.addContent(bean);
        //建立输出对象
  XMLOutputter out = new XMLOutputter();
        //使用输出格式”format”
  out.setFormat(format);
  try {
   out.output(document,System.out);
        //把document对象输出到d:/userdate.xml里
   out.output(document,new FileOutputStream("d:/userdate.xml"));
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}

解析XML
1)依靠的类:
org.jdom.input.SAXBuilder;
org.jdom.input.JDOMException;
java.io.ByteArrayInputStream;
java.io.FileInputStream;
java.io.IOException;

2)使用方法:

/**
* 从xml字符串生成Document对象。
*/
 public void StringTest(){
  String xml="<?xml version=\"1.0\" encoding=\"UTF-8\"?><beans><bean><id>1000</id><name>宝宝</name><age>26</age></bean></beans>";
  try {
   ByteArrayInputStream input=new       
          ByteArrayInputStream(xml.getBytes("utf-8"));
   try {
    Document document=new SAXBuilder().build(input);
    XMLOutputter out=new XMLOutputter();
    Format format=Format.getPrettyFormat();
    format.setEncoding("gb2312");
    out.setFormat(format);
    out.output(document,System.out);
   } catch (JDOMException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   }
  } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
  }
 }

/**
* 从文件生成Document对象。
*/
public void read(){
  SAXBuilder sb=new SAXBuilder();
  try {
   Document document=sb.build(new
           FileInputStream("d:/userdate.xml"));
   Element root=document.getRootElement();
   
   XMLOutputter out=new XMLOutputter();
   Format format=Format.getPrettyFormat();
   format.setEncoding("gb2312");
   out.setFormat(format);
   out.output(root.getChild("bean").getChild("name"),System.out);
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (JDOMException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  
 }

/**
 * 从资源文件生成Document对象。
 * @param sResourcePath String
 *   /comoohla/account/permission/permission.xml
 * @return Document
 */
public static Document getResourceDocument (String sResourcePath) {
  Document doc = null;
  try {
InputStream in =
new XMLUtils().getClass().getResourceAsStream(sResourcePath);
    doc = new SAXBuilder().build(in);
  }
  catch (JDOMException ex) {
  }
  return doc;
}

/**
 *
 */
public static void parse () {
  String sFilePath = "/comoohla/account/permission/permission.xml";
  Document doc = getResourceDocument(sFilePath);
  //! modules-permission
  Element root = doc.getRootElement();
  //! module
  java.util.List children = root.getChildren();
 
  /**
   * id, name, mask
   */
  for (int i=0; i<children.size(); i++) {
    Element module = (Element)children.get(i);
    int id = oohla.util.CharKit.getInt(module.getChildText("id"));
String name
 = oohla.util.CharKit.getString(module.getChildText("name"));
    int mask = oohla.util.CharKit.getInt(module.getChildText("mask"));
  }
}


浏览Element树的一些方法

Element root = doc.getRootElement();//获得根元素element

List allChildren = root.getChildren();// 获得所有子元素的一个list

List namedChildren = root.getChildren("name");//获得指定名称的子元素的list

Element child = root.getChild("name");//获得指定名称的第一个子元素


管理子元素的方法

List allChildren = root.getChildren();

allChildren.remove(3); // 删除第四个子元素

allChildren.removeAll(root.getChildren("jack"));// 删除叫“jack”的子//元素

root.removeChildren("jack"); // 便捷写法

allChildren.add(new Element("jane"));// 加入

root.addContent(new Element("jane")); // 便捷写法

allChildren.add(0, new Element("first"));

原文地址:https://www.cnblogs.com/hanxianlong/p/1312662.html