The first line is the XML declaration. It defines the XML version (1.0) and the encoding used (ISO-8859-1 = Latin-1/Western European character set ).
<To> George </to> <from> JOHN </from>
The last line defines the end of the root element:
</Note>
In this example, we can imagine that the XML document contains a signature John gave George.
XML has excellent self-description. Do you agree?
XML documents form a Tree Structure
The XML document must containRoot element. This element is the parent element of all other elements.
The elements in the XML document form a document tree. The tree starts from the root and expands to the bottom of the tree.
All elements can have child elements:
<Root> <child> <subchild>... </subchild> </child> </root>
Terminologies such as parent, child, and compatriot are used to describe the relationship between elements. The parent element has child elements. Child elements at the same level become siblings ).
All elements can have text content and attributes (similar to HTML ).
Instance
Represents a book in the following 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>
In this example, the root element is <bookstore>. All <book> elements in the document are included in <bookstore>.
The <book> element has four child elements: <title>, <author>, <year>, and <price>.
XML syntax rules
XML syntax rules are simple and logical. These rules are easy to learn and use.
All XML elements must have close labels
In HTML, you will often see elements that do not close tags:
<P> This is a paragraph <p> This is another paragraph
In XML, it is invalid to omit and close tags. All elementsRequiredThere are closed labels:
<P> This is a paragraph </P> <p> This is another paragraph </P>
Note: You may have noticed that the XML declaration does not close the tag. This is not an error. Declarations are not part of XML. It is not an XML Element and does not need to close tags.
XML tags are case sensitive.
XML elements are defined using XML tags.
XML tags are case sensitive. In XML, labels <letter> and <letter> are different.
You must use the same case to write the Enable and disable tags:
<Message> This is incorrect. </Message> <message> This is correct. </Message>
Note: Open and Close tags are usually called start and end tags. No matter which term you like, they share the same concept.
XML must be correctly nested
In HTML, you will often see elements that are not correctly nested:
<B> <I> This text is bold and italic </B> </I>
In XML, all elements areRequiredCorrectly nest each other:
<B> <I> This text is bold and italic </I> </B>
In the preceding example, correct nesting means that the <I> element is opened in the <B> element, so it must be closed in the <B> element.
XML documents must have root elements
An XML document must have one element that belongs to all other elements.Parent Element. This element is calledRoot element.
<Root> <child> <subchild>... </subchild> </child> </root>
The XML property value must be enclosed in quotation marks.
Similar to HTML, XML can also have attributes (name/value pairs ).
In XML, XML Attribute values must be enclosed by quotation marks. Study the following two XML documents. The first is incorrect, and the second 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 attribute in the note element is not enclosed by quotation marks.
Entity reference
In XML, some characters have special meanings.
If you put the character "<" in an XML element, an error occurs because the parser starts to treat it as a new element.
XML errors will occur:
<Message> If salary <1000 then </message>
To avoid this error, useEntity referenceTo replace the "<" character:
<Message> If salary& Lt;
1000 then </message>
In XML, there are 5 predefined entity references: