国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
XMLProgramming-DOM" >XMLProgramming-DOM
A development package for programming " >JavaA development package for programming
W3C" >, yes W3C
DOM parser# in Jaxp " >Get the DOM parser# in Jaxp

XML Programming-DOM

Feb 20, 2017 pm 03:08 PM

XMLProgramming-DOM

#XML

Parsing technology


##saxAnalysis. dom(Document Object Model, That is, the document object model

)isW3CThe organization's recommended way of processing XML. sax(Simple API for XML) is not an official standard, but it is

XMLThe de facto standard in the community, almost all XML parsers support it. JaxpIntroduction

Jaxp (Java API for XML Processing)

is

JavaA development package for programming

XML, which consists of javax.xml, org. It consists of w3c.dom , org.xml.sax packages and their sub-packages. In the javax.xml.parsers package, several factory classes are defined. When programmers call these factory classes, they can get the The

DOM or SAX parser object used to parse the xml document. DOMBasic overview

DOM(Document Object ModelDocument Object Model

)

, yes W3C

The organization's recommended standard programming interface for handling extensible markup language. XML DOM defines the objects and attributes of all XML elements, as well as the methods (interfaces) to access them. Schematic


#DOM

Model

(document object model)

DOMWhen the parser parses the XML

document, it will parse all the elements in the document into individual elements according to the hierarchical relationship in which they appear.

Node

Object(Node). In dom, the relationship between nodes is as follows: 1

,

are located on one node The node is the parent node of the node (parent)2

,

