Method 1:
Copy codeThe Code is as follows:
<? Php
Header ("Content-type: text/xml ");
Echo "<? Xml version =/"1.0/" encoding =/"UTF-8/"?> ";
Echo "<users> ";
Echo "<user> ";
Echo "<name> ";
Echo "";
Echo "</name> ";
Echo "<age> ";
Echo "24 ";
Echo "</age> ";
Echo "<sex> ";
Echo "male ";
Echo "</sex> ";
Echo "</user> ";
Echo "<user> ";
Echo "<name> ";
Echo "Yan ";
Echo "</name> ";
Echo "<age> ";
Echo "23 ";
Echo "</age> ";
Echo "<sex> ";
Echo "female ";
Echo "</sex> ";
Echo "</user> ";
Echo "</users> ";
?>
Method 2:
Copy codeThe Code is as follows:
<? Php
Header ("Content-type: text/xml ");
Echo "<? Xml version =/"1.0/" encoding =/"UTF-8/"?> ";
Echo "<users> <user> <name> ainiao </name> <age> 24 </age> <sex> male </sex> </user> <user> <name> Yan </name> <age> 23 </age> <sex> female </sex> </user> </users> ";
?>
Method 3:
Copy codeThe Code is as follows:
<? Php
/*
Use the php dom control to create XML output
Set the output content type to xml
*/
Header ('content-Type: text/xml ;');
// Create a New xml file
$ Dom = new DOMDocument ('1. 0', 'utf-8 ');
// Create the <response> element
$ Response = $ dom-> createElement ('response ');
$ Dom-> appendChild ($ response );
// Create the <books> element and use it as the sub-element of <response>
$ Books = $ dom-> createElement ('books ');
$ Response-> appendChild ($ books );
// Create a title for the book
$ Title = $ dom-> createElement ('title ');
$ TitleText = $ dom-> createTextNode ('php and AJAX ');
$ Title-> appendChild ($ titleText );
// Create an isbn element for the book
$ Isbn = $ dom-> createElement ('isbn ');
$ IsbnText = $ dom-> createTextNode ('1-21258986 ');
$ Isbn-> appendChild ($ isbnText );
// Create a book Element
$ Book = $ dom-> createElement ('book ');
$ Book-> appendChild ($ title );
$ Book-> appendChild ($ isbn );
// Use <book> as a sub-element of <books>
$ Books-> appendChild ($ book );
// Create an XML structure in a string variable
$ XmlString = $ dom-> saveXML ();
// Output an XML string
Echo $ xmlString;
?>