[java][代码]java操作XML代码

public static void main(String[] args) throws Exception {  
        //得到解析器  
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  
        DocumentBuilder builder = factory.newDocumentBuilder();  
        //通过解析器就可以得到代表整个内存中XML的Document对象  
        Document document = builder.parse("D:/1.xml");  
        System.out.println(document.toString());
        test1(document);  
    }  
//  1、得到某个具体的节点内容
    private static void test1(Document document){  
        NodeList nl = document.getElementsByTagName("shipper");  
        Node authorNode = nl.item(0); 
        test2(authorNode);
        System.out.println(authorNode.getTextContent());  
    }  
//  2、遍历所有元素节点:打印元素的名称  
    private static void test2(Node node){  
        //确定node的类型  
        //方式一  
//      if(node.getNodeType()==Node.ELEMENT_NODE){  
//          //是元素  
//      }  
        //方式二  
        if(node instanceof Element){  
            //是元素  
            Element e = (Element)node;  
            System.out.println("name:"+e.getNodeName());//打印元素名称  
            System.out.println( "value:"+e.getTextContent());//打印元素名称  

           
        }  
        //判断有没有子节点  
        NodeList nl = node.getChildNodes();  
        for(int i=0;i<nl.getLength();i++){  
            Node n = nl.item(i);  
            test2(n);  
        }  
    }  
//  3、修改某个元素节点的主体内容:<售价>39.00元</售价>--->10元  
    private static void test3(Document document) throws Exception{  
        //得到售价  
        Node priceNode = document.getElementsByTagName("售价").item(0);  
        priceNode.setTextContent("10元");  
        //更新XML文件  
        TransformerFactory tf = TransformerFactory.newInstance();  
        Transformer t = tf.newTransformer();  
        //构建输入源:  
        Source source = new DOMSource(document);  
        //构建目标:  
        Result result = new StreamResult("src/book.xml");  
          
        t.transform(source, result);  
    }  
      
//  4、向指定元素节点中增加子元素节点:第一本书添加子元素 <出版社>黑马程序员</出版社>  
    private static void test4(Document document) throws Exception{  
        //创建:<出版社>黑马程序员</出版社>  
        Element e = document.createElement("出版社");  
        e.setTextContent("黑马程序员");  
        //得到书,把新节点挂上去  
        Node bookNode = document.getElementsByTagName("书").item(0);  
        bookNode.appendChild(e);  
        //更新XML文件  
        TransformerFactory tf = TransformerFactory.newInstance();  
        Transformer t = tf.newTransformer();  
        //构建输入源:  
        Source source = new DOMSource(document);  
        //构建目标:  
        Result result = new StreamResult("src/book.xml");  
          
        t.transform(source, result);  
    }  
//  5、向指定元素节点上增加同级元素节点:第一本书<售价>前面添加<批发价>30</批发价>  
    private static void test5(Document document) throws Exception{  
        //创建新节点  
        Element e = document.createElement("批发价");  
        e.setTextContent("30元");  
        //找到<售价>  
        Node priceNode = document.getElementsByTagName("售价").item(0);  
        //父标签:调用insertBefore(新节点,参考节点);  
          
        Node bookNode = priceNode.getParentNode();  
        bookNode.insertBefore(e, priceNode);  
        //更新XML文件  
        TransformerFactory tf = TransformerFactory.newInstance();  
        Transformer t = tf.newTransformer();  
        //构建输入源:  
        Source source = new DOMSource(document);  
        //构建目标:  
        Result result = new StreamResult("src/book.xml");  
          
        t.transform(source, result);  
    }  
//  6、删除指定元素节点:删除批发价  
    private static void test6(Document document) throws Exception{  
        Node priceNode = document.getElementsByTagName("批发价").item(0);  
        priceNode.getParentNode().removeChild(priceNode);  
        //更新XML文件  
        TransformerFactory tf = TransformerFactory.newInstance();  
        Transformer t = tf.newTransformer();  
        //构建输入源:  
        Source source = new DOMSource(document);  
        //构建目标:  
        Result result = new StreamResult("src/book.xml");  
          
        t.transform(source, result);  
    }  
//  7、操作XML文件属性:书籍添加一个属性:ISBN=“ABC”  
    private static void test7(Document document) throws Exception{  
        Node bookNode = document.getElementsByTagName("书").item(0);  
        if(bookNode instanceof Element){  
            Element e = (Element)bookNode;  
            e.setAttribute("ISBN", "ABC");  
        }  
        //更新XML文件  
        TransformerFactory tf = TransformerFactory.newInstance();  
        Transformer t = tf.newTransformer();  
        //构建输入源:  
        Source source = new DOMSource(document);  
        //构建目标:  
        Result result = new StreamResult("src/book.xml");  
          
        t.transform(source, result);  
    }  
//  8、操作XML文件属性:获取ISBN=“ABC”  
    private static void test8(Document document) throws Exception{  
        Node bookNode = document.getElementsByTagName("书").item(0);  
        if(bookNode instanceof Element){  
            Element e = (Element)bookNode;  
            System.out.println(e.getAttribute("ISBN"));  
        }  
    }  

代码是一个Java程序,用于处理XML文件。它使用了DOM(Document Object Model)解析器来读取、修改和写入XML数据。以下是代码中每个方法的简要说明:

  1. main 方法:这是程序的入口点。它初始化一个XML解析器,解析指定路径的XML文件,并调用其他方法来演示不同的XML操作。

  2. test1 方法:获取名为 "shipper" 的第一个元素节点,并打印其文本内容。然后调用 test2 方法。

  3. test2 方法:递归遍历所有子节点,检查它们是否是元素节点,并打印元素的名称和文本内容。

  4. test3 方法:修改名为 "售价" 的第一个元素节点的文本内容,然后使用 Transformer 将修改后的文档写回文件。

  5. test4 方法:在名为 "书" 的第一个元素节点下添加一个新的子元素 "出版社",并设置其文本内容,然后使用 Transformer 更新文件。

  6. test5 方法:在名为 "售价" 的元素节点前插入一个新的同级元素 "批发价",然后使用 Transformer 更新文件。

  7. test6 方法:删除名为 "批发价" 的元素节点,然后使用 Transformer 更新文件。

  8. test7 方法:为名为 "书" 的第一个元素节点添加一个名为 "ISBN" 的属性,并设置其值为 "ABC",然后使用 Transformer 更新文件。

  9. test8 方法:获取名为 "书" 的第一个元素节点的 "ISBN" 属性值并打印。

请注意,这段代码中的文件路径(如 "D:/1.xml" 和 "src/book.xml")需要根据实际情况进行修改,以确保它们指向正确的XML文件。此外,代码中的异常处理是通过抛出异常来处理的,这意味着调用这些方法的代码需要处理或声明这些异常。

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

awonw

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值