Introduction to XML

Source: Internet
Author: User

The main differences between XML and HTML

XML is not an alternative to HTML.

XML and HTML are designed for different purposes:

XML is designed to transmit and store data, with the focus on the content of the data.

HTML is designed to display data with the focus on the appearance of the data.

HTML is designed to display information, while XML is designed to transmit information.

XML is not acted on (XML without any behavior)

Maybe it's a little hard to understand, but XML doesn't do anything. XML is designed to structure, store, and transmit information.

Here is John's note to George, stored as XML:

<note><to>George</to><from >john</from >'t forget the meeting!</body></note>

ML Grammar Rules

    • Previous Page
    • Next Page

The syntax rules for XML are simple and logical. These rules are easy to learn and easy to use.

All XML elements must have a close tag

In HTML, you will often see elements without closing tags:

<p>this is a paragraph

<p>this is another paragraph

In XML, it is illegal to omit the close tag. All elements must have a close tag:

<p>this is a paragraph</p>

<p>this is another paragraph</p>

Note: You may have noticed that the XML declaration did not close the label. This is not a mistake. Declarations are not part of the XML itself. It is not an XML element, and you do not need to close the label.

The XML tag is case sensitive

XML elements are defined using XML tags.

The XML tag is case sensitive. In XML, label <Letter> is different from tag <letter>.

You must use the same casing to write open tags and close labels:

<Message> this is wrong. </message>

<message> that's right. </message>

Note: Opening labels and closing labels are often referred to as start and end tags. Whatever terminology you prefer, the concepts are the same.

XML must be nested correctly

In HTML, you often see elements that are not nested correctly:

<b><i>this text is bold and italic</b></i>

In XML, all elements must be nested correctly with each other:

<b><i>this text is bold and italic</i></b>

In the example above, the proper nesting means that because the <i> element is open within the <b> element, it must be closed within the <b> element.

The XML document must have a root element

An XML document must have an element that is the parent element of all other elements. This element is called the root element.

<root>

<child>

<subchild>.....</subchild>

</child>

</root>

Attribute values for XML must be quoted

Like HTML, XML can also have attributes (name/value pairs).

In XML, the attribute value of XML should be quoted. Please study the following two XML documents. The first one is wrong and the second one is correct:

<note date=08/08/2008>

<to>George</to>

<from>John</from>

</note>

<note date= "08/08/2008" >

<to>George</to>

<from>John</from>

</note>

The error in the first document is that the Date property in the note element is not quoted.

Entity references (<> ' "&)

In XML, some characters have a special meaning.

If you put the character "<" in an XML element, an error occurs because the parser takes it as the beginning of the new element.

This produces an XML error:

<message>if Salary < Then</message>

To avoid this error, use an entity reference instead of the "<" character:

<message>if Salary &lt; Then</message>

In XML, there are 5 predefined entity references:

&LT;

<&NBSP;

less than

&GT;

>&NBSP;

&amp;

&

and number

&APOS;

single quote

&quot;

quotes

Note: In XML, only the characters "<" and "&" are really illegal. The greater than sign is legal, but it is a good practice to replace it with an entity reference.

Annotations in XML

The syntax for writing comments in XML is similar to the syntax of HTML:

<!--This is a comment-

Spaces in the XML are preserved

HTML will cut multiple consecutive whitespace characters into one:

Html:hello my name is David.

Output: Hello My name is David.

In XML, spaces in a document are not truncated.

XML wraps a line in the LF store

In a Windows application, a newline is typically stored in a pair of characters: a carriage return (CR) and a newline character (LF). This pair of characters is similar to the action of a typewriter setting a new line. In Unix applications, new lines are stored as LF characters. The Macintosh application uses CR to store new rows.

XML Naming conventions

XML elements must follow the following naming conventions:

    • · Names can contain letters, numbers, and other characters
    • · Names cannot start with numbers or punctuation marks
    • · The name cannot start with the character "XML" (or XML, XML)
    • · Name cannot contain spaces

You can use any name without a reserved word.

<! DOCTYPE HTML Public"-//W3C//DTD XHTML 1.0 transitional//en" "HTTP://WWW.W3.ORG/TR/XHTML1/DTD/XHTML1-TRANSITIONAL.DTD">"http://www.w3.org/1999/xhtml">"Content-type"Content="text/html; Charset=utf-8"/><title> Untitled Document </title><script src=".. /jquery-1.11.2.min.js"></script>"AA"></div></body><script type="Text/javascript">$ (document). Ready (function (e) {/*$.ajax ({url: "chuli.php", DataType: "XML", success:function (data) {///takes the subset element below the root,                Take out an array of var CD = $ (data). Find ("Nation"). Children ();                var str = "";                    for (var i=0; i<cd.length;i++) {var code = CD.EQ (i). Find ("code"). Text ();                    var name = Cd.eq (i). Find ("name"). Text ();                str = str+ "<div>" +code+ "--" +name+ "</div>";            } $ ("#aa"). html (str); }        });*/$.ajax ({URL:"chuli.php", DataType:"XML", Success:function (data) {alert (data). Find ("Shuju"). attr ("name")); }        });});</script>

Working with pages

<?Phpinclude (".. /dbda.php"); $db=NewDbda ();//$sql = "SELECT * from Nation";$sql ="SELECT * from Info where code= ' P001 '"; $attr= $dbQuery ($sql); Echo"<nation>";/*foreach ($attr as $k = $v) {echo "<n{$k}> <code>{$v [0]}</code> <name>{$v [1]}</nam e> </n{$k}> ";}*/Echo"<shuju code= ' {$attr [0][0]} ' name= ' {$attr [0][1]} ' nation= ' {$attr [0][3]} ' ></shuju>";Echo"</nation>";

Introduction to XML

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.