Summary of methods of PHP output XML format data

Source: Internet
Author: User
Tags cdata
This article mainly introduced the PHP output XML format data method, combined with the example form summarizes the PHP commonly used XML format data output related operation skill, needs the friend can refer to the next

This article describes the PHP output XML format data method. Share to everyone for your reference, as follows:

Method 1:

<?phpheader ("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 "+";  echo "</age>";  echo "<sex>";  echo "Male";  echo "</sex>"; echo "</user>"; echo "<user>";  echo "<name>";  echo "Yan Yan";  echo "</name>";  echo "<age>";  echo "All";  echo "</age>";  echo "<sex>";  echo "female";  echo "</sex>"; echo "</user>"; echo "</users>";? >

The above example runs as follows:

Method 2:

<?phpheader ("Content-type:text/xml"); echo "<?xml version= ' 1.0 ' encoding= ' UTF-8 '?>"; echo "<users> <user><name> Little rookie </name><age>24</age><sex> male </sex></user>< User><name> Yan Yan </name><age>23</age><sex> female </sex></user></users > ";? >

Method 3:

<?phpheader (' content-type:text/xml; '); /Create new XML file $dom = DOMDocument (' 1.0 ', ' utf-8 ');//build <response> element $response = $dom->createelement (' response ') ); $dom->a ($response);//create <books> elements as sub-elements of <response> $books = $dom->createelement (' books '); $ Response->a ($books);//create title for book $title = $dom->createelement (' title '); $titleText = $dom->createtextnode (' PHP and Ajax '), $title->a ($titleText);//Create an ISBN element for book $ISBN = $dom->createelement (' ISBN '); $isbnText = $dom createTextNode (' 1-21258986 '); $isbn->a ($isbnText);//create book element $book = $dom->createelement (' book '); $book->a ($title) $book->a ($ISBN);//<book> as a <books> sub-element $books->a ($book);//Create an XML structure in a string variable $xmlstring = $ Dom->savexml ();//output XML string echo $xmlString;? >

Example of 1.php output XML

Header (' Content-type:text/xml '); $sql = "SELECT * from article ORDER by post_date limit"; $re = $db->query ($sql); Ech O "<?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. Manipulating XML instances with JavaScript

Try to use all the actions and attributes to do the demonstration.

<script language= "JavaScript" ><!--var doc = new ActiveXObject ("Msxml2.domdocument"); Ie5.5+,createobject ("Microsoft.XMLDOM")//Loading document//doc.load ("B.xml");//Create file header var p = doc.createprocessinginstruction ("xml", "version= ' 1.0 ' encoding= ' gb2312 ');//Add File Header DOC.A (p);//Get root contact for direct load//var root = doc.documentelement;// Two ways to create root contact//var root = doc.createelement ("students"); var root = Doc.createnode (1, "Students", "" ");//create sub-contact var n = Doc.createnode (1, "Ttyp", "" ");//Specify sub-contact text//n.text =" This is a test ";//create grandchild contact var o = doc.createelement (" Sex "); o.text =" male "; Specify its text//create attribute var r = doc.createattribute ("id"); r.value= "Test";//Add Property N.setattributenode (R);//Create Second property var R1 = Doc.createattribute ("class"); R1.value= "TT";//Add Attribute N.setattributenode (r1);//Delete second attribute N.removeattribute ("class");// Add Sun Contact N.A (o);//Add Text contact N.A (Doc.createtextnode ("This is a text node.")); /Add Comment n.a (doc.createcomment ("This is a comment\n");//Add sub-contact ROOT.A (n);//Copy contact var m = N.clonenode (True); Root.a (m);// Delete Contact Root.removechild (root.childnodes (0));//Create data segment var c = Doc.createcdatasection ("This is a CDATA"); C.text = "hi,cdata";//Add Data Segment Root.a (c);//Add root node doc.a (root);//Find contact var a =    Doc.getelementsbytagname_r ("Ttyp");//var a = Doc.selectnodes ("//ttyp");//Display the properties of the change point for (Var i= 0;i<a.length;i++) {    alert (a.xml);    for (Var j=0;j<a.attributes.length;j++) {alert (a.attributes[j].name); }}//modifies the node, uses the XPath to locate the node var b = Doc.selectsinglenode ("//ttyp/sex"); b.text = "female";//alert (doc.xml);//xml Save (required on the server, Client with FSO)//doc.save ();//view root contact Xmlif (n) {alert (n.ownerdocument.xml);} --></script>

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.