Php parsing xml method example (with code)

Source: Internet
Author: User
This article mainly introduces the php xml Parsing method and analyzes the related skills of php XML parsing in the form of examples. if you need it, refer to the following article to introduce the php xml Parsing method, detailed analysis of php XML parsing skills in the form of examples. For more information, see

This document describes in detail the xml Parsing method in php as an example. Share it with you for your reference. The specific analysis is as follows:

The books. xml file is as follows:

 
  
   
   Harry Potter J K. Rowling 
   
    2005
    
   
    29.99
    
   
   
   Everyday Italian Giada De Laurentiis 
   
    2005
    
   
    30.00
    
   
   
   Learning XML Erik T. Ray 
   
    2003
    
   
    39.95
    
  
 

1. DOM parsing XML

 Load ("books. xml "); // get all book Tags $ bookDom = $ doc-> getElementsByTagName (" book "); foreach ($ bookDom as $ book) {$ title = $ book-> getElementsByTagName ("title")-> item (0)-> nodeValue; $ author = $ book-> getElementsByTagName ("author ") -> item (0)-> nodeValue; $ year = $ book-> getElementsByTagName ("year")-> item (0)-> nodeValue; $ price = $ book-> getElementsByTagName ("price")-> item (0)-> nodeValue; echo "title :". $ title."
"; Echo" author: ". $ author ."
"; Echo" year: ". $ year ."
"; Echo" price: ". $ price ."
"; Echo "***********************************
";}?>

2. xml_parse_into_struct

Create a parser, parse the xml data to the array, release the parser, and then extract the desired value from the array.

 "; Print_r ($ arr); echo"
";?>

3. use the SAX parser to read the XML-----XML Simple API (SAX) parser

 "; print_r($xml); echo "
";?>

The above is a detailed description of the php parsing xml method example (with code). 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.