Php code for XML operations, data reading, and data writing

Source: Internet
Author: User
Tags flock
This article mainly introduces the php implementation code for operating XML, reading data, and writing data. For more information, see the 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 );}

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.