XML and xml formatting
XML: eXtensible Markup Language
Comparison Between XML and Html
| |
XML |
Html |
| Design purpose |
Data storage and transmission |
Show data |
| Tag |
Custom |
Predefined |
| Syntax |
Strict |
Loose |
XML garbled characters
Cause: the encoding format of the file storing XML is the same as that stated in the first line of the XML file <? Xml version = "1.0" encoding = "encoding"?> XML error.
Analysis: the encoding format of the file storing XML is the actual encoding type of the XML file, and the file declared encoding tells the application to open it and decode it using the specified encoding method, an error occurred.
Solution: Change encoding to file encoding, or change the file encoding.
Question: What if I do not specify encoding?
Answer: If encoding is not specified, the XML application will be opened in two ways:
1. open the file according to the file encoding (better off)
2 open according to the default code of the application, for example, some Browsers open according to the default UTF-8.
Revelation: encoding must be added to the XML file.
XML Constraints
Currently, XML constraints are divided into two types: dtd and schema.
Dtd: the disadvantage is that it cannot accurately limit the data type.
Dtd category:
* Internal dtd: defines a dtd in xml.
* External dtd: defines a dtd in an external file.
* Local dtd file: <! DOCTYPE students SYSTEM "student. dtd">
* Network dtd file: <! DOCTYPE students PUBLIC "namespace" "student. dtd">
Dtd details see cainiao Tutorial: http://www.runoob.com/dtd/dtd-tutorial.html
Schema:
Import xsd constraints:
1. Compile the root tag
2. Introduce the instance namespace xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
3. Introduce namespace xsi: schemaLocation = "http://www.itcast.cn/xml student. xsd"
4. Introduce the default namespace
Schema details see cainiao Tutorial: http://www.runoob.com/schema/schema-tutorial.html
I don't know how to recognize Java EE. This is the code starting with web. xml IN THE JavaEE project that TMD cannot understand. Schema constraints are used here.
XML Parsing
Xml parsing ideology:
* DOM parsing: loads a document into the memory to form a dom tree (document Object). It encapsulates various components of the document into some objects.
* Advantage: Because the dom tree is formed in the memory, you can add, delete, modify, and query the dom tree.
* Disadvantage: the dom tree occupies a lot of memory and the parsing speed is slow.
* SAX parsing: Row-by-row reading, event-driven
* Advantage: no memory occupied, fast
* Disadvantage: it can only be read and cannot be written back.
Common Parser for xml:
* DOM4J:
1. Import the jar package dom4j. jar, the latest dom4j-1.6.1.jar
2. Create a parser
SAXReader reader = new SAXReader ();
3. parse xml to get the document Object
Document document = reader. read (url );
* XPATH: used for query
* Defines a rule.
* Usage:
* SelectSingleNode ():
* SelectNodes ():
Procedure:
1, Note: To export package dom4j-1.6.1.jar and jaxen-1.1-beta-6.jar two packages.
2. Create a parser
SAXReader reader = new SAXReader ();
3. parse xml to get the document Object
Document document = reader. read (url );