Basic XML knowledge

Source: Internet
Author: User
Tags cdata xml parser xml stylesheet

1. xml Basics
2. XML syntax
3. XML DOM
4. parse XML documents using JavaScript


1. xml Basics
Extensible Markup Language
XML is a platform-independent method for carrying and transmitting data.

~~~~ XML mainly describes what data is. Generally, there is no information about how data is presented. Unlike HTML, HTML is an implementation of XML. (Note: XHTML is an implementation of XML)

You can use the IE text editor or a special XML editor to browse XML documents.

~~~ XML data exchange on different platforms, but not suitable for storing and processing large batches of data (different from databases)

You can also define the primary/foreign key relationships between elements of multiple XML documents.

XML advantages:
~~~ Versatility, scalability, self-descriptive

Use of XML
~~ It is used as a common data format to store and exchange data. It is used as a configuration file.


2. XML syntax

XML Document declaration
XML processing commands
XML Element (element attribute element content)

<? XML version = "1.0" encoding = "UTF-8"?> <! -- XML Document declaration -->
<? XML-stylesheet type = "text/XSL" href = "mystyle. XSL"?> <! -- XML Processing Command (optional) -->
<Stu-roster> <! -- XML element -->
<Student>
<ID> a001 </ID>
<Name> dingdang </Name>
</Student>
<Student>
<ID> a009 </ID>
<Name> Kitty </Name>
<Student>
</STU-roster>

~~~ The XML Document declaration must contain the version attribute. Other attributes cannot be in the following order.
<? XML version = "1.0" encoding = "UTF-8" standalone = "yes"?>

XML processing commands
Provides prompt information for applications processing XML documents.
The XML parser will pass the processing commands intact to the processing program, which processes the application according to the instructions.

Display XML documents in the format
Use CSS to define the appearance style or use XSL to define the appearance. XML Stylesheet Language is more powerful.

XML element: Start ID, end tag, attribute content
<Title> foomu orphan </title>

All XML elements must have an end mark.

The tag name is case sensitive. The start tag and end tag must match.

Element Content:
Element contains child element
Element directly contains content
The element does not have any blank content mark

Root element: the highest level element of the element that is not nested in other elements
All elements in the document are child elements of the root element.
The root element must have only one

Element Content
CDATA character data
Specifies a raw data zone that the parser does not want to parse.

Pcdata parsed character data parsed by the XML Parser

CDATA example:
<! [CDATA [... anything can go here...]>
The CDATA zone cannot contain characters "]>"
Code in other languages, such as JavaScript, can be embedded in the CDATA zone.

Pcdata: The character data to be parsed by the XML parser.
Directly write the content between the start tag and the end tag.

Character escape
Some special characters cannot appear in the pcdata area and must be replaced by character entities, such as <&
& Lt <
& Gt>
& Amp & ampersand
& APO 'apostrophe
& Quot"

XML annotation is the same as HTML <! -- Comment -->

The XML Document declaration must be the first line of code, and the annotation cannot be placed before the XML Document declaration.

3. XML DOM

Dom is a standard supported by all browsers. It defines a set of standard objects irrelevant to browsers and programming languages. Using these object models, you can easily operate XML documents.

Dom is the most popular method for accessing XML documents.

Advantages and disadvantages of DOM: when Dom is used, all XML document information is stored in the memory, which is easy to traverse, but the memory usage is high and the parsing is slow.

Each component of the XML document in the DOM tree structure is considered as a node.

Node Type:
Document Root Node 9
The representation of the object referenced by the documenttype DTD 10
Element Node 1
ATTR attribute node 2
Text Node 3
Cdatasection character data node 4
Comment comment node 8

Relationship between nodes in the DOM tree structure: Parent Child sibling

4. parse XML documents using JavaScript
Xml dom attributes and Methods
Javascript manipulate XML DOM

Currently, most browsers support XML

Both IE and Mozilla provide XML support through JavaScript.

The basis for JavaScript to process XML documents is the DOM tree structure.
There are Dom APIs in Javascript, which define Dom attributes and methods.
You can use Dom APIs in JavaScript to access, create, delete, and modify XML documents.
Processing XML documents with JavaScript is the core of Ajax technology

How JavaScript processes XML documents:
1. Create an xml dom object
2. Load the content of the XML document
3. Call JavaScript to process xml dom attributes and methods.

Creating xml dom objects using IE and non-ie browsers is different
Ie uses the MSXML library in ActiveX plug-in to support XML
VaR xmldom = new activexobject (aversion );
The aversion value can be Microsoft. xmldom.
Msxml2.domdocument
Msxml2.domdocument3.0
Msxml2.domdocument4.0
Msxml2.domdocument5.0
Msxml2.domdocument6.0

For example:
VaR xmldom = new activexobject ("msxml2.domdocument6.0 ");

IE supports loading XML documents in 2: load () and loadxml ()
VaR xmldoc = xmldom. Load (URL); // XML document data is loaded asynchronously by default.

// Load XML document data in synchronous Mode
Xmldom. async = false;
VaR xmldoc = xmldom. Load (URL );

VaR xmldoc = xmldom. loadxml (sxml); // sxml is a string in XML format.


Get xml dom objects in Firefox
In Firefox, you must call the createdocument () method of the document. inplementation object to create an xml dom object.
VaR xmldom = Document. inplementation. createdocument ("", "", null );

Firefox only supports loading XML documents using the load () method.
VaR xmldoc = xmldom. Load ("test. xml ");

Get xml dom objects-cross-browser compatibility method. Use the zxml library to support XML across browsers.

VaR xmldom = zxmldom. createdocument ();

Zxml supports loading XML documents in 2, which is the same as IE. xmldom. Load () and xmldom. loadxml ();


>>>> XML document Operations <
Node attributes:
Nodetype
Nodename
Nodevalue
Attribute of the attributes Element Node

Node navigation
Firstchild
Lastchild
Childnodes
Previussibling
Nextsibling

Documentelement points to the root node of the XML document. Only the document object has this attribute. Document.doc umentelement
Owerdocument points to the document of this node.

Xml dom method:
Create a node:
Createelement (tagname) creates an element named tagname.
Createtexnode (text) to create a text node
Createattribute (attrname) creates an attribute. node attributes are not assigned a value.

Createdocumentfragment () create a document fragment Node
Createprocessinginstruction (target, data );

Read node:
Getelementbytagname (tagname) // return the nodelist with the tagname
Getattribute (attrname) // get the attribute value attrname in the current node

Setattribute (attrname, attrvalue) sets the attribute node Value
Setattributenode (attrnode) adds an attribute node to the end of a node attribute node list.

Add or delete a node:
Appendchild (node );
Removechild (node)
ReplaceChild (newnode, oldnode)
Insertbefore (newnode, oldnode );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.