JDOM 实例

JDOM:http://www.jdom.org
一、读取文件
1、data.xml
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book>
<no>00001</no>
<name>DOM4J</name>
</book>
<book>
<no>00002</no>
<name>JDOM</name>
</book>
</books>


2、TestJDOM.java
import java.io.File;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
public class TestJDOM {
public static void main(String[] args) {
        try {
         File file = new File("data.xml");
   SAXBuilder builder = new SAXBuilder();
   Document document = builder.build(file);
   Element element = document.getRootElement();
   List list = element.getChildren();
   for(int i=0;i<list.size();i++){
    Element element2 = (Element)list.get(i);
    System.out.println("NO:"+element2.getChildText("no")+" NAME:"+element2.getChildText("name"));
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
}
}

你可能感兴趣的:(JDOM 实例)