Home  >  Article  >  Backend Development  >  Introduction to JAXP parsing of xml parsing methods

Introduction to JAXP parsing of xml parsing methods

黄舟
黄舟Original
2017-02-16 15:41:532192browse

 XML解析	
		1 引入
		xml文件除了给开发者看,更多的情况使用[程序读取xml文件]的内容。这叫做xml解析
		2 XML解析方式(原理不同)
			DOM解析
			SAX解析
		3 XML解析工具
				DOM解析原理:
						1)JAXP (oracle-Sun公司官方)
						2)JDOM工具(非官方)
						3)Dom4J工具(非官方)
							三大框架(默认读取xml的工具就是Dom4j)
						.......

				SAX解析原理:
						1)Sax解析工具(oracle-sun公司官方)

The following introduces the parsing principle of JAXP:


lJAXP: (Java API for XML Processing)The development package is part of JavaSE, which consists of the following packages and their sub-packages:

org.w3c.dom:Provides a standard interface for parsing XML##DOM

org.xml.sax: Provides SAX way to parseXML's standard interface

javax.xml:provides parsing XMLDocument class

l

javax.xml.parsersIn the package, several are defined Factory class. We can get the DOM and SAX## that parse the XML document by calling these factory classes. #Parser object.

DocumentBuilderFactory

SAXParserFactoryl

javax.xml.parsers#DocumentBuilderFactory in the package is used to create DOMPattern parser object , DocumentBuilderFactory is an abstract factory class, which cannot be instantiated directly, but this class provides a newInstance method, which will According to the parser installed by default on the local platform, a factory object is automatically created and returned. Process:


##

调用 DocumentBuilderFactory.newInstance() 方法得到创建 DOM 解析器的工厂。

调用工厂对象的 newDocumentBuilder方法得到 DOM 解析器对象。

调用 DOM 解析器对象的 parse() 方法解析 XML 文档,得到代表整个文档的 Document 对象,进而可以利用DOM特性对整个XML文档进行操作了。

The specific code to obtain the document object is as follows:



//1. 获得工厂 
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		
		// 2. 拿到 builder 对象
		DocumentBuilder builder = factory.newDocumentBuilder();
		
		//3. 拿到 代表book.xml文件的document 对象 
		// ctrl+1 --- 提示 快速  分配一个变量值 
		Document document = builder.parse("src/book.xml");




##The next article will include an interlude - unit Test the framework, and then introduce some CRUD examples to have an intuitive understanding of JAXP parsing xml.


The above is the introduction to JAXP parsing of xml parsing method. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
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
Previous article:java Domj4 reads xml fileNext article:java Domj4 reads xml file