PHP simple XML processing

Source: Internet
Author: User
1. manually output xml ** manually output xml * @ authorflyer0126 * @ since2012427 * header (& amp; quot; Content-Type: textxml & amp; quot ;... i. manually output xml

/** Manually output xml * @ author flyer0126 * @ since 2012/4/27 */header ("Content-Type: text/xml"); print'
 '. "\ N"; print"
 
  
\ N "; $ shows = array ('name' => 'A', 'channel' => 'A1', 'Start' => 'A2 '), array ('name' => 'BB', 'channel' => 'b1 ', 'Start' => 'B2 '), array ('name' => 'CC', 'channel' => 'C1', 'Start' => 'C2'); foreach ($ shows as $ show) {print"
  
   
\ N "; foreach ($ show as $ tag => $ val) {print" <$ tag> ". htmlspecialchars ($ val )."
   \ N ";} print"
  \ N ";} print"
 \ N ";/**
 
 
  
   
    
Aa
   
   
    
A1
   
   
    
A2
   
  
  
   
    
Bb
   
   
    
B1
   
   
    
B2
   
  
  
   
    
Cc
   
   
    
C1
   
   
    
C2
   
  
 **/

II. use DOMDocument to generate xml

/** Generate xml * @ author flyer0126 * @ since 2012/4/27 * // create a new document $ dom = new DOMDocument ("1.0", "UTF-8 "); // create the root element humans and add it to the document $ humans = $ dom-> appendChild ($ dom-> createElement ("humans ")); // create the root element zhangying and add it to humans $ zhangying = $ humans-> appendChild ($ dom-> createElement ("zhangying"); // create the name element, and add it to zhangying $ name = $ zhangying-> appendChild ($ dom-> createElement ("name ")); $ name-> appendChild ($ dom-> createTextNode (""); $ sex = $ zhangying-> appendChild ($ dom-> createElement ("sex ")); $ sex-> appendChild ($ dom-> createTextNode ("male"); $ old = $ zhangying-> appendChild ($ dom-> createElement ("old ")); $ old-> appendChild ($ dom-> createTextNode ("28"); // create the root element tank, and add it to humans $ tank = $ humans-> appendChild ($ dom-> createElement ("tank"); // Create name element, and add it to tank $ name = $ tank-> appendChild ($ dom-> createElement ("name ")); $ name-> appendChild ($ dom-> createTextNode ("tank"); $ sex = $ tank-> appendChild ($ dom-> createElement ("sex ")); $ sex-> appendChild ($ dom-> createTextNode ("male"); $ old = $ tank-> appendChild ($ dom-> createElement ("old ")); $ old-> appendChild ($ dom-> createTextNode ("28 ")); /* // Set The author element $ author = $ book-> appendChild ($ dom-> createElement ("author ")); $ author-> appendChild ($ dom-> createTextNode ("zhangh"); $ author-> setAttribute ("class", "win_close "); * // Output $ dom-> formatOutput = true; header ('content-Type: text/XML'); echo $ dom-> saveXML (); // put the document representation in the book variable // $ book = $ dom-> saveXML (); // output the document as book. xml file // $ dom-> save ("humans. xml ");/**
     
     
      
   
    
Zhang Ying
       
   
    
Male
       
   
    
28
       
      
      
   
    
Tank
       
   
    
Male
       
   
    
28
       
      
 **/

III. xml reading

/** Read xml * @ author flyer0126 * @ since 2012/4/27 * // 1. use simplexml_load_file to read a simple xml file $ humans = simplexml_load_file ("humans. xml "); foreach ($ humans as $ key = >$ human) {echo $ human-> name. "-> ". $ human-> sex. "-> ". $ human-> old. "\ n";}/** Zhang Ying-> male-> 28tank-> male-> 28 ** // 2. use DOMDocument to read xml $ dom = new DOMDocument (); $ dom-> load ("humans. xml "); $ humans = $ dom-> getElementsByTagName (" humans "); foreach ($ humans as $ key => $ human) {$ names = $ human-> getElementsByTagName ("name"); $ olds = $ human-> getElementsByTagName ("old "); $ sexes = $ human-> getElementsByTagName ("sex"); foreach ($ names as $ k = >$ name) {$ name = $ names-> item ($ k) -> nodeValue; $ sex = $ sexes-> item ($ k)-> nodeValue; $ old = $ olds-> item ($ k)-> nodeValue; echo "$ name-> $ sex-> $ old \ n ";}} /** Zhang Ying-> male-> 28tank-> male-> 28 ** // 3. use regular expressions to obtain data $ xml = ''; $ f = fopen (" humans. xml "," r "); while ($ data = fread ($ f, 2096) {$ xml. = $ data;} fclose ($ f); preg_match_all ('/\
 
  
(.*?) \ <\/Humans \>/S', $ xml, $ humans); foreach ($ humans [1] as $ human) {preg_match_all ('/\
  
   
(.*?) \ <\/Name \>/S', $ human, $ names); preg_match_all ('/\
   
    
(.*?) \ <\/Sex \>/S', $ human, $ sexes); preg_match_all ('/\
    
     
(.*?) \ <\/Old \>/s ', $ human, $ olds);} foreach ($ names [1] as $ k => $ name) {echo $ names [1] [$ k]. '-'. $ sexes [1] [$ k]. '-'. $ olds [1] [$ k]. "\ n";}/** -> male-> 28tank-> male-> 28 ** // 4. use XMLReader to obtain data $ reader = new XMLReader (); $ reader-> open ("humans. xml "); $ I = 1; while ($ reader-> read () {if ($ reader-> nodeType = XMLReader: TEXT) {if ($ I % 3) {echo $ reader-> value;} else {echo $ reader-> value. "\ n" ;}$ I ++ ;}/ ** Zhang Yingnan 28tank male 28 **/
    
   
  
 

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.