完整版jdom解析xml文件

来源:互联网 发布:怎么查公司的海关数据 编辑:程序博客网 时间:2024/05/22 16:04

转自:http://263229365.iteye.com/blog/1198425

前提条件:需要jdom,dom4j以及jaxen jar包

Jaxen是一个Java编写的开源的XPath库。这是适应多种不同的对象模型,包括DOM,XOM,dom4j和JDOM。也可以作为适配器,转换Java字节代码或XML的Java bean为xml,从而使您可以使用XPath查询这些树了。

一、 解析XML文件

import java.io.File;import java.io.IOException;  import java.util.Iterator;  import java.util.List;    import org.jdom.input.SAXBuilder;  import org.jdom.xpath.XPath;  import org.jdom.Document;  import org.jdom.Element;  import org.jdom.JDOMException;public class ReadXML {  /**      * @param args      */      public static void main(String[] args) {        try {         SAXBuilder sax = new SAXBuilder(false);         Document doc = sax.build(new File("D:\\xmlTtest\\test.xml"));              Element rootEle = doc.getRootElement();              Element driverClassNameElement = (Element)XPath.selectSingleNode(rootEle, "//sys-config/jdbc-info/driver-class-name");              String driverClassName = driverClassNameElement.getText();              System.out.println("driverClassName = " + driverClassName);                            List provinceList = XPath.selectNodes(rootEle, "//sys-config/provinces-info/province");              for(Iterator it = provinceList.iterator();it.hasNext();){                  Element provinceEle = (Element)it.next();                  String proId = provinceEle.getAttributeValue("id");                  String proName = provinceEle.getAttributeValue("name");                    System.out.println("provinceId = " + proId + "   provinceName = " + proName);                                    List cityEleList = (List)provinceEle.getChildren("city");                                    for(Iterator cityIt = cityEleList.iterator();cityIt.hasNext();){                      Element cityEle = (Element)cityIt.next();                      String cityId = cityEle.getAttributeValue("id");                      String cityName = cityEle.getText();                        System.out.println("    cityId = " + cityId + "   cityName = " + cityName);                  }              }          } catch (JDOMException e) {              // TODO 自动生成 catch 块              e.printStackTrace();          } catch (IOException e) {              // TODO 自动生成 catch 块              e.printStackTrace();          }        }    }  

首先,用 org.jdom.input.SAXBuilder 这个类取得要操作的xml文件,会返回一个 org.jdom.Document 对象,这里需要做一下异常处理。然后,取得这个xml文件的根节点,org.jdom.Element 代表xml文件中的一个节点,取得跟节点后,便可以读取xml文件中的信息。利用 org.jdom.xpath.XPath 可以取得xml中的任意制定的节点中的信息。

    例如,要取得上面文件中的 <jdbc-info> 下的 <driver-class-name> 中的内容,先取得这个节点Element driverClassNameElement = (Element)XPath.selectSingleNode(rootEle, "//sys-config/jdbc-info/driver-class-name"),注意,根节点前要使用两个 "/" ,然后,用 driverClassNameElement.getText() 便可以取得这个节点下的信息。

    如果一个节点下有多个名称相同的子节点,可以用XPath.selectNodes()方法取得多个子节点的List,遍历这个List就可以操作各个子节点的内容了。


二、 写入XML

import java.io.FileNotFoundException;  import java.io.FileOutputStream;  import java.io.IOException;    import org.jdom.Document;  import org.jdom.Element;  import org.jdom.output.XMLOutputter;  public class WriteXML {  public static void main(String[] args) {          // TODO 自动生成方法存根          Element rootEle = new Element("sys-config");          Element provincesEle = new Element("provinces-info");                    Element provinceEle = new Element("province");          provinceEle.setAttribute("id","hlj");          provinceEle.setAttribute("name","黑龙江省");                    Element cityEle1 = new Element("city");          cityEle1.setAttribute("id","harb");          cityEle1.addContent("哈尔滨");                    Element cityEle2 = new Element("city");          cityEle2.setAttribute("id","nj");          cityEle2.addContent("嫩江");                              provinceEle.addContent(cityEle1);          provinceEle.addContent(cityEle2);          provincesEle.addContent(provinceEle);          rootEle.addContent(provincesEle);                    Document doc = new Document(rootEle);                    XMLOutputter out = new XMLOutputter();                      //      out.setFormat(Format.getCompactFormat().setEncoding("GBK"));//设置文件编码,默认为UTF-8          String xmlStr = out.outputString(doc);          System.out.println(xmlStr);                    try {              out.output(doc, new FileOutputStream("D:/test111.xml"));          } catch (FileNotFoundException e) {              // TODO 自动生成 catch 块              e.printStackTrace();          } catch (IOException e) {              // TODO 自动生成 catch 块              e.printStackTrace();          }                }    }  




0 0
原创粉丝点击