jdom操作xml文件

来源:互联网 发布:system01.dbf数据大 编辑:程序博客网 时间:2024/05/16 05:11

下面通过一个简单的例子说明一下怎么用JDOM这一适合Java程序员习惯的工具包来解析XML文档。
为了简单,我用了如下XML作为要解析的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<books>
   <book email="zhoujunhui">
     <name>rjzjh</name>
     <price>60.0</price>
  </book>
</books>
够简单的吧,但它对于我们关心的东西都有了,子节点,属性。
下面是用于解析这个XML文件的Java文件:

1 public class JDomParse {2    public JDomParse(){3        String xmlpath="library.xml";4        SAXBuilder builder=new SAXBuilder(false);5        try {6            Document doc=builder.build(xmlpath);7            Element books=doc.getRootElement();8            List booklist=books.getChildren("book");9            for (Iterator iter = booklist.iterator(); iter.hasNext();) {10                Element book = (Element) iter.next();11                String email=book.getAttributeValue("email");12                System.out.println(email);13                String name=book.getChildTextTrim("name");14                System.out.println(name);15                book.getChild("name").setText("alterrjzjh");16                17            }18            19            XMLOutputter outputter=new XMLOutputter();20            outputter.output(doc,new FileOutputStream(xmlpath));21            22        } catch (JDOMException e) {23            e.printStackTrace();24        } catch (IOException e) {25            e.printStackTrace();26        }27    }28    public static void main(String[] args) {29        new JDomParse();30    }31}
不到30行代码,现在我对代码解释一下:
引用的类:
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
//下面是引用到JDOM中的类
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
(1)使用JDOM首先要指定使用什么解析器。如:
        SAXBuilder builder=new SAXBuilder(false); 这表示使用的是默认的解析器
(2)得到Document,我们以后要进行的所有操作都是对这个Document操作的:
        Document doc=builder.build(xmlpath);
(3)得到根元素:
        Element books=doc.getRootElement();
在JDOM中所有的节点(DOM中的概念)都是一个org.jdom.Element类,当然他的子节点也是一个org.jdom.Element类。
(4)得到元素(节点)的集合:
      List booklist=books.getChildren("book");
这表示得到“books”元素的所在名称为“book”的元素,并把这些元素都放到一个List集合中
(5)轮循List集合
     for (Iterator iter = booklist.iterator(); iter.hasNext();) {
       Element book = (Element) iter.next();
    }
    还有一种轮循方法是:
    for(int i=0;I       Element book=(Element)booklist.get(i);
    }
(6)取得元素的属性:
    String email=book.getAttributeValue("email");
   取得元素book的属性名为“email”的属性值。
(7)取得元素的子元素(为最低层元素)的值:
    String name=book.getChildTextTrim("name");
    注意的是,必须确定book元素的名为“name”的子元素只有一个。
(8)改变元素(为最低层元素)的值:
    book.getChild("name").setText("alterrjzjh");
    这只是对Document的修改,并没有在实际的XML文档中进行修改
(9)保存Document的修改到XML文件中:
   XMLOutputter outputter=new XMLOutputter();
    outputter.output(doc,new FileOutputStream(xmlpath));

我们先要有一个XMLOutputter类,再把已经修改了的Document保存进XML文档中。

到此。用JDOM解析和处理XML文档讲解完了,麻雀虽小,五脏俱全。现在已对JDOM有个整体上的概念了吧。

以上转自:http://liuwentao.iteye.com/blog/59978

相关文章:http://blog.csdn.net/harderxin/article/details/7285754


自己补充,除了以上方法,也可以通过XPath来提取元素

//先Document对象:InputStream inputStream = Config.class.getClassLoader().getResourceAsStream("com.czl.library.xml");SAXBuilder  sb = new SAXBuilder();Document  doc = sb.build(inputStream);//创建Document对象之后可以通过XPath来提取元素:String name =  null;List all = XPath.selectNodes(doc , "/books/book[1]/name");if (CollectionUtils.isNotEmpty(all)) {Object one = all.get(0);if (one  instanceof  Element) {name= ((Element) one).getTextTrim();} else if (one  instanceof  Attribute) {name= ((Attribute) one).getValue();} else if (one instanceof Text) {name= ((Text) one).getText();} }

原创粉丝点击