XML (extensible Markup Language)
The contents of a document can be given a meaning, and the machine is easy to handle such information
For data storage , information configuration . A. When you do a data exchange platform, you will be able to encapsulate data from the data source into an XML file. The XML file is then packaged and encrypted and transmitted over the network to the recipient, which is then decompressed and decrypted to process the relevant information in the restored XML. B. It is easy to make use of XML when making software configuration . The various configurations of the software are stored in the XML document.
One, Tool XMLSpy 2006
Second, the field of XML application
(1) Application of data conversion. XML becomes a specification that all kinds of programs can automatically understand and process data.
(2) Application of a large number of operations on the client, reducing server load. Clients can select and develop different applications to process data as needed
The server only needs to send the same XML file
(3) Use the same data to show different users in different faces. Data source and display separation.
(4) Application network The same XML file can be edited by multiple users, increase or decrease.
Third, DTD
A DTD (Date Type Define) that defines a legitimate XML document structure that uses a series of legitimate elements to define the structure of the document. and XML is a relationship between classes and objects.
Validates an XML document with a DTD.
DTDs are included in your XML source file
<! DOCTYPE Note [
<! ELEMENT Note (to,from,heading,body) >
<! ELEMENT to (#PCDATA) >
<! ELEMENT from (#PCDATA) >
<! ELEMENT heading (#PCDATA) >
<! ELEMENT Body (#PCDATA) >
]>
<note>
<to>George</to>
<from>John</from>
<body>don ' t forget the meeting!</body>
</note>
Iv. Schema
Use an XML schema to constrain an XML file. Includes a complete definition of elements, attributes, and data types, and special constraints on the elements, entities, attributes of elements, and the relationships between elements and entities that are used in an XML document. Limit the values of the age element and the height element. That is, the age must be between 26-18 years old. Height must be between 140.0cm and 190.0cm. The code is as follows: Using XML syntax
<xs:simpletype name= "Age" >
<xs:restriction base= "Xs:int" >
<xs:maxexclusive value= "></xs:maxExclusive>"
<xs:minexclusive value= "></xs:minExclusive>"
</xs:restriction>
</xs:simpleType>
<xs:simpletype name= "Length" >
<xs:restriction base= "Xs:float" >
<xs:maxexclusive value= "190.0" ></xs:maxExclusive>
<xs:minexclusive value= "140.0" ></xs:minExclusive>
</xs:restriction>
</xs:simpleType>
V. SAX (Simple API XML)
Sax is a standard interface for event-driven XML parsing.
How it works: sequentially scans the document, notifies the event handler function when the document starts and ends, the element begins and ends, the document ends, and so on, and then continues the same scan until the end of the document.
Advantages: Fast parsing, low memory consumption, suitable for large XML documents.
Disadvantages: Not random access, only sequential access. You can access only the contents of the document and you cannot modify it.
SAXParserFactory factory = Saxparserfactory.newinstance ();
SAXParser parser= Factory.newsaxparser ();
Parser.parse (Source,handler);
Source can be a file, and handler is a subclass of DefaultHandler.
VI. DOM
How it works: by parsing XML documents, we build a tree model logically for XML documents, and the nodes of the tree are objects. By accessing these objects, we can manipulate the contents of the XML document.
Advantages: XML Document information is put into memory, traversal is simple, support XPath can increase random access.
How to get a document
Documentbuilderfactory factory = Documentbuilderfactory.newinstance ();
Documentbuilder builder = Factory.newdocumentbuilder ();
File F = new file ("D:/a.xml");
Document doc = Builder.parse (f);
The four basic interfaces of the DOM (focus on mastery).
(1) Node interface
The parent of most other interfaces, such as document, Element, Attribute, Text, comment, and so on, are inherited from the node interface. That represents a single node in the document tree.
Main methods:
Node appendchild (node NewNode); Adds a node newchild to the end of the list of child nodes for this node.
Node Getfirstchild () The first child node of this node
Node Getlastchild () The last nodes of this node
GetAttributes ()
String Getnodevalue () The value of this node, depending on its type
Getparentnode () parent node of this node
The node Remove (node Oldchild) removes the child nodes indicated by Oldchild from the list of child nodes and returns them.
(2) Document interface
Represents an entire HTML or XML document. It refers not only to the root of the document, but also provides basic access to the document's data. The node interface is inherited.
Attribute CreateAttribute (String name) method: Creates a attr based on the name given
Element createelement (String TagName) method: Creates an element of the specified type.
Text createTextNode (String data) method: Creates a text node based on the given string data.
Element getElementById (String Element) method: Returns an element object named element.
NodeList getElementsByTagName (String tagname) method: Returns a list of all elements contained in a document and with the given tag name in document order.
(3) NodeList interface
The NodeList interface provides an abstraction of an ordered set of nodes.
int GetLength () method: Number of nodes in the list.
node item (int index) method: Returns the nodes in the collection that are indexed to index. Returns NULL if index is greater than or equal to the number of nodes in this list.
Seven, XPATH