SFD2008 in GuiZhou University Eddan

Tuesday May 26, 2009

JDOM是Java中最常用的解析XML文档的方法。创始人为Jason Hunter,其集成了DOM解析的简单易用和SAX解析的性能优越两大优点。

package com.jdom.parse;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.JDOMException;
import org.jdom.input.*;
import org.jdom.output.*;
import java.io.IOException;
import java.util.List;

/*1、安装包:在http://www.jdom.org下下载jdom的压缩文件,解压后,在其build子文件夹下可得到解析XML文档所需的jar包 jdom.jar
 * 把它放到该项目的Build Path下。Document doc=null;SAXBuilder sax=new SAXBuilder(false);doc=sax.build(fileName);
 * 2、JDOM可以将整个XML文档转换为一个document对象,相当于XML文档根节点,这与XSLT中的document()函数类似。
 * 3、利用上面得到的document节点的getRootElement()方法,得到XML文档的根元素。
 * 4、根元素提供了getChildren("person")方法获得子节点。
 * 5、用getAttribute(String attributeName)获得属性,getAttributeValue(String attributeName)获得属性的值。
 */
public class XMLParser {
 //
    public static void main(String args[]){
  //建立XMLParser类实例
  XMLParser parser=new XMLParser();
  //调用输出方法
  parser.getPeopleInformation(parser.getRoot("E:/project/JDOM/NewFile.xml"));
 }
 public Element getRoot(String fileName){
  Document doc=null;
  //创建解析对象
  SAXBuilder sax=new SAXBuilder(false);
  try {
   //解析XML文档,建立节点树,并返回根节点
   doc=sax.build(fileName);
  } catch (JDOMException e) {
   e.printStackTrace();
  } catch (IOException ex){
   ex.printStackTrace();
  }
  
  //获得根元素
  Element root=doc.getRootElement();
  return root;
 }
 
 public void getPeopleInformation(Element root){
  //获得根元素下所有名为“person”的子节点,这些节点被存储在List中
  List list=root.getChildren("person");
  //对List中的节点进行循环处理
  for(int i=0;i<list.size();i++){
   Element person=(Element)list.get(i);
   String name=person.getChild("name").getText();
   String id=person.getAttributeValue("id");
   String age=person.getChild("age").getText();
   String dept=person.getChild("dept").getText();
   System.out.print("person:"+name);
   System.out.print(" id:"+id);
   System.out.print(" age:"+age);
   System.out.print(" dept:"+dept);
   System.out.println();
  }
  
 } 
 
}

原XML为:

<?xml version="1.0" encoding="UTF-8"?>
<people>
  <person id="001">
    <name>Jack</name>
    <age>25</age>
    <dept>Development</dept>
  </person>
  <person id="002">
    <name>Alex</name>
    <age>22</age>
    <dept>Management</dept>
  </person>
  <person id="003">
    <name>Martin</name>
    <age>25</age>
    <dept>Development</dept>
  </person>
  <person id="004">
    <name>Mike</name>
    <age>22</age>
    <dept>testing</dept>
  </person>
  <person id="005">
    <name>John</name>
    <age>23</age>
    <dept>Management</dept>
  </person>
  <person id="006">
    <name>Lina</name>
    <age>22</age>
    <dept>testing</dept>
  </person>
</people>

运行结果:

person:Jack id:001 age:25 dept:Development
person:Alex id:002 age:22 dept:Management
person:Martin id:003 age:25 dept:Development
person:Mike id:004 age:22 dept:testing
person:John id:005 age:23 dept:Management
person:Lina id:006 age:22 dept:testing

 

Reference:《XML数据标记、处理、共享与分析开发典型应用》  张朝明等编著  电子工业出版社

一下两篇日志均引用此书

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed