Web foundation - XML

Article directory

XML:

1. concept:

	Extensible Markup Language
	*Extensible: labels are all customized. <user>  <student>

	* function
		*Store data
			1. Profile
			2. Transmission in the network
	*The difference between xml and html
		1. xml tags are all customized, and html tags are predefined.
		2. xml has strict syntax and html has loose syntax
		3. xml stores data and html displays data

	*w3c: World Wide Web Alliance

2. syntax:

	* Basic syntax:
		1. xml Suffix name of the document .xml
		2. xml The first line must be defined as a document declaration
		3. xml There is and only one root label in the document
		4. Attribute value must be quoted(Both single and double can.)Bring up
		5. Label must be closed correctly
		6. xml Label names are case sensitive
	* Quick start:
		<?xml version='1.0' ?>
		<users>
			<user id='1'>
				<name>zhangsan</name>
				<age>23</age>
				<gender>male</gender>
				<br/>
			</user>
			
			<user id='2'>
				<name>lisi</name>
				<age>24</age>
				<gender>female</gender>
			</user>
		</users>
		
	* component:
		1. Document declaration
			1. Format:<?xml Attribute list ?>
			2. Property list:
				* version: Version number, required attribute
				* encoding: Coding method. Tells the parsing engine the character set used by the current document. Default value: ISO-8859-1
				* standalone: Independence
					* Value:
						* yes: Do not rely on other files
						* no: Rely on other files
		2. instructions(understand): Combination css Of
			* <?xml-stylesheet type="text/css" href="a.css" ?>
		3. Labels: label name custom
			* Rules:
				* Names can contain letters, numbers, and other characters 
				* Names cannot start with numbers or punctuation 
				* Name cannot be in letters xml(perhaps XML,Xml Wait) start 
				* Name cannot contain spaces 

		4. Properties:
			id Unique property value
		5. Text:
			* CDATA Area: data in this area will be displayed as is
				* Format:  <![CDATA[ data ]]>


	* Constraints: Provisions xml Writing rules of documents
		* As a user of the framework(Programmer): 
			1. Can be in xml Constraint document in
			2. Be able to read constraint documents easily
		
		* Classification:
			1. DTD:A simple constraint technique
			2. Schema:A complex constraint technique


		* DTD: 
			* Introduce dtd Document to xml In document
				* inside dtd: Define constraint rules in xml In document
				* external dtd: Define the constraint's rules outside the dtd In file
					* Local:<!DOCTYPE Root signature SYSTEM "dtd Location of files">
					* Network:<!DOCTYPE Root signature PUBLIC "dtd File name" "dtd Location of files URL">


		* Schema:
			* Introduce:
				1.fill in xml Root element of the document
				2.Introduce xsi prefix.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
				3.Introduce xsd File namespace.  xsi:schemaLocation="http://www.itcast.cn/xml  student.xsd"
				4.For every one xsd Constraints declare a prefix,As logo  xmlns="http://www.itcast.cn/xml" 

			<students   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
				xmlns="http://www.itcast.cn/xml"
				xsi:schemaLocation="http://www.itcast.cn/xml  student.xsd">

