Php parsing xml method example details, parsing xml instance details _ PHP Tutorial

Source: Internet
Author: User
For example, the xml Parsing method in php is described in detail. For example, the xml Parsing method of php is described in detail. Share it with you for your reference. The detailed analysis is as follows: the books. xml file php parses the xml method instance details, and the xml parsing instance details

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:

<?xml version="1.0" encoding="ISO-8859-1"?>
  
   
   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

<? Php // Create a DOMDocument object $ doc = new DOMDocument (); // load the XML file $ doc-> 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.

<? Php // read xml file $ file = "books. xml "; $ data = file_get_contents ($ file); // Create a parser $ parser = xml_parser_create (); // parse XML data to the array xml_parse_into_struct ($ parser, $ data, $ vals, $ index); // release the parser xml_parser_free ($ parser); // array Processing $ arr = array (); $ t = 0; foreach ($ vals as $ value) {$ type = $ value ['type']; $ tag = $ value ['tag']; $ level = $ value ['level']; $ attributes = isset ($ value ['bubuckets'])? $ Value ['bubuckets']: ""; $ val = isset ($ value ['value'])? $ Value ['value']: ""; switch ($ type) {case 'open': if ($ attributes! = "" | $ Val! = "") {$ Arr [$ t] ['tag'] = $ tag; $ arr [$ t] ['bubuckets'] = $ attributes; $ arr [$ t] ['level'] = $ level; $ t ++;} break; case "complete": if ($ attributes! = "" | $ Val! = "") {$ Arr [$ t] ['tag'] = $ tag; $ arr [$ t] ['bubuckets'] = $ attributes; $ arr [$ t] ['Val'] = $ val; $ arr [$ t] ['level'] = $ level; $ t ++;} break ;}} echo"
"; print_r($arr); echo "
";?>

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

<?php $file="books.xml"; $xml = simplexml_load_file($file); echo "
"; print_r($xml); echo "
";?>

I hope this article will help you with php programming.

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: books. xml file...

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.