PHP stores data in XML

Source: Internet
Author: User
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;

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.