PHP generates XML instance and basic knowledge

Source: Internet
Author: User
Tags addchild closing tag

1, xml,extensible Markup Language (Extensible Markup Language) is a markup language that can create custom tag symbols (tags), a standard text format used to represent structured information, as a standard for next-generation data interchange formats. Like HTML, XML is based on the standard Universal Markup Language (Standard generalized Markup language,sgml). Document structure, the level is very clear. An XML document simply stores data, is a regular structured document, but does not define how to display the data. The document structure can be called the document tree, and the backbone is the parent element at the top, and the branches and leaves are child elements.

XML declaration, which indicates that the document follows the specification of XML version 1.0
2, XML features:
Scalability, XML allows users to create and use their own tags rather than the limited HTML tag, enterprises can use XML to define their own markup language for E-commerce and supply chain integration, and even specific industries to define special markup language in this field, as the basis of information sharing and data exchange in this field. However, these custom tags are only used to identify information data, and it does not convey information in itself.
Flexibility, XML is more like a small database, providing a structured way of data representation that separates the user interface from structured data.
Self-describing, the way XML represents data is truly independent of the application system, and the data can be reused, and XML documents are considered to be database of documents and document of data.
3, XML and HTML differences
HTML is a stereotyped markup language that is described with intrinsic markup that displays the content of the Web page. XML is not a fixed tag, can not describe the specific appearance of the Web page, content, it is just describing the content of the data form and structure. The Web page mixes the data with the display, while XML separates the data from the display.
XML Document Considerations
All tags must have a corresponding closing tag, the tags must be reasonably nested, the markup is case-sensitive, and the marked attributes must be enclosed in "".
Tag naming rules: Names can contain letters, numbers, underscores, and other letters, cannot begin with numbers or underscores, names cannot be in XML (or XML or XML ...). The beginning, the name can not contain spaces, the name can not use the ".", "." is used to connect elements and attributes.
PS: Support Chinese characters as tag names, pay attention to coding. File encoding is consistent with XML declaration encoding
In XML, there are 5 predefined entity references:

&lt; < less than
&gt;> Greater than
&amp; & and Number
&apos; ' Single quotes
&quot; "Double quotes

XML file generation Operation Simple Example

The code is as follows Copy Code

<?php
Header ("Content-type:application/xml;charset=utf-8");
Header ("Cache-control:no-cache,must-revalidate");
Header ("Expries:fri, Mar 1980 20:00:00 GMT");
Header ("last-modified:". Date (' R '));
Header ("Prama:no-cache");
$xml = ' <?xml version= ' 1.0 ' encoding= ' utf-8 '?>
<rss></rss> ';
$sxe =new simplexmlelement ($xml);
if (method_exists ($sxe, ' addChild ')) {
$sxe->addattribute (' Version ', ' 2.0 ');
$channel = $sxe->addchild (' channel ');
$channel->addchild ("title", "Clothing wholesale");
For ($i =0 $i <5; $i + +) {
$item = $channel->addchild (' item ');
$item->addchild (' title ', ' Commodity '. $i);
$item->addchild (' link ', $i);
}
echo $sxe->asxml ();
}else{
$dom =new DOMDocument ("1.0");//Generate a new DOM object
$rss = $dom->createelement ("RSS");//Generate RSS Element
$rss->setattribute ("version", "2.0");
$rss = $dom->appendchild ($RSS);

$channel = $dom->createelement ("channel");
$channel = $rss->appendchild ($channel);

$title = $dom->createelement (' title ');
$text = $dom->createtextnode ("Clothing wholesale");
$title->appendchild ($text);
$title = $channel->appendchild ($title);

For ($i =0 $i <5; $i + +) {
$item = $dom->createelement ("item");
$item = $channel->appendchild ($item);

$title = $dom->createelement ("title");
$text = $dom->createtextnode ("commodity". $i);
$title->appendchild ($text);
$title = $item->appendchild ($title);

$link = $dom->createelement ("link");
$text = $dom->createtextnode ("$i");
$link->appendchild ($text);
$link = $item->appendchild ($link);
}
echo $dom->savexml ();
$dom->save ("Nnn.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.