zoukankan      html  css  js  c++  java
  • How to parse Xml file -- DOM!

    We can see that when we want to use the Dom4j to parse XML , we should import org.dom4j.*

    At the same time ,that to parse XML by DOM ,we should import org.w3c.dom.*

    The conclusion is that DOM and Dom4j is different and located in different jar.The DOM belongs to w3c and the Dom4j belongs to dom4j.

    The DOM parser will load the whole xml file and create a DOM tree, it will cost much space of memoty and lead to memory leak.

    So the DOM parser will be easily to modify the xml file.

    The SAX parser will load the single node and cost little space of memory, while it's not so easy to modify the xml file.

    The DOM parser belongs to JAXP(Java API for XML processing), and it is part of JavaSE includes the following parts:

      org.w3c.dom: provide the DOM parser to parse XML standard interface

      org.xml.sax: provide the SAX parser to parse XML standard interface

      javax.xml: provide the class of parsing XML file

    But how do we parse XML by DOM?

      //1.get the DocumentBuilderFactory by its static method

      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

      //2.get the DocumentBuilder by DocumentBuilderFactory's newDocumentBuilder() method

      DocumentBuilder builder = factory.newDocumentBuilder();

      //3.get the Document object by DocumentBuilder

      Document document = DocumentBuilder.parse("book.xml");

      //4.to handle the document further ,we can use getElementByTagName("") method get the NodeList

      NodeList list = document.getElementByTagName("");

      //5.get the single specific node

      Node node = list.item(0);

      //6.print the node's text content

      System.out.println(node.getTextContent());

    To improve the effeciency of using DOM to parse XML , write a util is not a bad idea,isn't it?

    public class DOMUtils{

      public static Document getDocument(String path){

        return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(path);

      }

      

      public static Document getDocument(File file){

        return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(path);

      }

      public void writeXml2File(Document document,String path){

        TransformerFactory.newInstance().newTransformer.transform(new DOMSource(document),new StreamResult(path));

      }

    }

    To append an element to a specific element by DOM , we can use these code:

      //1.create an element first

      Element element = document.createElement("element's name");

      //2.set the element's text content

      element.setTextContent("element's content text");

      //3.set the element as the child element and append this child element to the parent element.

      parentElement.appendChild(element);

  • 相关阅读:
    CentOS /RHEL系统怎么更新安全补丁
    使用yum查询系统安装的软件及可以更新的软件并单独指定升级某一个软件
    惠普服务器通过ILO系统远程安装系统
    django进行数据库迁移的错误处理方法:You are trying to change the nullable field 'title' on book to non-nullable without a d
    Django数据库增删改查
    linux用户
    linux基础
    mysql小白系列_01 原理
    mysql运维入门6:MySQL读写分离
    mysql运维入门5:MySQL+kepalived高可用架构
  • 原文地址:https://www.cnblogs.com/ppcoder/p/7147630.html
Copyright © 2011-2022 走看看