search
HomeBackend DevelopmentXML/RSS TutorialDetailed introduction to DOM parsing in XML parsing

1. Concept

xml files are mostly used to describe information, so after obtaining an xml document, extracting the corresponding information according to the elements in the xml is xml parsing. There are two ways to parse Xml, one is DOM parsing and the other is SAX parsing. The two operation methods are as shown in the figure.

Detailed introduction to DOM parsing in XML parsing

2. DOM parsing

The xml parser based on DOM parsing converts it into a collection of object models, using A tree is a data structure that stores information. Through the DOM interface, the application can access any part of the data in the xml document at any time, so this method of using the DOM interface to access is also called random access.

This method also has flaws, because the DOM analyzer converts the entire xml file into a tree and stores it in memory. When the file structure is large or the data is complex, this method has higher memory requirements. , and traversing a tree with a complex structure is also a very time-consuming operation. However, the tree structure used by DOM is consistent with the way xml stores information, and its random access can also be used, so the DOM interface still has widespread use value.

Here we give an example to illustrate the data structure of converting xml into a tree.

<?xml version="1.0" encoding="GBK"?>
<address>
	<linkman>
		<name>Van_DarkHolme</name>
		<email>van_darkholme@163.com</email>
	</linkman>
	<linkman>
		<name>Bili</name>
		<email>Bili@163.com</email>
	</linkman>
</address>

The structure of converting the xml into a tree is:

Detailed introduction to DOM parsing in XML parsing

There are the following 4 core operation interfaces in DOM parsing

Document: This interface represents the entire xml document and is represented as the root of the entire DOM, which is the entrance to the tree. Through this interface, the contents of all elements in the xml can be accessed. The common methods are as follows.

(Note: Although not shown in the above figure, the attributes of name and email are also one node respectively)

Common methods of Document

Detailed introduction to DOM parsing in XML parsing

Node: This interface plays an important role in the entire DOM tree. The core interfaces of DOM operations are inherited from Node (Document, Element, Attr). In the DOM tree, each Node interface represents a DOM tree node

Common methods of Node interface

Detailed introduction to DOM parsing in XML parsing

NodeList: This interface represents a collection of points. Generally used for a set of nodes in an ordered relationship.

NodeList common methods

Detailed introduction to DOM parsing in XML parsing

##NamedNodeMap: This interface represents the one-to-one relationship between a group of nodes and their unique names, and is mainly used to represent node attributes

In addition to the above four core interfaces, if a program needs to perform DOM parsing operations, it needs to follow the following steps:

1. Establish a DocumentBuilderFactor to obtain the DocumentBuilder object:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

2. Create DocumentBuidler:

DocumentBuilder builder = factory.newDocumentBuilder();

3. Create Document object and obtain Entry of the tree:

Document doc = builder.parse("relative path or absolute path of the xml file");

4. Create NodeList:

NodeList n1 = doc .getElementByTagName("Read Node");

5. Get xml information

public class DOMDemo01 {
	
	public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException{
		//建立DocumentBuilderFactor,用于获得DocumentBuilder对象:
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		//2.建立DocumentBuidler:
		DocumentBuilder builder = factory.newDocumentBuilder();
		//3.建立Document对象,获取树的入口:
		Document doc = builder.parse("src//dom_demo_02.xml");
		//4.建立NodeList:
		NodeList node = doc.getElementsByTagName("linkman");
		//5.进行xml信息获取
		for(int i=0;i<node.getLength();i++){
			Element e = (Element)node.item(i);
			System.out.println("姓名:"+
					e.getElementsByTagName("name").item(0).getFirstChild().getNodeValue());
			System.out.println("邮箱:"+
					e.getElementsByTagName("email").item(0).getFirstChild().getNodeValue());
		}	
		
	}
}

Detailed introduction to DOM parsing in XML parsing

The above code will be analyzed from the fourth point:

Through doc.getElementByTagName("linkman") we obtain a NodeList. The above xml file contains two linkman nodes, so the NodeList here contains two Node (both linkman nodes), and then through the loop method to obtain the information in the xml file.

Element e = (Element)node.item(i) obtains the linkman node, that is, e points to the linkman

