XML for Beginne...LOGIN

XML for Beginners to PHP

1.What is xml

XML is the abbreviation of Extensible Markup Language (Extensible Markup Language), of which markup is the key part. You can create content and then mark it with qualifying tags, making each word, phrase, or chunk of information a recognizable, classifiable piece of information. The file, or document instance, you create is made up of elements (tags) and content. Elements can help to better understand a document when it is read from a printout or processed electronically. The more descriptive an element is, the easier it is to identify parts of the document. Since the emergence of tags, tagged content has an advantage, that is, when the computer system is missing, the printed data can still be understood through tags

2.Building xml

As mentioned earlier, XML files consist of content and markup. You contain most of your content within an element by surrounding it with markup. For example, suppose you need to create an XML cookbook. You need to write a recipe called Ice Cream Sundae in XML. In order to mark the recipe name, you need to include this text into the element, that is, add opening and closing tags at the beginning and end of the text. Elements can be named recipename. To mark the opening tag of an element, enclose the element name in angle brackets (<>) like this: <recipename>. Then enter the text Ice Cream Sundae. Enter a closing tag after the text, that is, put the element name within angle brackets, and then add a terminating slash (/) in front of the element name, for example: </recipename>. These tags form an element inside of which you can add content or other elements.

You can create element names for a document or document set. Rules can be created to combine elements based on your specific needs. Element names can be more specific or more general. You can also create rules that determine what elements to add. These rules can be strict or loose, it's entirely up to you. Be sure to create elements for your document to identify the parts you think are important.

3.Create xml

The first line of an XML document can be an XML declaration. This is an optional part of the file that identifies the file as an XML file, helping tools and humans recognize XML (without being mistaken for SGML or other markup). This declaration can be simply written as <?xml?>, or include the XML version (<?xml version="1.0"?>), or even the character encoding, such as <?xml version=" for Unicode 1.0" encoding="utf-8"?>. Because this declaration must appear at the beginning of the file, you can omit this optional information if you plan to merge multiple small XML files into one large XML file

4.Create root element

The opening and closing tags of the root element are used to surround the content of the XML document. A file can only have one root element, and a "wrapper" needs to be used to contain it. Listing 1 shows an abridged example where the root element is named <recipe>. (See the Download section for the complete XML file).

5.Named elements

So far, <recipe> has been used as the root element. In XML, you choose names for elements and then define the corresponding DTD or schema based on those names. You can use English letters, numbers, and special characters such as underscore (_) when creating a name. The following is what you need to pay attention to when naming:

· There cannot be spaces in the element name.

· The name can only start with English letters, not numbers or symbols. (After the first letter, letters, numbers, or prescribed symbols may be used, or a mixture thereof).

· There is no restriction on uppercase and lowercase letters, but they must be consistent to avoid confusion.

Continuing with the previous example, if an element named <recipename> is added, it will have a start tag <recipename> and a corresponding end tag </recipename>.

6.Nested elements

Nesting means placing an element inside other elements. These new elements are called child elements, and the element that contains them is called the parent element. The <recipe> root element has several other elements nested within it, as shown in Listing 3. These nested child elements include <recipename>, <ingredlist>, and <preptime>. The <ingredlist> element contains multiple child elements <listitem>. XML documents can use multiple levels of nesting.

A common syntax error is the incorrect nesting of parent and child elements. Any child element must be completely contained within the opening and closing tags of its parent element. Each sibling element must end before the next sibling element begins

<?xml version="1.0" encoding="UTF-8"?>
<recipe>
<recipename>Ice Cream Sundae</recipename>
<ingredlist>
<listitem>
<quantity>3</quantity>
<itemdescription>chocolate syrup or chocolate fudge</itemdescription>
</listitem>
<listitem>
<quantity>1</quantity>
<itemdescription>nuts</itemdescription>
</listitem>
<listitem>
<quantity>1</quantity>
<itemdescription>cherry</itemdescription>
</listitem>
</ingredlist>
<preptime>5 minutes</preptime>
</recipe>

7.xml file with elements and attributes

<?xml version="1.0" encoding="UTF-8"?>
<recipe type="dessert">
<recipename cuisine="american" servings="1">Ice Cream Sundae</recipename>
<preptime>5 minutes</preptime>
</recipe>


Next Section
<?php echo "欢迎学习xml"; ?>
submitReset Code
ChapterCourseware