The node under a node is the child node of the node (children

3, Nodes at the same level and with the same parent node are sibling nodes (sibling)

4,The node set at the next level of a node is the node descendant(descendant)

5,parent,grandfather node and all nodes located above the node are Node’s ancestor(ancestor)

NodeObject

The Node object provides a series of constants to represent The type of node. When developers obtain a certain Node type, they can convert the Node node into the corresponding node object. (Node's subclass object), in order to call its unique method. (See API documentation)

The Node object provides corresponding methods to obtain its parent node or child node. Through these methods, programmers can read the contents of the entire XML document, or add, modify, or delete the contents of the XML document. .

PS: Its sub-interface Element has more functions.

Get the DOM parser# in Jaxp

##1

, call the DocumentBuilderFactory.newInstance() method to create the DOM parser factory.

2

, call the DocumentBuilderFactory object’s newDocumentBuilder() method to get DOMParser object,It is the object of DocumentBuilder.

3

, call the DocumentBuilder object’s parse() method parsing XMLDocument, get the Document object representing the entire document.

4

, through Document objects and some related classes and methods, to XML Document to operate.

Update

XMLDocumentation

javax.xml.transform

in the package The Transformer class is used to convert the Document object representing the XML file into a certain format. For output, for example, apply a style sheet to the xml file and convert it into a html document. Using this object, of course, you can also rewrite the Document object into a XML file.

The Transformer class completes the transformation operation through the transform method, which receives a source and a destination. We can associate the

document

object to be converted through: javax.xml.transform.dom.DOMSource class,

Use javax.xml.transform.stream.StreamResult object to represent the destination of the data.

The Transformer object is obtained through TransformerFactory.

Case:

XML5.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?><班級(jí) 班次="1班" 編號(hào)="C1">
	<學(xué)生 地址="湖南" 學(xué)號(hào)="n1" 性別="男" 授課方式="面授" 朋友="n2" 班級(jí)編號(hào)="C1">
		<名字>張三</名字>
		<年齡>20</年齡>
		<介紹>不錯(cuò)</介紹>
	</學(xué)生>
	<學(xué)生 學(xué)號(hào)="n2" 性別="女" 授課方式="面授" 朋友="n1 n3" 班級(jí)編號(hào)="C1">
		<名字>李四</名字>
		<年齡>18</年齡>

		<介紹>很好</介紹>
	</學(xué)生>
	<學(xué)生 學(xué)號(hào)="n3" 性別="男" 授課方式="面授" 朋友="n2" 班級(jí)編號(hào)="C1">
		<名字>王五</名字>
		<年齡>22</年齡>
		<介紹>非常好</介紹>
	</學(xué)生>
	<學(xué)生 性別="男">
		<名字>小明</名字>
		<年齡>30</年齡>
		<介紹>好</介紹>
	</學(xué)生>
</班級(jí)>


package com.pc;

import java.awt.List;
import java.util.ArrayList;

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

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

/**
 * 
 * @author Switch
 * @function Java解析XML
 * 
 */
public class XML5 {
	// 使用dom技術(shù)對(duì)xml文件進(jìn)行操作
	public static void main(String[] args) throws Exception {
		// 1.創(chuàng)建一個(gè)DocumentBuilderFactory對(duì)象
		DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
				.newInstance();
		// 2.通過DocumentBuilderFactory,得到一個(gè)DocumentBuilder對(duì)象
		DocumentBuilder documentBuilder = documentBuilderFactory
				.newDocumentBuilder();
		// 3.指定解析哪個(gè)xml文件
		Document document = documentBuilder.parse("src/com/pc/XML5.xml");
		// 4.對(duì)XML文檔操作
		// System.out.println(document);
		// list(document);
		// read(document);
		// add(document);
		// delete(document, "小明");
		update(document, "小明", "30");
	}

	// 更新一個(gè)元素(通過名字更新一個(gè)學(xué)生的年齡)
	public static void update(Document doc, String name, String age)
			throws Exception {
		NodeList nodes = doc.getElementsByTagName("名字");
		for (int i = 0; i < nodes.getLength(); i++) {
			Element nameE = (Element) nodes.item(i);
			if (nameE.getTextContent().equals(name)) {
				Node prNode = nameE.getParentNode();
				NodeList stuAttributes = prNode.getChildNodes();
				for (int j = 0; j < stuAttributes.getLength(); j++) {
					Node stuAttribute = stuAttributes.item(j);
					if (stuAttribute.getNodeName().equals("年齡")) {
						stuAttribute.setTextContent(age);
					}
				}
			}
		}
		updateToXML(doc);
	}

	// 刪除一個(gè)元素(通過名字刪除一個(gè)學(xué)生)
	public static void delete(Document doc, String name) throws Exception {
		// 找到第一個(gè)學(xué)生
		NodeList nodes = doc.getElementsByTagName("名字");
		for (int i = 0; i < nodes.getLength(); i++) {
			Node node = nodes.item(i);
			if (node.getTextContent().equals(name)) {
				Node prNode = node.getParentNode();
				prNode.getParentNode().removeChild(prNode);
			}
		}

		// 更新到XML
		updateToXML(doc);
	}

	// 添加一個(gè)學(xué)生到XML文件
	public static void add(Document doc) throws Exception {
		// 創(chuàng)建一個(gè)新的學(xué)生節(jié)點(diǎn)
		Element newStu = doc.createElement("學(xué)生");
		newStu.setAttribute("性別", "男");
		Element newStu_name = doc.createElement("名字");
		newStu_name.setTextContent("小明");
		Element newStu_age = doc.createElement("年齡");
		newStu_age.setTextContent("21");
		Element newStu_intro = doc.createElement("介紹");
		newStu_intro.setTextContent("好");
		newStu.appendChild(newStu_name);
		newStu.appendChild(newStu_age);
		newStu.appendChild(newStu_intro);
		// 把新的學(xué)生節(jié)點(diǎn)添加到根元素
		doc.getDocumentElement().appendChild(newStu);

		// 更新到XML
		updateToXML(doc);

	}

	// 更新到XML
	private static void updateToXML(Document doc)
			throws TransformerFactoryConfigurationError,
			TransformerConfigurationException, TransformerException {
		// 得到TransformerFactory對(duì)象
		TransformerFactory transformerFactory = TransformerFactory
				.newInstance();
		// 通過TransformerFactory對(duì)象得到一個(gè)轉(zhuǎn)換器
		Transformer transformer = transformerFactory.newTransformer();
		transformer.transform(new DOMSource(doc), new StreamResult(
				"src/com/pc/XML5.xml"));
	}

	// 具體查詢某個(gè)學(xué)生的信息(小時(shí)第一個(gè)學(xué)生的所有)
	public static void read(Document doc) {
		NodeList nodes = doc.getElementsByTagName("學(xué)生");
		// 取出第一個(gè)學(xué)生
		Element stu1 = (Element) nodes.item(0);
		Element name = (Element) stu1.getElementsByTagName("名字").item(0);
		System.out.println("姓名:" + name.getTextContent() + " 性別:"
				+ stu1.getAttribute("性別"));
	}

	// 遍歷該XML文件
	public static void list(Node node) {
		if (node.getNodeType() == node.ELEMENT_NODE) {
			System.out.println("名字:" + node.getNodeName());
		}
		// 取出node的子節(jié)點(diǎn)
		NodeList nodes = node.getChildNodes();
		for (int i = 0; i < nodes.getLength(); i++) {
			// 顯示所有子節(jié)點(diǎn)
			Node n = nodes.item(i);
			list(n);
		}
	}

}

The above is the content of XML programming-DOM. For more related content, please pay attention to the PHP Chinese website (www.miracleart.cn)!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

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.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1502
276
Can I open an XML file using PowerPoint? Can I open an XML file using PowerPoint? Feb 19, 2024 pm 09:06 PM

Can XML files be opened with PPT? XML, Extensible Markup Language (Extensible Markup Language), is a universal markup language that is widely used in data exchange and data storage. Compared with HTML, XML is more flexible and can define its own tags and data structures, making the storage and exchange of data more convenient and unified. PPT, or PowerPoint, is a software developed by Microsoft for creating presentations. It provides a comprehensive way of

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

How to handle XML and JSON data formats in C# development How to handle XML and JSON data formats in C# development Oct 09, 2023 pm 06:15 PM

How to handle XML and JSON data formats in C# development requires specific code examples. In modern software development, XML and JSON are two widely used data formats. XML (Extensible Markup Language) is a markup language used to store and transmit data, while JSON (JavaScript Object Notation) is a lightweight data exchange format. In C# development, we often need to process and operate XML and JSON data. This article will focus on how to use C# to process these two data formats, and attach

How to use PHP functions to process XML data? How to use PHP functions to process XML data? May 05, 2024 am 09:15 AM

Use PHPXML functions to process XML data: Parse XML data: simplexml_load_file() and simplexml_load_string() load XML files or strings. Access XML data: Use the properties and methods of the SimpleXML object to obtain element names, attribute values, and subelements. Modify XML data: add new elements and attributes using the addChild() and addAttribute() methods. Serialized XML data: The asXML() method converts a SimpleXML object into an XML string. Practical example: parse product feed XML, extract product information, transform and store it into a database.

C   and XML: Exploring the Relationship and Support C and XML: Exploring the Relationship and Support Apr 21, 2025 am 12:02 AM

C interacts with XML through third-party libraries (such as TinyXML, Pugixml, Xerces-C). 1) Use the library to parse XML files and convert them into C-processable data structures. 2) When generating XML, convert the C data structure to XML format. 3) In practical applications, XML is often used for configuration files and data exchange to improve development efficiency.

