JDom操作XML

来源:互联网 发布:视频背景音乐提取软件 编辑:程序博客网 时间:2024/06/16 18:28
1.<!--注释-->
   声明:<?xml version="1.0" encoding="UTF-8" ?>    默认utf-8
   在DTD的个数限制中常使用的符号是:  *_ 、__+__和__?   。
 XML文档中语句<!DOCTYPE dlib SYSTEM “mydtd.dtd”>中SYSTEM的作用是   连接外部DTD文件 
 
2.标签:
   必须有根标签,每对标签应有开始标签和结束标签,大小写要一致
   当前标签内容为空的时候(<realname/>)   可以在开始标签之后加/
3.属性:可以给标签加属性,但是属性名称不能相同
   文本:如果要使用<和>  使用&lt  &gt    特殊符号,需要用特殊的标识符代替
4.xml的生成和解析
    dom :把整个xml文件读取到内存中                                       dom和sax一般不用
              优点:整体把握文档,可以任意修改文档的某些节点
              缺点:当xml文件非常大时,非常浪费内存
    sax:以流的方式一步步读取
              优点:比较节省内存
              缺点:不能整体把握文档,不可以对文档节点进行修改删除,一旦读过去不能再返回
   JDOM:比较常用   www.jdom.org
              融合dom和sax的优点

   JDOM生成xml:  引入lib/jdom-1.1.3.jar
       Element usersElement=new Element("users");
      Element personElement=new Element("person");
      Element pnameElement=new Element("pname");
      Element ppwdElement=new Element("pwd");
        
      usersElement.setAttribute("id","u007");//设置根节点属性
      personElement.setAttribute("id","p001");//设置第一个节点属性
      pnameElement.setText("zhangsan");
      ppwdElement.setText("123456");
        
      usersElement.addContent(personElement);//将子节点添加到根节点
      personElement.addContent(pnameElement);
      personElement.addContent(ppwdElement);
        
      Document document=new Document(usersElement); //建立Document关联
      Format format=Format.getPrettyFormat();
      format.setEncoding("utf-8");
      XMLOutputter xmlOutputter=new XMLOutputter(format); //以format形式输出
      try {
          xmlOutputter.output(document, new FileOutputStream("e:\\myuser.xml"));
          System.out.println("success");
      } catch (FileNotFoundException e) {
           e.printStackTrace();
      } catch (IOException e) {
           e.printStackTrace();
      }

-------------------------------------------------------------------------------------------------------------------------------------
jdom获取节点形式:jaxen-1.1.6.jar(报NoClassDefFoundError时使用)+jdom-1.1.3.jar
解析节点:http://www.open-open.com/lib/view/open1386045558392.html
  SAXBuilder sb = new SAXBuilder();
        Document doc = sb.build(new FileInputStream(filePath));
        Element root = doc.getRootElement(); //读取根节点
        //返回resume节点下的所有子节点
        XPath xPath = XPath.newInstance("/resume/*");
        //返回文档中所有preOccupation节点
        //XPath xPath = XPath.newInstance("//preOccupation");
        //返回/resume/wife/preOccupation这个节点
        //XPath xPath = XPath.newInstance("/resume/wife/preOccupation");
        //返回名字preOccupation有属性peroid的节点
        //XPath xPath = XPath.newInstance("/resume/wife/preOccupation[@period]");
        //返回文档中所有preOccupation节点,并且有属性period
        //XPath xPath = XPath.newInstance("//preOccupation[@period]");
        //返回名字为preOccupation属性period='5-18'的节点。
        //XPath xPath = XPath.newInstance("/resume/wife/preOccupation[@period='5-18']");
        //返回文档中所有preOccupation节点,并且文本内容为皇子
        //XPath xPath = XPath.newInstance("//preOccupation[text()='皇子']");
        List list = xPath.selectNodes(root);
        for (int i = 0; i < list.size(); i++) {
            Element e = (Element) list.get(i);
            System.out.println(e.getText());
        }
//        Element e2 = (Element) xPath.selectSingleNode(root);
//        System.out.println(e2.getText());
    }
0 0
原创粉丝点击