PHP operation XML, reading data and writing data methods

Source: Internet
Author: User
Tags flock

XML file

<?xml version= "1.0" encoding= "Utf-8"?><vip><id>23</id><username> Happy Lu Fei </username ><sex> male </sex><face>face/43.jpg</face><email>[email protected]</email> <qq>1212121212</qq></vip>
PHP parsing XML Gets the value in the tag

/* * _get_xml Gets the XML file * @access public indicates that the function is exposed externally * @param $_xmlfile XML file * $_html data array extracted from XML * */function _get_xml ($_xmlfil    E) {$_html = array ();        if (file_exists ($_xmlfile)) {$_xml = file_get_contents ($_xmlfile);               Preg_match_all ('/<vip> (. *) <\/vip>/', $_xml,$_dom);            foreach ($_dom[1] as $_value) {Preg_match_all ('/<id> (. *) <\/id>/', $_value,$_id);            Preg_match_all ('/<username> (. *) <\/username>/', $_value,$_username);            Preg_match_all ('/<sex> (. *) <\/sex>/', $_value,$_sex);            Preg_match_all ('/<face> (. *) <\/face>/', $_value,$_face);            Preg_match_all ('/<email> (. *) <\/email>/', $_value,$_email);            Preg_match_all ('/<qq> (. *) <\/qq>/', $_VALUE,$_QQ);            $_html[' id '] = $_id[1][0];            $_html[' username '] = $_username[1][0];            $_html[' sex '] = $_sex[1][0];            $_html[' face '] = $_face[1][0]; $_html['Email '] = $_email[1][0];        $_html[' qq '] = $_qq[1][0];    }}else{_alert_back ("file does not exist"); } return $_html;}
PHP writes data to an XML file

/* * _set_xml writes information to an XML file * @access public indicates that the function is publicly exposed * @param $_xmlfile XML file * @param $_clean An array of information to write * */function _set_xml ($_    Xmlfile,$_clean) {$_fp = @fopen (' Newuser.xml ', ' W '); if (!$_FP) {exit (' system error, file does not exist!    ');    } flock ($_FP,LOCK_EX);    $_string = "<?xml version=\" 1.0\ "encoding=\" utf-8\ "? >\r\t";    Fwrite ($_FP, $_string,strlen ($_string));    $_string = "<vip>\r\t";    Fwrite ($_FP, $_string,strlen ($_string));    $_string = "\t<id>{$_clean[' id ']}</id>\r\t";    Fwrite ($_FP, $_string,strlen ($_string));    $_string = "\t<username>{$_clean[' username ']}</username>\r\t";    Fwrite ($_FP, $_string,strlen ($_string));    $_string = "\t<sex>{$_clean[' sex ']}</sex>\r\t";    Fwrite ($_FP, $_string,strlen ($_string));    $_string = "\t<face>{$_clean[' face ']}</face>\r\t";    Fwrite ($_FP, $_string,strlen ($_string));    $_string = "\t<email>{$_clean[' email ']}</email>\r\t";    Fwrite ($_FP, $_string,strlen ($_string)); $_string = "\t<qq>{$_clean[' url ']}</qq>\r\t";    Fwrite ($_FP, $_string,strlen ($_string));    $_string = "</vip>";    Fwrite ($_FP, $_string,strlen ($_string));    Flock ($_fp,lock_un); Fclose ($_FP);}




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.