What are the dom and bom objects? What are the dom and bom objects? Nov 13, 2023 am 10:52 AM

There are 5 DOM objects including "document", "element", "Node", "Event" and "Window"; 2. "window", "navigator", "location" and "history" and "screen" and other 5 BOM objects.

Comparison of Java libraries for XML parsing: Finding the best solution Comparison of Java libraries for XML parsing: Finding the best solution Mar 09, 2024 am 09:10 AM

Introduction XML (Extensible Markup Language) is a popular format for storing and transmitting data. Parsing XML in Java is a necessary task for many applications, from data exchange to document processing. To parse XML efficiently, developers can use various Java libraries. This article will compare some of the most popular XML parsing libraries, focusing on their features, functionality, and performance to help developers make an informed choice. DOM (Document Object Model) parsing library JavaXMLDOMAPI: a standard DOM implementation provided by Oracle. It provides an object model that allows developers to access and manipulate XML documents. DocumentBuilderFactoryfactory=D

XML/RSS Data Integration: Practical Guide for Developers & Architects XML/RSS Data Integration: Practical Guide for Developers & Architects Apr 02, 2025 pm 02:12 PM

XML/RSS data integration can be achieved by parsing and generating XML/RSS files. 1) Use Python's xml.etree.ElementTree or feedparser library to parse XML/RSS files and extract data. 2) Use ElementTree to generate XML/RSS files and gradually add nodes and data.

See all articles