<bookstore><book category="CHILDREN"><title>Harry Potter</title><author>J K. Rowling</author><year>2005</year><price>29.99</price></book ><book category="WEB"><title>Learning XML</title><author>Erik T. Ray</author><year>2003</year><price>39.95</price></book>< /bookstore>
In the above example,
<bookstore>
and
<book>
All have element content because they contain other elements.
<book>
Elements also have attributes (category="CHILDREN").
<title>,<author>,<year>
and <price> have text content because they contain text.
XML elements must follow the following naming rules:
Names can contain letters, numbers, and other characters
The name cannot start with a number or punctuation mark
The name cannot start with the letters xml (or XML, Xml, etc.)
Name cannot contain spaces
Any name can be used, no reserved words.
Make the name descriptive. It's also a good idea to use underscores in names:
<first_name>, <last_name>.
Names should be short and simple, such as:
<book_title>
, instead of:
<the_title_of_the_book>
avoid"
-
" character. If you name it like this: "
first-name
", some software will think you want to start from
first
subtract from it
name
avoid"
.
" character. If you name it like this: "
first.name
", some software will think"
name
"is an object"
first
" attribute. Avoid the "" character. The colon will be converted to a namespace for use (described later).
XML documents often have a corresponding database, with fields corresponding to elements in the XML document. A practical rule of thumb is to use the naming rules of the database to name the elements in the XML document.
In XML,
éòá
etc. non-English letters are perfectly legal, but be aware of problems that may arise if your software provider does not support these characters.
XML elements are extensible to carry more information.
Please see the following XML example:
<note><to>Xiao Ming</to><from>Little Lion Girl</from><body>Don’t forget to learn programming on weekends~</body></note>
Let's imagine that we create an application that
<to>
,
<from>
as well as
<body>
Elements are extracted from the XML document and produce the following output:
MESSAGE To: Xiao MingFrom: Little Lion Girl Don’t forget to learn programming on the weekend~ |
Imagine that the author of the XML document adds some extra information:
<note><date>2020-09-09</date><to>Xiao Ming</to><from>Little Lion Girl</from><heading>Reminder</heading><body>Don’t forget to learn programming on weekends ~</body></note>
So will this app break or crash?
Won't. This application can still find the
<to>
,
<from>
as well as
<body>
element, and produces the same output.
One of the advantages of XML is that it can be extended without disrupting the application.
The above is all about XML elements. You need to pay attention to one thing: in XML, all elements must have a closing tag!