zoukankan      html  css  js  c++  java
  • XML

    package cn.zzsxt.demo;

    import java.io.FileInputStream;

    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;

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

    /**
    * 将xml文件转换成一个org.w3c.dom.Document对象
    * Document:文档树
    * Node:节点(Element元素,Attribute属性,Text文本..)
    * getNodeType():获取Node节点的类型
    *
    * NodeList:节点列表
    * getLength():获取节点列表的个数
    * item(int i):在节点列表中根据下标获取该下标对应的节点
    *
    * Element:元素
    * NodeList getChildNodes():获取当前元素的子节点列表
    * getAttribute(String attrName):获取指定属性名称对应的属性值
    * getTextContent():获取该元素中的文本
    *
    * 使用DOM解析xml文件的步骤:
    * 1.创建解析器工厂对象
    * DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    * 2.由 解析器工厂对象创建解析器对象
    * DocumentBuilder db = dbf.newDocumentBuilder();
    * 3.由解析器对象对指定XML文件进行解析,构建相应DOM树,创建Document对象
    * Document document = db.parse(new FileInputStream("product.xml"));
    * 4.以Document对象为起点对DOM树的节点进行增删改查操作。
    *
    */
    public class TestDOM {
    public static void main(String[] args) throws Exception{
    //1.创建一个解析器工厂对象
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    //2.由 解析器工厂对象创建解析器对象
    DocumentBuilder db = dbf.newDocumentBuilder();
    //3.由解析器对象对指定XML文件进行解析,构建相应DOM树,创建Document对象
    Document document = db.parse(new FileInputStream("product.xml"));
    //4.获取根节点:products
    Element productsElement = document.getDocumentElement();
    System.out.println(productsElement.getTagName());
    //获取根节点的子节点: product
    NodeList productNodeList = productsElement.getChildNodes();
    for (int i = 0; i < productNodeList.getLength(); i++) {
    Node node = productNodeList.item(i);//product #text
    if(node.getNodeType()==Node.ELEMENT_NODE){//判断该节点是否为元素
    // System.out.println(node.getNodeName());
    Element productElement = (Element)node;
    String id = productElement.getAttribute("id");//通过属性名称获取属性的值
    System.out.println("id="+id);
    //获取product节点的子节点
    NodeList productChildNodeList =productElement.getChildNodes();
    for (int j = 0; j < productChildNodeList.getLength(); j++) {
    Node childNode = productChildNodeList.item(j);
    if(childNode.getNodeType()==Node.ELEMENT_NODE){//判断该节点是否为元素 Node.ELEMENT_NODE值为1
    Element childElement = (Element)childNode;//name,price,color....
    String text = childElement.getTextContent();//获取文本节点的值
    System.out.println(" "+childElement.getTagName()+"="+text);
    }
    }
    }
    }
    }
    }

    从src下将book.xml文件转换成一个org.w3c.dom.Document对象

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    // Document document = db.parse("src/book.xml");//如果文件的路径不正确将抛出FileNotFoundException异常
    //getResourceAsStream("/book.xml")--->将类路径根目录下的book.xml转换成输入流
    InputStream ips = TestDOM2.class.getResourceAsStream("/book.xml");
    Document document = db.parse(ips);//如果文件的路径不正确将抛出FileNotFoundException异常
    Element booksElement = document.getDocumentElement();
    NodeList bookNodeList = booksElement.getChildNodes();

    package cn.zzsxt.dom4j;

    import java.io.File;
    import java.util.Iterator;

    import org.dom4j.Document;
    import org.dom4j.Element;
    import org.dom4j.io.SAXReader;

    /**
    *dom4j:第三方的解析和写入xml的专有类库
    *步骤:
    *1. 将dom4j类库(dom4j-1.6.1.jar)添加到项目中.
    * 新建一个lib/libs目录,将类库复制到该目录,右键-->add to build path
    *2.使用dom4j的工具加载xml文件,并将xml文件转换成一个Document对象
    *3.对Document对象进行操作(获取子元素,属性,文本等内容)
    *
    */
    public class TestDom4j {
    public static void main(String[] args) throws Exception{
    //1.创建SAXReader对象
    SAXReader saxReader = new SAXReader();
    //2.利用SAXReader的read方法将文件转换为一个Document对象
    Document document = saxReader.read(new File("product.xml"));
    //3.获取Document对象中的元素,元素的属性,文本等信息
    Element root = document.getRootElement();//获取文档的根节点 products
    Iterator<Element> productIter = root.elementIterator();//获取子元素的迭代器
    while(productIter.hasNext()){
    Element productElement = productIter.next();//获取product元素
    String id = productElement.attributeValue("id");//通过属性名称获取该属性的值
    System.out.println("id="+id);
    Iterator<Element> childIter = productElement.elementIterator();//获取product元素的迭代器
    while(childIter.hasNext()){
    Element childElement = childIter.next();//获取product元素的子元素(name,price....)
    String name = childElement.getName();//获取元素名称
    String text = childElement.getText();//获取文本节点信息
    System.out.println(" "+name+"="+text);
    }
    }
    }
    }

  • 相关阅读:
    【leetcode】1020. Partition Array Into Three Parts With Equal Sum
    【leetcode】572. Subtree of Another Tree
    【leetcode】123. Best Time to Buy and Sell Stock III
    【leetcode】309. Best Time to Buy and Sell Stock with Cooldown
    【leetcode】714. Best Time to Buy and Sell Stock with Transaction Fee
    【leetcode】467. Unique Substrings in Wraparound String
    【leetcode】823. Binary Trees With Factors
    【leetcode】143. Reorder List
    【leetcode】1014. Capacity To Ship Packages Within D Days
    【leetcode】1013. Pairs of Songs With Total Durations Divisible by 60
  • 原文地址:https://www.cnblogs.com/seePoppy/p/6879972.html
Copyright © 2011-2022 走看看