jdom操作xml文件

下面通过一个简单的例子说明一下怎么用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();
	} 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值