Most of the time, when the data volume is small or we need to use XML files to store some data for fast loading, today, I will share with you some of my experiences in using XML to operate data in the PHP project. First, PHP itself has a function set for operating XML. In this way, we can operate XML without using other plug-ins. Below is the PHP's own
Most of the time, when the data volume is small or we need to use XML files to store some data for fast loading, today, I will share with you some of my experiences in using XML to operate data in the PHP project. First, PHP itself has a function set for operating XML. In this way, we can operate XML without using other plug-ins. Below is the PHP's own
Most of the time, when the data volume is small or we need to use XML files to store some data for fast loading, today, I will share with you some of my experiences in using XML to operate data in the PHP project.
First, PHP itself has a function set for operating XML. In this way, we can operate XML without using other plug-ins.
Below is the XML function set of PHP itself, which I will list here:
SimpleXMLElement: addAttribute-adds a property SimpleXMLElement: addChild for the XML node-adds a subnode SimpleXMLElement: asXML for the XML node-returns a formatted XML SimpleXMLElement :: simplebutes-the same node's attribute set SimpleXMLElement: children-finds the child node SimpleXMLElement :__ construct-constructor through the given node, and creates a new XML Object SimpleXMLElement :: count-calculate the total number of child nodes of a node SimpleXMLElement: getDocNamespaces-return the document namespace SimpleXMLElement: getName-get the XML node name SimpleXMLElement :: getNamespaces-returns the namespace name SimpleXMLElement: registerXPathNamespace-Creates a prefix/ns context for the next XPath query SimpleXMLElement: xpath-Runs XPath query on XML data.
Specific application:
$ Data = '<? Xml version = "1.0" encoding = "gb2312"?> <Xml> </xml> '; // create the XML file header $ xml = new SimpleXMLElement ($ data); // construct an XML object, and the corresponding header is given as a parameter; $ id = intval (getRequest ('id', 0); $ lotid = intval (getRequest ('lotid ', 0); $ PrizeBulle = News_Article: getPrize ($ id); // obtain data from the database; $ prizecount = count ($ PrizeBulle); if (! Empty ($ PrizeBulle) {$ m = $ xml-> addChild ('state', 100 ); // create a subnode state $ m = $ xml-> addChild ('message', iconv ('gbk', 'utf-8', 'obtained successfully ')); // Note: if it is Chinese, // It Must Be transcoded, otherwise it will be garbled; $ list = $ xml-> addChild ('LIST '); for ($ I = 0; $ I <$ prizecount; $ I ++) {$ row = $ list-> addChild ('row '); $ row-> addChild ('title', iconv ('gbk', 'utf-8', $ PrizeBulle [$ I] ['title']); // Add a node for the child node $ row-> addChild ('imgadd', $ PrizeBulle [$ I] ['ticketurl']); $ row-> addChild ('user ', iconv ('gbk', 'utf-8', $ PrizeBulle [$ I] ['Initiator']); $ row-> addChild ('prizedetail ', iconv ('gbk', 'utf-8', $ PrizeBulle [$ I] ['prizedescription']); $ row-> addChild ('fatype ', iconv ('gbk', 'utf-8', $ PrizeBulle [$ I] ['playtype']); $ row-> addChild ('lotid', $ lotid ); $ row-> addChild ('playtype', $ PrizeBulle [$ I] ['playtypeid']); $ row-> addChild ('pid ', $ PrizeBulle [$ I] ['projectid']) ;}} else {$ m = $ xml-> addChild ('state', 109 ); $ m = $ xml-> addChild ('message', iconv ('gbk', 'utf-8', 'retrieve failed');} header ("content-type: text/xml "); // set the header information echo $ xml-> asXML (); // output the content of the XML file
Here, we need to pay attention to the transcoding problem. If the XML contains Chinese characters and the encoding is inconsistent with the XML, transcoding is required before output;