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;
?>