3. Parsing: operate the xml document and read the data in the document into memory

	* operation xml File
		1. analysis(read): Read the data in the document into memory
		2. Write: save data in memory to xml In the document. Persistent storage

	* analysis xml The way:
		1. DOM: Load the markup language document into memory at one time to form a dom tree
			* Advantages: easy to operate, can be on the document CRUD All actions for
			* Disadvantage: memory occupied
		2. SAX: Read line by line, event driven.
			* Advantage: no memory.
			* Disadvantages: only read, not add, delete or modify


	
	* xml Common parsers:
		1. JAXP: sun Company provided resolver, support dom and sax Two ideas
		2. DOM4J: A very good parser
		3. Jsoup: jsoup It's a paragraph. Java Of HTML Resolver, which can directly parse a URL Address, HTML Text content. It provides a very labor-saving API,May pass DOM,CSS And similar to jQuery To retrieve and manipulate data.
		4. PULL: Android The built-in parser of the operating system, sax Manner.


	* Jsoup: jsoup It's a paragraph. Java Of HTML Resolver, which can directly parse a URL Address, HTML Text content. It provides a very labor-saving API,May pass DOM,CSS And similar to jQuery To retrieve and manipulate data.
		* Quick start:
			* Steps:
				1. Import jar package
				2. Obtain Document object
				3. Get the corresponding label Element object
				4. get data

		* Code:
			 //2.1 get the path of student.xml
	        String path = JsoupDemo1.class.getClassLoader().getResource("student.xml").getPath();
	        //2.2 parse the xml document, load the document into memory, and obtain the dom tree -- > Document
	        Document document = Jsoup.parse(new File(path), "utf-8");
	        //3. Get Element
	        Elements elements = document.getElementsByTag("name");
	
	        System.out.println(elements.size());
	        //3.1 get the Element object of the first name
	        Element element = elements.get(0);
	        //3.2 access to data
	        String name = element.text();
	        System.out.println(name);

	* Use of objects:
		1. Jsoup: Tool class, which can be parsed html or xml Documents, returning Document
			* parse: analysis html or xml Documents, returning Document
				* parse​(File in, String charsetName): analysis xml or html Documentation.
				* parse​(String html): analysis xml or html Character string
				* parse​(URL url, int timeoutMillis): Get the specified html or xml Document object for
		2. Document: Document object. Represents in memory dom tree
			* Obtain Element object
				* getElementById​(String id): according to id Property value get unique element object
				* getElementsByTag​(String tagName): Get element object collection based on label name
				* getElementsByAttribute​(String key): Get element object collection based on attribute name
				* getElementsByAttributeValue​(String key, String value): Get element object collection according to corresponding attribute name and attribute value
		3. Elements: element Element The collection of objects. Can be regarded as ArrayList<Element>To use
		4. Element: Element object
			1. Get child element object
				* getElementById​(String id): according to id Property value get unique element object
				* getElementsByTag​(String tagName): Get element object collection based on label name
				* getElementsByAttribute​(String key): Get element object collection based on attribute name
				* getElementsByAttributeValue​(String key, String value): Get element object collection according to corresponding attribute name and attribute value

			2. Get property value
				* String attr(String key): Get property value according to property name
			3. Get text content
				* String text():Get text content
				* String html():Get all contents of label body(Include string contents of word Tags)
		5. Node: Node object
			* yes Document and Element Parent class

		
	* Shortcut:
		1. selector:selector
			* Method used: Elements	select​(String cssQuery)
				* Syntax: Reference Selector Syntax defined in class
		2. XPath: XPath mean XML Path language, which is used to determine XML(A subset of the standard General Markup Language) the language of a part of a document
			* Use Jsoup Of Xpath Additional import required jar Bag.
			* query w3cshool Reference manual, using xpath Syntax completion query for
			* Code:
				//1. Get the path of student.xml
		        String path = JsoupDemo6.class.getClassLoader().getResource("student.xml").getPath();
		        //2. Get Document object
		        Document document = Jsoup.parse(new File(path), "utf-8");
		
		        //3. Create a JXDocument object according to the document object
		        JXDocument jxDocument = new JXDocument(document);
		
		        //4. Query with xpath syntax
		        //4.1 query all student Tags
		        List<JXNode> jxNodes = jxDocument.selN("//student");
		        for (JXNode jxNode : jxNodes) {
		            System.out.println(jxNode);
		        }
		
		        System.out.println("--------------------");
		
		        //4.2 query name tags under all student Tags
		        List<JXNode> jxNodes2 = jxDocument.selN("//student/name");
		        for (JXNode jxNode : jxNodes2) {
		            System.out.println(jxNode);
		        }
		
		        System.out.println("--------------------");
		
		        //4.3 query the name tag with id attribute under the student tag
		        List<JXNode> jxNodes3 = jxDocument.selN("//student/name[@id]");
		        for (JXNode jxNode : jxNodes3) {
		            System.out.println(jxNode);
		        }
		        System.out.println("--------------------");
		        //4.4 query the name tag with id attribute under the student tag and the id attribute value is itcast
		
		        List<JXNode> jxNodes4 = jxDocument.selN("//student/name[@id='itcast']");
		        for (JXNode jxNode : jxNodes4) {
		            System.out.println(jxNode);
		        }
Published 65 original articles, won praise 1, visited 2308
Private letter follow

Keywords: xml Attribute network Java

Added by jawinn on Thu, 23 Jan 2020 16:54:38 +0200