Example code of the PHP method for reading XML files

Source: Internet
Author: User
This example summarizes how PHP reads XML files. We will share this with you for your reference. The details are as follows:

This example summarizes how PHP reads XML files. We will share this with you for your reference. The details are as follows:

Books. xml file:
 
  
   Jack Herrington
   PHP Hacks
   
    O'Reilly
   
  
  
   Jack Herrington
   Podcasting Hacks
   
    O'Reilly
   
  
 
  1. DOMDocument method

 load( 'books.xml' );$books = $doc->getElementsByTagName( "book" );foreach( $books as $book ){    $authors = $book->getElementsByTagName( "author" );    $author = $authors->item(0)->nodeValue;    $publishers = $book->getElementsByTagName( "publisher" );    $publisher = $publishers->item(0)->nodeValue;    $titles = $book->getElementsByTagName( "title" );    $title = $titles->item(0)->nodeValue;    echo "$title - $author - $publisher\n";    echo "
";}?>

2. read XML with a SAX parser:

 

3. parse XML using regular expressions:

 (.*?)\<\/book\>/s", $xml, $bookblocks );foreach( $bookblocks[1] as $block ){    preg_match_all( "/\(.*?)\<\/author\>/", $block, $author );    preg_match_all( "/\
 
  (.*?)\<\/title\>/",  $block, $title );    preg_match_all( "/\
  
   (.*?)\<\/publisher\>/", $block, $publisher );    echo( $title[1][0]." - ".$author[1][0]." - ".$publisher[1][0]."\n" );}?>
  
 

4. parse XML to the array

 
 
 
  
Simple XML data
 "; $ Parser = xml_parser_create (); // create the parser xml_parse_into_struct ($ parser, $ data, $ values, $ index); // Parse it to the array xml_parser_free ($ parser ); // release resources // display the array structure echo "\ n index array \ n"; print_r ($ index); echo "\ n data array \ n "; print_r ($ values);?>

5. check whether XML is valid

 "; // Output error row, column and error information $ error_line = xml_get_current_line_number ($ xml_parser); $ error_row = xml_get_current_column_number ($ xml_parser ); $ error_string = xml_error_string (xml_get_error_code ($ xml_parser); $ message = sprintf ("[row % d, column % d]: % s", $ error_line, $ error_row, $ error_string); echo $ message;} else {echo "the XML document is well structured. ";}// Close the XML parser pointer and release the resource xml_parser_free ($ xml_parser);?>

6. it can be used to accurately read XML

Test. xml

   
     
  
   
100
      
  
   
123456
      
  
   
20040605
      
  
   
153020
      
  
   
1
      
  
   
1
      
  
   
010
      
  
   
Beijing
      
  
   
010
      
  
   
Beijing
      
  
   
0
      
  
   
0
      
  
   
15933626501
      
  
   
8
      
  
   
5618 perennial supply of seedlings, varieties of Magnolia, yellow leaf Yang and so on. Contact: Zhang San, tel: 1234567890.
      
  
   
100
      
  
   
9588
    
 

Test. php:

 -1) {try {// load parsing xml $ xml = simplexml_load_string ($ file); if ($ xml) {// echo $ this-> result; // Get the node value $ CONNECT_ID = $ xml-> CONNECT_ID; $ MO_MESSAGE_ID = $ xml-> MO_MESSAGE_ID; $ RECEIVE_DATE = $ xml-> RECEIVE_DATE; $ RECEIVE_TIME = $ xml-> RECEIVE_TIME; $ GATEWAY_ID = $ xml-> GATEWAY_ID; $ VALID = $ xml-> VALID; $ CITY_CODE = $ xml-> CITY_CODE; $ CITY_NAME = $ xml-> CITY_NAME; $ STATE_CODE = $ xml-> CITY_CODE; $ STATE_NAME = $ Xml-> STATE_NAME; $ TP_PID = $ xml-> TP_PID; $ TP_UDHI = $ xml-> TP_UDHI; $ MSISDN = $ xml-> MSISDN; $ MESSAGE_TYPE = $ xml-> MESSAGE_TYPE; $ MESSAGE = $ xml-> MESSAGE; // SMS $ LONG_CODE = $ xml-> LONG_CODE; $ SERVICE_CODE = $ xml-> SERVICE_CODE; preg_match ("/(561) \ d {5618}/", $ MESSAGE, $ code); switch ($ code [0]) {case: $ myData [message] = $ MESSAGE; break; default: $ myData [] = 'no short message. '; Break ;}} an error occurred while loading the xml file in else {echo. ";}} Catch (exception $ e) {print_r ($ e) ;}} else {echo" does not have this XML file. ";} Echo"
";print_r($myData);echo "";echo $myData[message

The above is the details of the instance code used by PHP to read XML files. For more information, see other related articles in the first PHP community!

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.