e.getElementTagName("name").item(0).getFirstChild ().getNodeValue();

getElementTagName("name"); Obtained all name nodes under the linkman (actually only 1);

Item(0); Take the first Name nodes (just one);

getFristChild(); Get the text node under the name node, which is the node where the content van is located (as mentioned above, the text content is also a separate node, createTextNode() in the Document method list is to create the text node);

getNodeValue() gets the value of the text node: van_darkholme;

For more related questions, please visit the PHP Chinese website: XML video tutorial

The above is the detailed content of Detailed introduction to DOM parsing in XML parsing. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete
Vue3获取DOM节点的方式有哪些Vue3获取DOM节点的方式有哪些May 11, 2023 pm 04:55 PM

1.原生js获取DOM节点:document.querySelector(选择器)document.getElementById(id选择器)document.getElementsByClassName(class选择器)....2.vue2中获取当前组件的实例对象:因为每个vue的组件实例上,都包含一个$refs对象,里面存储着对应的DOM元素或组件的引用。所以在默认情况下,组件的$refs指向一个空对象。可以先在组件上加上ref="名字",然后通过this.$refs.

PHP中的DOM操作指南PHP中的DOM操作指南May 21, 2023 pm 04:01 PM

在网页开发中,DOM(DocumentObjectModel)是一个非常重要的概念。它可以让开发者轻松地对一个网页的HTML或XML文档进行修改和操作,比如添加、删除、修改元素等。而PHP中内置的DOM操作库也为开发者提供了丰富的功能,本文将介绍PHP中的DOM操作指南,希望可以帮助到大家。DOM的基本概念DOM是一个跨平台、独立于语言的API,它可以将

vue dom是什么意思啊vue dom是什么意思啊Dec 20, 2022 pm 08:41 PM

dom是一种文档对象模型,同时也是用于html编程的接口,通过dom来操作页面中的元素。DOM是HTML文档的内存中对象表示,它提供了使用JavaScript与网页交互的方式。DOM是节点的层次结构(或树),其中document节点作为根。

vue3中ref绑定dom或组件失败的原因是什么及怎么解决vue3中ref绑定dom或组件失败的原因是什么及怎么解决May 12, 2023 pm 01:28 PM

vue3ref绑定dom或者组件失败原因分析场景描述在vue3中经常用到使用ref绑定组件或者dom元素的情况,很多时候,明明使用ref绑定了相关组件,但是经常ref绑定失败的情况。ref绑定失败情况举例ref绑定失败的绝大多数情况是,在ref和组件绑定的时候,该组件还未渲染,所以绑定失败。或者组件刚开始未渲染,ref未绑定,当组件开始渲染,ref也开始绑定,但是ref和组件并未绑定完成,这个时候使用组件相关的方法就会出现问题。ref绑定的组件使用了v-if,或者他的父组件使用了v-if导致页面

dom和bom对象有哪些dom和bom对象有哪些Nov 13, 2023 am 10:52 AM

dom和bom对象有:1、“document”、“element”、“Node”、“Event”和“Window”等5种DOM对象;2、“window”、“navigator”、“location”、“history”和“screen”等5种BOM对象。

bom和dom有什么区别bom和dom有什么区别Nov 13, 2023 pm 03:23 PM

bom和dom在作用和功能、与JavaScript的关系、相互依赖性、不同浏览器的兼容性和安全性考虑等方面都有区别。详细介绍:1、作用和功能,BOM的主要作用是操作浏览器窗口,它提供了浏览器窗口的直接访问和控制,而DOM的主要作用则是将网页文档转换为一个对象树,允许开发者通过这个对象树来获取和修改网页的元素和内容;2、与JavaScript的关系等等。

JavaScript DOM 常用事件最新总结!JavaScript DOM 常用事件最新总结!Aug 07, 2022 am 11:05 AM

本文给大家总结了JS DOM的常用事件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。

dom内置对象有哪些dom内置对象有哪些Dec 19, 2023 pm 03:45 PM

dom内置对象有:1、document;2、window;3、navigator;4、location;5、history;6、screen;7、document.documentElement;8、document.body;9、document.head;10、document.title;11、document.cookie。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools