zoukankan      html  css  js  c++  java
  • XML案例(使用JAXP进行DOM解析)

    1.book.xml

    <?xml version="1.0" encoding="UTF-8" standalone="no"?><书架>
    <书>
    <书名 name="sss">Java就业培训教程</书名>
    <作者>张孝祥</作者>

    <售价>109元</售价>
    <售价>59.00元</售价>
    <售价>1.00元</售价>
    </书>
    </书架>

    2.

    package cn.itcast.xml;

    import java.io.FileOutputStream;
    import java.util.List;

    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.stream.StreamResult;

    import org.junit.Test;
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;

    //使用dom方式对xml文档进行crud
    public class Demo2 {
    @Test
    public void read1() throws Exception {
    // 1.创建工厂
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    // 2.得到dom解析器
    DocumentBuilder builder = factory.newDocumentBuilder();

    // 3.解析xml文档,得到代表文档的document
    Document document = builder.parse("src/cn/itcast/xml/book.xml");

    NodeList list = document.getElementsByTagName("书名");
    Node node = list.item(0);
    String content = node.getTextContent();
    System.out.println(content);
    }

    // 得到xml文档中的所有标签
    @Test
    public void read2() throws Exception {
    // 1.创建工厂
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    // 2.得到dom解析器
    DocumentBuilder builder = factory.newDocumentBuilder();
    // 3.解析xml文档,得到代表文档的document
    Document document = builder.parse("src/cn/itcast/xml/book.xml");
    // 得到根节点
    Node root = document.getElementsByTagName("书架").item(0);
    list(root);
    }

    private void list(Node node) {
    if (node instanceof Element) {
    System.out.println(node.getNodeName());
    }
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
    Node child = list.item(i);
    list(child);
    }
    }

    // 得到xml文档中标签属性
    @Test
    public void read3() throws Exception {
    // 1.创建工厂
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    // 2.得到dom解析器
    DocumentBuilder builder = factory.newDocumentBuilder();
    // 3.解析xml文档,得到代表文档的document
    Document document = builder.parse("src/cn/itcast/xml/book.xml");
    // 得到根节点
    Element bookname = (Element) document.getElementsByTagName("书名")
    .item(0);
    String value = bookname.getAttribute("name");
    System.out.println(value);
    }

    // 向xml文档中添加节点:<售价>1.00</售价>
    @Test
    public void add() throws Exception {
    // 1.创建工厂
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    // 2.得到dom解析器
    DocumentBuilder builder = factory.newDocumentBuilder();
    // 3.解析xml文档,得到代表文档的document
    Document document = builder.parse("src/cn/itcast/xml/book.xml");

    // 创建节点
    Element price = document.createElement("售价");
    price.setTextContent("1.00元");

    // 把创建的节点挂到第一本书上
    Element book = (Element) document.getElementsByTagName("书").item(0);
    book.appendChild(price);

    // 把更新后内存写回到xml文档
    TransformerFactory tffactory = TransformerFactory.newInstance();
    Transformer tf = tffactory.newTransformer();
    tf.transform(new DOMSource(document), new StreamResult(
    new FileOutputStream("src/cn/itcast/xml/book.xml")));

    }

    // 向xml文档中指定位置上添加节点:<售价>20.00</售价>
    @Test
    public void add1() throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse("src/cn/itcast/xml/book.xml");

    // 创建节点
    Element price = document.createElement("售价");
    price.setTextContent("20.00元");

    // 得到参考节点
    Element refNode = (Element) document.getElementsByTagName("售价").item(1);

    // 得到要挂载的节点
    Element book = (Element) document.getElementsByTagName("书").item(0);
    // 往book节点的指定位置插入
    book.insertBefore(price, refNode);

    // 把更新后内存写回到xml文档
    TransformerFactory tffactory = TransformerFactory.newInstance();
    Transformer tf = tffactory.newTransformer();
    tf.transform(new DOMSource(document), new StreamResult(
    new FileOutputStream("src/cn/itcast/xml/book.xml")));

    }

    // 向xml文档节点上添加属性:<书名>Java就业培训教程</书名>上添加name="sss"
    @Test
    public void addAttr() throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse("src/cn/itcast/xml/book.xml");

    Element bookname = (Element) document.getElementsByTagName("书名")
    .item(0);
    bookname.setAttribute("name", "sss");

    // 把更新后内存写回到xml文档
    TransformerFactory tffactory = TransformerFactory.newInstance();
    Transformer tf = tffactory.newTransformer();
    tf.transform(new DOMSource(document), new StreamResult(
    new FileOutputStream("src/cn/itcast/xml/book.xml")));
    }

    //删除节点
    @Test
    public void delete() throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse("src/cn/itcast/xml/book.xml");

    // 得到要删除的节点
    Element e = (Element) document.getElementsByTagName("售价").item(0);
    //方法一:e.getParentNode().removeChild(e);

    //方法二:
    // 得到要删除节点的父节点
    Element book = (Element) document.getElementsByTagName("书").item(0);
    // 父节点删除子节点
    book.removeChild(e);

    // 把更新后内存写回到xml文档
    TransformerFactory tffactory = TransformerFactory.newInstance();
    Transformer tf = tffactory.newTransformer();
    tf.transform(new DOMSource(document), new StreamResult(
    new FileOutputStream("src/cn/itcast/xml/book.xml")));

    }

    //更新价格
    @Test
    public void update() throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse("src/cn/itcast/xml/book.xml");

    Element e = (Element) document.getElementsByTagName("售价").item(0);
    e.setTextContent("109元");

    // 把更新后内存写回到xml文档
    TransformerFactory tffactory = TransformerFactory.newInstance();
    Transformer tf = tffactory.newTransformer();
    tf.transform(new DOMSource(document), new StreamResult(
    new FileOutputStream("src/cn/itcast/xml/book.xml")));

    }
    }

  • 相关阅读:
    svn 提交时强制注释、不允许指定的文件类型的钩子写法
    开源跨平台网络库(lxnet)
    关于android studio的配置记录
    一般处理程序(ashx)对session处理摘要
    利用反射代替switch -转
    c#+jquery.autocomplete.js
    C#+ajaxupload实现图片上传
    几种常见SQL分页方式效率比较-转
    js页面跳转
    C#编码规范-转
  • 原文地址:https://www.cnblogs.com/xiaohuihui123/p/4359561.html
Copyright © 2011-2022 走看看