This article mainly introduces php implementation code for operating XML, reading data, and writing data. For more information, see
This article mainly introduces php implementation code for operating XML, reading data, and writing data. For more information, see
Xml file
<? Xml version = "1.0" encoding = "UTF-8"?> 23 Happy lufei Male Face/43.jpg 123@qq.com 1212121212
Php parses XML to get values in tags
/** _ Get_xml: The obtained XML file * @ access public indicates that the function is made public * @ param $ _ xmlfile: xml file * $ _ the data array retrieved from XML by html **/function _ get_xml ($ _ xmlfile) {$ _ html = array (); if (file_exists ($ _ xmlfile) {$ _ xml = file_get_contents ($ _ xmlfile); preg_match_all ('/ (. *) <\/Vip>/', $ _ xml, $ _ dom); foreach ($ _ dom [1] as $ _ value) {preg_match_all ('/ (. *) <\/Id>/', $ _ value, $ _ id); preg_match_all ('/ (. *) <\/Username>/', $ _ value, $ _ username); preg_match_all ('/ (. *) <\/Sex>/', $ _ value, $ _ sex); preg_match_all ('/ (. *) <\/Face>/', $ _ value, $ _ face); preg_match_all ('/ (. *) <\/Email>/', $ _ value, $ _ email); preg_match_all ('/ (. *) <\/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 XML files
/** _ Set_xml write information to the XML file * @ access public indicates that the function is made public * @ param $ _ xmlfile xml file * @ param $ _ clean indicates the array of information to be written ** /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 =" \ R \ t "; fwrite ($ _ fp, $ _ string, strlen ($ _ string); $ _ string =" \ t {$ _ Clean ['id']} \ R \ t "; fwrite ($ _ fp, $ _ string, strlen ($ _ string); $ _ string =" \ t {$ _ Clean ['username']} \ R \ t "; fwrite ($ _ fp, $ _ string, strlen ($ _ string); $ _ string =" \ t {$ _ Clean ['sex']} \ R \ t "; fwrite ($ _ fp, $ _ string, strlen ($ _ string); $ _ string =" \ t {$ _ Clean ['face']} \ R \ t "; fwrite ($ _ fp, $ _ string, strlen ($ _ string); $ _ string =" \ t {$ _ Clean ['email ']} \ R \ t "; fwrite ($ _ fp, $ _ string, strlen ($ _ string); $ _ string =" \ t {$ _ Clean ['url']} \ R \ t "; fwrite ($ _ fp, $ _ string, strlen ($ _ string); $ _ string =" "; Fwrite ($ _ fp, $ _ string, strlen ($ _ string); flock ($ _ fp, LOCK_UN); fclose ($ _ fp );}
,