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);}