• 2.1 使用JAXP 对 xml文档进行DOM解析


     1 //使用 jaxp 对xml文档进行dom解析
     2 public class Demo2 {
     3 
     4     //必要步骤
     5     @Test
     6     public void test() throws Exception {
     7         
     8     //1.创建工厂
     9         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    10                 
    11     //2.得到dom解析器
    12         DocumentBuilder db = dbf.newDocumentBuilder();
    13         
    14     //3.解析XML文档,得到代表文档的Document
    15         Document d = db.parse("src/com/xml/javaweb/book.xml");
    16         
    17         
    18     }
    19 }

    示例:对xml节点进行增删改查

      1 public class Demo3 {
      2     //读取XML文件中:<书名>javaWEB</书名>  节点中的值
      3     @Test
      4     public void read() throws Exception {
      5         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      6         DocumentBuilder db = dbf.newDocumentBuilder();
      7         Document d = db.parse("src/com/xml/javaweb/book.xml");    
      8         
      9         NodeList list = d.getElementsByTagName("书名");
     10         Node node = list.item(1);
     11         String text = node.getTextContent();
     12         System.out.println(text);
     13     }    
     14     
     15     //遍历整个XML文档中的节点
     16     @Test
     17     public void read1() throws Exception {
     18         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
     19         DocumentBuilder db = dbf.newDocumentBuilder();
     20         Document d = db.parse("src/com/xml/javaweb/book.xml");    
     21         
     22         //得到根节点
     23         NodeList list = d.getElementsByTagName("书架");
     24         Node root = list.item(0);
     25         list(root);
     26     }
     27 
     28     private void list(Node root) {
     29         if(root instanceof Element)
     30         System.out.println(root.getNodeName());
     31         
     32         NodeList nodelist = root.getChildNodes();
     33         for(int i = 0;i<nodelist.getLength();i++) {
     34             Node node = nodelist.item(i);
     35             list(node);
     36         }
     37     }
     38     
     39     //拿到 name 属性的值
     40     @Test
     41     public void read2() throws Exception {
     42         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
     43         DocumentBuilder db = dbf.newDocumentBuilder();
     44         Document d = db.parse("src/com/xml/javaweb/book.xml");    
     45         
     46         Element node = (Element) d.getElementsByTagName("书名").item(0);   //node 里面的方法不够用了  没有指定名字得到属性,在知道得到的是Element的情况下  ,把node强转一下
     47         String att = node.getAttribute("name");
     48         System.out.println(att);
     49     }
     50     
     51     //向XML文档中添加节点          <售价>39.00元</售价>
     52     @Test
     53     public void add() throws Exception {
     54         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
     55         DocumentBuilder db = dbf.newDocumentBuilder();
     56         Document d = db.parse("src/com/xml/javaweb/book.xml");    
     57         
     58         //创建节点
     59         Element element = d.createElement("售价");
     60         element.setTextContent("39.00元");
     61         
     62         //添加节点
     63         Node node = d.getElementsByTagName("书").item(0);
     64         node.appendChild(element);
     65         
     66         //更新XML文档
     67         TransformerFactory tff = TransformerFactory.newInstance();
     68         Transformer tf = tff.newTransformer();
     69         tf.transform(new DOMSource(d), new StreamResult(new FileOutputStream("src/com/xml/javaweb/book.xml")));
     70     }
     71     
     72     //向XML文档中指定位置添加节点          <售价>39.00元</售价>
     73     @Test
     74     public void add2() throws Exception {
     75         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
     76         DocumentBuilder db = dbf.newDocumentBuilder();
     77         Document d = db.parse("src/com/xml/javaweb/book.xml");    
     78         
     79         //创建节点
     80         Element element = d.createElement("售价");
     81         element.setTextContent("29.00元");
     82         
     83         //得到参考节点    
     84         Element ref = (Element) d.getElementsByTagName("售价").item(0);
     85         
     86         //添加节点
     87         Node node = d.getElementsByTagName("书").item(0);
     88         
     89         //往“书”节点的指定位置插入节点
     90         node.insertBefore(element, ref);    //在参考节点之前插入新建的节点
     91         
     92         //更新XML文档
     93         TransformerFactory tff = TransformerFactory.newInstance();
     94         Transformer tf = tff.newTransformer();
     95         tf.transform(new DOMSource(d), new StreamResult(new FileOutputStream("src/com/xml/javaweb/book.xml")));
     96     }
     97     
     98     
     99     //向XML文档节点上添加属性         <书名> java就业培训课程</书名> 上添加属性  name = "xxxx"
    100     @Test
    101     public void add3() throws Exception {
    102         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    103         DocumentBuilder db = dbf.newDocumentBuilder();
    104         Document d = db.parse("src/com/xml/javaweb/book.xml");    
    105         
    106         //得到要添加属性的节点
    107         Element element= (Element) d.getElementsByTagName("书名").item(0);
    108         //往指定节点添加属性    
    109         element.setAttribute("name", "xxxx");
    110         
    111         //更新XML文档
    112         TransformerFactory tff = TransformerFactory.newInstance();
    113         Transformer tf = tff.newTransformer();
    114         tf.transform(new DOMSource(d), new StreamResult(new FileOutputStream("src/com/xml/javaweb/book.xml")));
    115     }
    116     
    117     
    118     //删除XML文档节点          <售价>39.00元</售价>
    119     @Test
    120     public void delete() throws Exception {
    121         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    122         DocumentBuilder db = dbf.newDocumentBuilder();
    123         Document d = db.parse("src/com/xml/javaweb/book.xml");    
    124         
    125         //得到要删除的节点
    126         Element element= (Element) d.getElementsByTagName("售价").item(1);
    127         //得到要删除的节点的父节点  再调用父节点中删除子节点的方法
    128         element.getParentNode().removeChild(element);
    129         
    130         //更新XML文档
    131         TransformerFactory tff = TransformerFactory.newInstance();
    132         Transformer tf = tff.newTransformer();
    133         tf.transform(new DOMSource(d), new StreamResult(new FileOutputStream("src/com/xml/javaweb/book.xml")));
    134     }
    135     
    136     //更新XML文档节点      <作者>张孝祥</作者> -> <作者>xzk</作者>
    137     @Test
    138     public void update() throws Exception {
    139         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    140         DocumentBuilder db = dbf.newDocumentBuilder();
    141         Document d = db.parse("src/com/xml/javaweb/book.xml");    
    142         
    143         //得到要更新的节点
    144         Element element= (Element) d.getElementsByTagName("作者").item(1);
    145         //更新该节点的文本
    146         element.setTextContent("xzk");
    147         
    148         //更新XML文档
    149         TransformerFactory tff = TransformerFactory.newInstance();
    150         Transformer tf = tff.newTransformer();
    151         tf.transform(new DOMSource(d), new StreamResult(new FileOutputStream("src/com/xml/javaweb/book.xml")));
    152     }
    153 }
  • 相关阅读:
    Object有哪些方法?
    去除掉myeclipse2017页面右上角的图片
    报错 IllegalArgumentException occurred calling getter of cn.itcast.entity.Customer.cid
    如果在applicationContext.xml中没有配置bean的属性,那么也会导致空指针异常
    报错HTTP Status 500
    srm开发(基于ssh)(4)
    报错HTTP Status 500
    HTTP Status 500
    数字
    算法总结——树状数组
  • 原文地址:https://www.cnblogs.com/xuzekun/p/7356083.html
Copyright © 2020-2023  润新知