Summary of methods for PHP output of XML format data, php output of xml format

Source: Internet
Author: User

Summary of methods for PHP output of XML format data, php output of xml format

This example describes how PHP outputs XML format data. We will share this with you for your reference. The details are as follows:

Method 1:

<? Phpheader ("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 ""; echo "</name> "; echo "<age>"; echo "23"; echo "</age>"; echo "<sex>"; echo "female"; echo "</sex> "; echo "</user>"; echo "</users>";?>

The preceding example is run as follows:

Method 2:

<? Phpheader ("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:

<? Phpheader ('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-> a ($ response ); // create the <books> element and use it as the <response> sub-element $ books = $ dom-> createElement ('books '); $ response-> a ($ books); // create a title for the book $ title = $ dom-> createElement ('title '); $ titleText = $ dom-> createTextNode ('php and AJAX '); $ title-> a ($ titleText); // create an isbn element for the book $ isbn = $ dom -> CreateElement ('isbn '); $ isbnText = $ dom-> createTextNode ('1-21258986'); $ isbn-> a ($ isbnText ); // create the book element $ book = $ dom-> createElement ('book'); $ book-> a ($ title); $ book-> a ($ isbn ); // use <book> as the <books> sub-element $ books-> a ($ book ); // create an XML structure in a string variable $ xmlString = $ dom-> saveXML (); // output the XML string echo $ xmlString;?>

1. php output xml instance

header('Content-Type: text/xml');$sql = "select * from article order by post_date limit 100";$re = $db->query($sql);echo "<?xml version="1.0" encoding="utf-8"?>\n";echo "<data>\n";while($row = $db->fetchArray($re)){echo "<event ";echo "start="".strftime ("%b %d %Y %H:%M:%S", $row['post_date'])."" ";echo "title="".htmlspecialchars($row['title'])."" ";echo "link="tdetail.php?id=".$row['time_point'].""";echo ">";echo htmlspecialchars($row['content']);echo "</event>\n";}echo "</data>";

2. Use JavaScript to operate XML instances

Try to use all the operations and attributes for demonstration.

<Script language = "JavaScript"> <! -- Var doc = new ActiveXObject ("Msxml2.DOMDocument"); // ie5.5 +, CreateObject ("Microsoft. XMLDOM ") // load the document // doc. load ("B. xml "); // create a file header var p = doc. createProcessingInstruction ("xml", "version = '1. 0 'encoding= 'gb2312' "); // Add a file header doc. a (p); // obtain the root contact when loading directly. // var root = doc.doc umentElement; // create the root contact in either of the following ways: // var root = doc. createElement ("students"); var root = doc. createNode (1, "students", ""); // create a subpoint var n = doc. createNode (1, "ttyp", ""); // specifies the Child Contact text // n. text = "this is a test"; // create a sun contact var o = doc. createElement ("sex"); o. text = "male"; // specify its text // create the property var r = doc. createAttribute ("id"); r. value = "test"; // Add attribute n. setAttributeNode (r); // create the second property var r1 = doc. createAttribute ("class"); r1.value = "tt"; // Add attribute n. setAttributeNode (r1); // Delete the second attribute n. removeAttribute ("class"); // Add the sun Contact n. a (o); // Add text contacts n. a (doc. createTextNode ("this is a text node. "); // Add annotation n. a (doc. createComment ("this is a comment \ n"); // Add the Child Contact root. a (n); // copy the contact var m = n. cloneNode (true); root. a (m); // Delete the contact root. removeChild (root. childNodes (0); // create a data segment var c = doc. createCDATASection ("this is a cdata"); c. text = "hi, cdata"; // Add the data segment root. a (c); // Add the root contact doc. a (root); // find the contact var a = doc. getElementsByTagName_r ("ttyp"); // var a = doc. selectNodes ("// ttyp"); // display the attributes of the change contact for (var I = 0; I <. length; I ++) {alert (. xml); for (var j = 0; j <. attributes. length; j ++) {alert (. attributes [j]. name) ;}} // modify the node and use XPATH to locate the node var B = doc. selectSingleNode ("// ttyp/sex"); B. text = ""; // alert (doc. xml); // Save the XML file (you must use FSO on the server side and the client side) // doc. save (); // view the root contact XMLif (n) {alert (n. ownerDocument. xml);} // --> </script>

PS: Here are some online tools for xml operations for your reference:

Online XML/JSON conversion tools:
Http://tools.jb51.net/code/xmljson

Online formatting XML/online compression XML:
Http://tools.jb51.net/code/xmlformat

XML online compression/formatting tool:
Http://tools.jb51.net/code/xml_format_compress

XML code online formatting and beautification tools:
Http://tools.jb51.net/code/xmlcodeformat

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.