w3pop.com :: ÍøÂçѧԺ :: XML :: xml ÎĵµÊ÷
XML documents form a tree structure that starts at "the root" and branches to "the leaves".
XML ÎĵµÊ÷ÆðʼÓÚ“¸ùÔªËØ”£¬²¢ÒÔ´ËΪ»ù´¡À©Õ¹ÎĵµµÄ·ÖÖ§½á¹¹¡£
XML documents use a self-describing and simple syntax:
XML ÎĵµÊ¹ÓÃÒ»ÖÖÏà¶Ô¼òµ¥µÄ×ÔÊöÐÔÓï·¨£º
<?xml version="1.0" encoding="ISO-8859-1"?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> |
The first line is the XML declaration. It defines the XML version (1.0) and the encoding used (ISO-8859-1 = Latin-1/West European character set).
´úÂëµÄµÚÒ»ÐжÔXMLÎĵµ×ö³öÁËÉùÃ÷¡£Ëü¶¨ÒåÁËXML°æ±¾ºÅ£¨1.0£©ÒÔ¼°ÎĵµËùʹÓõÄ×Ö·û±àÂ루ISO-8859-1£ºÀ¶¡ÎÄ/Î÷Å·×Ö·û¼¯£©¡£
The next line describes the root element of the document (like saying: "this document is a note"):
½ÓÏÂÀ´µÄÒ»Ðж¨ÒåÁËÎĵµµÄ¸ùÔªËØ£¨Ö¸Ã÷¸ÃÎĵµÊÇÒ»·Ý±ãÌõ£©£º
<note> |
The next 4 lines describe 4 child elements of the root (to, from, heading, and body):
ÔÙ½ÓÏÂÈ¥µÄ4Ðж¨ÒåÁ˸ùÔªËØµÄ4¸ö×ÓÔªËØ£¬·Ö±ðÊÇ“to”¡¢“form”¡¢“heading”ºÍ“body”£º
<to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> |
And finally the last line defines the end of the root element:
ÎĵµµÄ×îºóÒ»Ðж¨ÒåÁ˸ùÔªËØµÄ½áÊø±êÇ©£º
</note> |
You can assume, from this example, that the XML document contains a note to Tove from Jani.
¾Ù¸öÀý×ÓÀ´Ëµ£¬Äã¿ÉÒÔ¼ÙÉèÕâ·ÝXMLÎĵµÊÇJaniµÝ½»¸øToveµÄÒ»ÕűãÌõ¡£
Don't you agree that XML is pretty self-descriptive?
ͨ¹ýÉÏÊö°¸Àý£¬¶ÔÓÚXMLÊÇÒ»ÖÖÍêÃÀµÄ×ÔÊöÐÔÓïÑÔÕâµãÓ¦¸ÃÎãÓ¹ÖÃÒÉÁ˰ɣ¿
XML documents must contain a root element. This element is "the parent" of all other elements.
XML Îĵµ±ØÐë°üº¬Ò»¸ö¸ùÔªËØ¡£Õâ¸ö¸ùÔªËØÊÇÆäËüËùÓÐÔªËØµÄ“¸¸ÔªËØ”¡£
The elements in an XML document form a document tree. The tree starts at the root and branches to the lowest level of the tree.
ÕâÐ©ÔªËØÎ»ÓÚXMLÊ÷×´½á¹¹ÎĵµÄÚ¡£Ê÷×´½á¹¹ÆðʼÓÚ¸ùÔªËØ£¬²¢Ïò¸üµÍ¼¶±ðµÄÊ÷×´·ÖÖ§½á¹¹À©Õ¹¡£
All elements can have sub elements (child elements):
ÎĵµÖÐËùÓеÄÔªËØ¶¼¿ÉÒÔ°üº¬¶þ¼¶ÔªËØ£¨¼´£º×ÓÔªËØ£©£º
<root>
<child>
<subchild>.....</subchild>
</child>
</root>
|
The terms parent, child, and sibling are used to describe the relationships between elements. Parent elements have children. Children on the same level are called siblings (brothers or sisters).
ÕâÀïÌáµ½µÄһЩÊõÓÈ磺¸¸ÔªËØ¡¢×ÓÔªËØ¡¢Í¬¼¶ÔªËØÊ¹ÓÃÓëÃèÊöÔªËØÖ®¼äµÄÏ໥¹ØÏµµÄ¡£¸¸ÔªËذüº¬×ÓÔªËØ£»ºÍ×ÓÔªËØÍ¬¼¶µÄ³ÆÎªÍ¬¼¶ÔªËØ£¨»òÐֵܡ¢½ãÃÃÔªËØ£©¡£
All elements can have text content and attributes (just like in HTML).
ËùÓеÄÔªËØ¶¼°üº¬Îı¾ÄÚÈݺÍÊôÐÔ£¨ÕâµãºÍHTML¼«ÆäÀàËÆ£©¡£
The image above represents one book in the XML below:
ÉÏÊöͼ±í´ú±íÁËÏÂÊöXMLÎĵµÖеÄÒ»±¾Ê飺
<bookstore> <book category="COOKING"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="CHILDREN"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="WEB"> <title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore> |
The root element in the example is <bookstore>. All <book> elements in the document are contained within <bookstore>.
°¸ÀýÖеĸùÔªËØÊÇ<bookstore>¡£ÎĵµÄÚËùÓеÄ<book>ÔªËØ¶¼Î»ÓÚ<bookstore>ÄÚ¡£
The <book> element has 4 children: <title>,< author>, <year>, <price>.
Óë´Ëͬʱ£¬<book>ÔªËØ»¹°üº¬ÁË4¸ö×ÓÔªËØ£º<title>¡¢< author>¡¢<year>¡¢<price>¡£