解析xml例子

一、首先在网上找了一段用jdom解析xml的代码,如下(使用工具为eclipse3.4):

DomTest.java

  1. package domtest;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import java.util.Iterator;
  5. import java.util.List;
  6. import org.jdom.Document;
  7. import org.jdom.Element;
  8. import org.jdom.JDOMException;
  9. import org.jdom.input.SAXBuilder;
  10. import org.jdom.output.XMLOutputter;
  11. public class DomTest {
  12.     public DomTest() {
  13.         String xmlpath = "src/book.xml"// 路径一定要注意(以你放的路径)
  14.         SAXBuilder builder = new SAXBuilder(false); // 使用JDOM首先要指定使用什么解析器,
  15.                                                     // 这表示使用的是默认的解析器
  16.         try {
  17.             Document doc = builder.build(xmlpath); // 得到Document
  18.             Element books = doc.getRootElement(); // 得到根元素
  19.             List booklist = books.getChildren("book"); // 得到元素(节点)的集合
  20.             for (Iterator iter = booklist.iterator(); iter.hasNext();) { // 循环节点集合
  21.                 Element book = (Element) iter.next();
  22.                 String email = book.getAttributeValue("email");// 取得元素book的属性名为“
  23.                                                                 // email”的属性值。
  24.                 System.out.println("邮箱===" + email);
  25.                 String name = book.getChildText("name");// 取得元素的子元素的值
  26.                 System.out.println("用户名===" + name);
  27.                 String price = book.getChildText("price");
  28.                 System.out.println("价格==" + price);
  29.                 book.getChild("name").setText("李三"); // 设置元素的值
  30.                 book.getAttribute("email").setValue("longyan@126.com"); // 设置元素属性的值
  31.             }
  32.             // 另外一程循环的的方法(解析xml文件后存入数据库的操作)
  33.             //XmlBean xbean = null;
  34.             //List list = new ArrayList();
  35.             //for(int i=0;i<booklist.size();i++){
  36.             //xbean = new XmlBean();
  37.             //Element book=(Element)booklist.get(i);
  38.             //xbean.setEmail(book.getAttributeValue("email"));
  39.             //xbean.setUsrename(book.getChildText("name"));
  40.             //xbean.setPrice(book.getChildText("price"));
  41.             //list.add(xbean);
  42.             //}
  43.             // 保存Document的修改到XML文件中
  44.             XMLOutputter outputter = new XMLOutputter();
  45.             outputter.output(doc, new FileOutputStream(xmlpath));
  46.             
  47.         }
  48.         catch (JDOMException e) {
  49.             e.printStackTrace();
  50.         }
  51.         catch (IOException e) {
  52.             e.printStackTrace();
  53.         }
  54.     }
  55.     public static void main(String[] args) {
  56.         new DomTest();// 测试上面所写的
  57.     }
  58. }

二、需要引入Jdom的jar包,先在网站上下载了jdom-1.1.zip,解压后将jdom-1.1/build/jdom.jar引入项目中。

三、建相应的book.xml文件,如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <books>
  3.     <book email="hello@live.com">
  4.         <name>Hello</name>
  5.         <price>100</price>
  6.     </book>
  7. </books>

eclipse运行结果输出,如下:

邮箱 ===hello@live.com
用户名===Hello
价格==100

book.xml运行后内容变为:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <books>
  3.     <book email="longyan@126.com">
  4.         <name>李三</name>
  5.         <price>100</price>
  6.     </book>
  7. </books>

四、被注释的代码现在还不清楚怎么运行,这里的XmlBean似乎是用的是Apache的一个开源项目,但是似乎没起什么作用,还在研究中

  1. //XmlBean xbean = null;
  2.             //List list = new ArrayList();
  3.             //for(int i=0;i<booklist.size();i++){
  4.             //xbean = new XmlBean();
  5.             //Element book=(Element)booklist.get(i);
  6.             //xbean.setEmail(book.getAttributeValue("email"));
  7.             //xbean.setUsrename(book.getChildText("name"));
  8.             //xbean.setPrice(book.getChildText("price"));
  9.             //list.add(xbean);
  10.             //}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值