JDom 与SAX DOM相比的优势
1. SAX中没有Document概念
2. DOM对与Java程序员来说语法不习惯。
3. Jdom并不是以DOM为基础开发的
下面写几个例子来对JDom进行介绍:
1. 利用JDom生成XML信息
2. 生成的XML文件
3. 读取XML信息
3.1 xml信息
3.2 java代码
1. SAX中没有Document概念
2. DOM对与Java程序员来说语法不习惯。
3. Jdom并不是以DOM为基础开发的
下面写几个例子来对JDom进行介绍:
1. 利用JDom生成XML信息
- package com.verran.jdom;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.List;
- import org.jdom.*;
- import org.jdom.output.Format;
- import org.jdom.output.XMLOutputter;
- public class XMLCreate {
- Element root=new Element("Student");
- Document document=new Document(root);
- public XMLCreate(){
- String [] name ={"VerRan","LHT","XXX"};
- //document.addContent(root);
- for(int i=0;i
- Element nameElement=new Element("name");
- nameElement.addContent(name[i]);
- nameElement.setAttribute("sort",new Integer(i).toString());
- Element sno=new Element("sno");
- sno.addContent(new Integer(i).toString());
- root.addContent(nameElement);
- root.addContent(sno);
- }
- deleteElement("sno");
- OutPutXML(document,"e:/myJDOM.xml");
- }
- private void OutPutXML(Document document, String fileName) {
- // TODO Auto-generated method stub
- XMLOutputter xmlout=new XMLOutputter();
- try {
- Format fm=xmlout.getFormat();
- fm.setEncoding("big5");
- fm.setLineSeparator("/n");
- //xmlout.setFormat()
- FileWriter fw=new FileWriter(fileName);
- xmlout.output(document, fw);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- private void deleteElement(String name){
- List list= root.getChildren(name);
- list.remove(1);
- }
- public static void main(String[] args){
- XMLCreate xmlcreate=new XMLCreate();
- }
- }
2. 生成的XML文件
xml 代码
- xml version="1.0" encoding="UTF-8"?>
- <Student><name sort="0">VerRanname><sno>0sno><name sort="1">LHTname><name sort="2">XXXname><sno>2sno>Student>
3. 读取XML信息
3.1 xml信息
- xml version="1.0" encoding="UTF-8"?>
- <HD>
- <disk name="C">
- <capacity>8Gcapacity>
- <directories>200directories>
- <files>1580files>
- disk>
- <disk name="D">
- <capacity>10Gcapacity>
- <directories>500directories>
- <files>3000files>
- disk>
- HD>
java 代码
- package com.verran.jdom;
- import java.io.IOException;
- import java.util.List;
- import org.jdom.Document;
- import org.jdom.Element;
- import org.jdom.JDOMException;
- import org.jdom.input.SAXBuilder;
- public class JdomDemo {
- public static void main(String args[]){
- //采用默认的SAXBuilder
- //也可以采用DOMBuilder来将由DOM创建的Document转换成JDOM的Document
- SAXBuilder saxBuilder=new SAXBuilder();
- try {
- Document doc=saxBuilder.build("test.xml");
- Element root=doc.getRootElement();
- List children=root.getChildren("disk");
- for(int i=0;i
- Element element=(Element)children.get(i);
- String name=element.getAttributeValue("name");
- String capacity=element.getChildText("capacity");
- String directories=element.getChildText("directories");
- String files=element.getChildText("files");
- System.out.println("磁盘信息:");
- System.out.println("分区盘符:"+name);
- System.out.println("分区容量:"+capacity);
- System.out.println("目录数:"+directories);
- System.out.println("文件数:"+files);
- System.out.println("-----------------------------------");
- }
- } catch (JDOMException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
3.3
java 代码
- package com.pub.util;
- import java.io.File;
- import java.io.IOException;
- import java.util.List;
- import org.jdom.Attribute;
- import org.jdom.Document;
- import org.jdom.Element;
- import org.jdom.JDOMException;
- import org.jdom.input.SAXBuilder;
- public class PaserXML {
- /**
- * @param args
- */
- org.jdom.input.SAXBuilder builder=new SAXBuilder();
- //根据Jdom函数解析XML文档
- public void paserByJdom(){
- try {
- Document document=builder.build(new File("c:/myJDOM1.xml"));
- Element root=document.getRootElement();
- System.out.println(root.getName());
- List list=root.getChildren();
- System.out.println(list.size());
- for(int i=0;i<list.size();i++){
- Element element=(Element)list.get(i);
- System.out.println("++++++++++++++++++++++++++++++");
- System.out.println("第I个"+"element.getName()"+element.getName());
- List attr=element.getAttributes();
- //System.out.println(attr.size());
- for(int j=0;j<attr.size();j++){
- Attribute attribute=(Attribute)attr.get(j);
- System.out.println("第"+i+"个节点的"+"第"+j+"个属性是名"+attribute.getName());
- System.out.println("第"+i+"个节点的"+"第"+j+"个属性是值"+attribute.getValue());
- }
- System.out.println("++++++++++++++++++++++++++++++");
- }
- } catch (JDOMException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- //根据XPATH解析XML
- public void parserByXpath(){
- try {
- Document document=builder.build(new File("c:/myJDOM1.xml"));
- Element root=document.getRootElement();
- System.out.println(root.getName());
- List list=org.jdom.xpath.XPath.selectNodes(root,"/root/order/order-id");
- System.out.println(list.size());
- for(int i=0;i<list.size();i++){
- Element element=(Element)list.get(i);
- System.out.println("element.getName()"+element.getName());
- System.out.println("element.getValue()"+element.getValue());
- //org.jdom.xpath.XPath.selectSingleNode(element, "//order-id ");
- }
- } catch (JDOMException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- PaserXML p=new PaserXML();
- p.parserByXpath();
- }
- }
附录:
http://www.jdom.org/docs/apidocs/