PHP output XML to the page 3 ways to explain _php skills

Source: Internet
Author: User

The first method:

Copy Code code as follows:

<?php
Header ("Content-type:text/xml");
echo "<?xml version=/" 1.0/"encoding=/" utf-8/"?>";
echo "<users>";
echo "<user>";
echo "<name>";
echo "Little rookie";
echo "</name>";
echo "<age>";
echo "24";
echo "</age>";
echo "<sex>";
echo "Male";
echo "</sex>";
echo "</user>";
echo "<user>";
echo "<name>";
echo "Yan Yan";
echo "</name>";
echo "<age>";
echo "23";
echo "</age>";
echo "<sex>";
echo "female";
echo "</sex>";
echo "</user>";
echo "</users>";
?>

The second method:
Copy Code code as follows:

<?php
Header ("Content-type:text/xml");
echo "<?xml version=/" 1.0/"encoding=/" utf-8/"?>";
echo "<users><user><name> Little rookie </name><age>24</age><sex> man </sex> </user><user><name> Yan Yan </name><age>23</age><sex> female </sex></user ></users> ";
?>

The third method:
Copy Code code as follows:

<?php
/*
Using PHP's DOM controls to create XML output
To set the type of output as XML
*/
Header (' content-type:text/xml; ');
Create a new XML file
$dom = new DOMDocument (' 1.0 ', ' utf-8 ');

Building <response> Elements
$response = $dom->createelement (' response ');
$dom->appendchild ($response);

Create <books> elements and take them as child elements of <response>
$books = $dom->createelement (' books ');
$response->appendchild ($books);

Create a title for book
$title = $dom->createelement (' title ');
$titleText = $dom->createtextnode (' PHP and Ajax ');
$title->appendchild ($titleText);

Create an ISBN element for book
$ISBN = $dom->createelement (' ISBN ');
$isbnText = $dom->createtextnode (' 1-21258986 ');
$ISBN->appendchild ($isbnText);

Create book Element
$book = $dom->createelement (' book ');
$book->appendchild ($title);
$book->appendchild ($ISBN);

To <book> as a <books> child element
$books->appendchild ($book);

To create an XML structure in a string variable
$xmlString = $dom->savexml ();

Output XML string
Echo $xmlString;

?>


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.