Parsing XML files with DOM parser study notes

  1. The principle of dom parsing is consistent with that of dom4j
  2. Node is the parent interface of all elements
  3. Commonly used APIs:
    • DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); Get DOM parser factory
    • DocumentBuilder domParser = factory.newDocumentBuilder(); Get DOM parser
    • domParser.parse(*.xml) loads the XML file that needs to be parsed
    • Document.getDocumentElement() gets the root element/node of the XML document
    • Element.getNodeName(): Get the root element
    • Element.getElementsByTagName("Car") gets a collection of "Car" elements
    • NodeList.item(i) gets the Nth element, starting from 0
    • Element.getTextContent(): Get the text content of the element
    • Element.getAttributes().getNamedItem("production time").getTextContent(): Get the value of an attribute in the element
    • document.createElement("car"); create a new element
    • Element.setTextContent("my car"); set the content of the element
    • Element.appendChild(newCarElement); add elements at the end
    • Element.insertBefore(newCarElement,
    • rootElement.getElementsByTagName("Car").item(1));Add an element before the specified element
    • TransformerFactory tf = TransformerFactory.newInstance(); create an output factory
    • Transformer transformer = tf.newTransformer(); create an output object
    • Source source = new DOMSource(document); create a document object in memory
    • Result result = new StreamResult(new File("src/cn/itcast/xml/dom/car.xml")); specify the destination of the output
    • transformer.transform(source, result); output the document object to the xml file
    • Element.setTextContent("Shenzhen"); update the content of the element
    • Element.removeChild(secondCarElement); removes direct child elements based on the parent element
  4. The dom parser treats whitespace characters as valid elements
  5. To make the dom parser ignore whitespace characters, two conditions must be met:
    a) A DTD constraint must be written for the XML file
    b) factory.setIgnoringElementContentWhitespace(true);
  6. dom class parser and sax class parser
    a) dom is loaded into the content at one time, forming a document object, manual navigation, suitable for curd
    b) sax is loaded into the content in stages, sax parser navigation, but programmers need to write sax Handler, must extend the DefaultHandler class, suitable for r

car.xml

<车辆清单>
    <汽车>
        <车牌 出产时间="2010年">奥迪</车牌>
        <产地>北京</产地>
        <单价>30</单价>
    </汽车>
    <汽车>
        <车牌 出产时间="2012年">本田</车牌>
        <产地>深圳</产地>
        <单价>60</单价>
    </汽车>
</车辆清单>
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

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

//使用DOM解析器解析XML文件
public class Demo1 {
    public static void main(String[] args) throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder domParser = factory.newDocumentBuilder();
        Document document = domParser.parse(new File("src/cn/itcast/xml/dom/car.xml"));
        Element rootElement  = document.getDocumentElement();
        System.out.println("根元素为:"+rootElement.getNodeName());
        NodeList nodeList = rootElement.getElementsByTagName("汽车");
        System.out.println("共有:" + nodeList.getLength()+"辆汽车");
        System.out.println("++++++++++++++++++++++++++");
        for(int i=0;i<nodeList.getLength();i++){
            Element element = (Element) nodeList.item(i);
            String band = element.getElementsByTagName("车牌").item(0).getTextContent();
            String place = element.getElementsByTagName("产地").item(0).getTextContent();
            String price = element.getElementsByTagName("单价").item(0).getTextContent();
            String time = element.getElementsByTagName("车牌").item(0).getAttributes().getNamedItem("出产时间").getTextContent();     

            System.out.println("车牌:" + band);
            System.out.println("产地:" + place);
            System.out.println("单价:" + price);
            System.out.println("出产时间:" + time);
            System.out.println("-------------------------");
        }
    }
}

Result:
The root element is: Vehicle list
Total: 2 cars
++++++++++++++++++++++++++
License plate: Audi Place of
Origin: Beijing
Unit price: 30
Production time :year 2010

License Plate: Honda Place
of Origin: Shenzhen
Unit Price: 60
Production Time: 2012

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324523089&siteId=291194637