Today’s content

XML 1. Concepts 2. Syntax 3Copy the code

XML:

* Extensible: The tags are all custom. <user> <student> * Function * Store data 1. Configuration file 2. Transmission over the network * Differences between XML and HTML 1. XML tags are user-defined, while HTML tags are predefined. 3. XML stores data, HTML presents data Syntax: * Basic syntax: 1. XML document suffix.xml 2. XML first line must be defined as document declaration 3. XML document has one and only root tag 4. Attribute values must be enclosed in quotation marks (odd or even). 5. Tags must be closed correctly. The 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> * 1. Format: <? XML attribute list? > 2. Attribute list: * version: version number, required attribute * encoding: encoding mode. Indicates the character set used by the parsing engine. Default value: ISO-8859-1 * standalone: Standalone or not Value: * yes: independent of other files * no: independent of other files 2. Instruction (understood) : Combined with CSS * <? xml-stylesheet type="text/css" href="a.css" ? > 3. Tag: Tag name User-defined * rules: * The name can contain letters, numbers, and other characters * the name cannot start with a number or punctuation * The name cannot start with the letter XML (or XML, XML, etc.) * The name cannot contain Spaces 4. Attribute: ID Attribute value unique 5. Text: * CDATA area: Data in this area will be displayed as is * format: <! [CDATA[data]]> * Constraint: specifies the rules for writing XML documents * As a framework consumer (programmer) : 1. DTD: a simple constraint technique 2. Schema: a complex constraint technique * DTD: * Imported DTD documents into XML documents * Internal DTDS: Define constraint rules in AN XML document * External DTD: Define the rules of a constraint in an external DTD file * local: <! DOCTYPE root tag name SYSTEM "DTD file location "> * network: <! DOCTYPE Root tag PUBLIC "DTD file name" "DTD file location URL"> * Schema: * Import: 1. Fill in the root element of the XML document (2) to introduce xsi prefix. XMLNS: xsi = "http://www.w3.org/2001/XMLSchema-instance" 3. Xsi :schemaLocation="http://www.itcast.cn/xml student.xsd" 4 For each statement a prefix XSD constraints, as mark 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: Manipulate XML document, read data from the document into memory * Manipulate XML document 1. Parse (read) : Reads the data in the document into memory. 2. Write: saves the data in memory to the XML document. DOM: The markup language document is loaded into memory at once, forming a DOM tree in memory * Advantages: easy to operate, can do all the CRUD operations on the document * Disadvantages: memory 2. SAX: read line by line, event-driven. * Advantages: no memory. * Common parsers for XML: 1. JAXP: SUN parsers that support both DOM and SAX; 2. DOM4J: an excellent parser; 3. Jsoup is a Java HTML parser that can directly parse a URL address and HTML text content. It provides a very labor-intensive API for retrieving and manipulating data using DOM, CSS, and jquery-like manipulation methods. 4. PULL: Built-in parser of Android operating system, in sax mode. * Jsoup: Jsoup is a Java HTML parser that can directly parse a URL address, HTML text content. It provides a very labor-intensive API for retrieving and manipulating data using DOM, CSS, and jquery-like manipulation methods. * Quick start: * Steps: 1. Import jar package 2. Obtain Document object 3. Get the corresponding tag Element object. / / 2.1 for student. XML path String path = JsoupDemo1. Class. GetClassLoader (). The getResource (" student. XML "). GetPath (); Document Document = Jsoup. Parse (new File(path), "utF-8 "); / / 3. Get Element object Element Elements Elements = document. GetElementsByTag (" name "); System.out.println(elements.size()); Element Element = elements. Get (0); String name = element.text(); System.out.println(name); Document * parse(File in, String charsetName); Document * parse(File in, String charsetName) Parsing XML or HTML files. * Parse (String HTML) : parses XML or HTML strings * parse(URL URL, int timeoutMillis) : obtains the specified HTML or XML Document object from the network path. GetElementById (String ID) : Gets a unique Element object based on the id attribute value. GetElementsByTag (String tagName) : GetElementsByAttribute (String Key) : getElementsByAttribute(String key) Collection of objects according to the attribute names for Elements * getElementsByAttributeValue (String key, String value) : according to the corresponding property name Element and attribute values for a collection of objects (3) Elements: the Element Element collections of objects. You can use it as an ArrayList<Element> GetElementById (String ID) : getElementsByTag(String tagName) : getElementById(String id) : getElementsByTag(String tagName) GetElementsByAttribute (String Key) : getElementsByAttribute(String key) According to the attribute name for element object set * getElementsByAttributeValue (String key, String value) : according to the corresponding attribute name and attribute value for element object set 2. Get attribute value * String attR (String key) : Get attribute value based on attribute name 3. HTML (): Get all the contents of the tag body (including the String content of the word tag) 5. Node: Node object * is the parent of Document and Element * Shortcut query: Elements select(String cssQuery) * Syntax: reference the syntax defined in the selector class. XPath, or XML path language, is a language used to determine the location of a part of an XML document (a subset of the Standard Universal Markup Language) * XPath using Jsoup requires an additional import of jar packages. * Query w3Cshool reference manual, using xpath syntax to complete the query * code: //1. Access to the student. The XML path String path = JsoupDemo6. Class. GetClassLoader (). GetResource (" student. XML "). GetPath (); Parse (new File(path), "utF-8 "); JXDocument JXDocument = new JXDocument(document); List< jxNodes > jxNodes = jxdocument.seln ("//student"); for (JXNode jxNode : jxNodes) { System.out.println(jxNode); } System.out.println("--------------------"); List<JXNode> jxNodes2 = jxdocument.seln ("//student/name"); for (JXNode jxNode : jxNodes2) { System.out.println(jxNode); } System.out.println("--------------------"); List<JXNode> jxNodes3 = jxdocument.seln ("//student/name[@id]"); for (JXNode jxNode : jxNodes3) { System.out.println(jxNode); } System.out.println("--------------------"); JxNodes4 = jxdocument.seln ("//student/name[@id='itcast']"); for (JXNode jxNode : jxNodes4) { System.out.println(jxNode); }Copy the code