Home  >  Article  >  Backend Development  >  PHP parsing xml method example (with code) detailed description

PHP parsing xml method example (with code) detailed description

高洛峰
高洛峰Original
2017-03-12 16:00:341972browse

This article mainly introduces the php parsing xml method, and analyzes in detail the relevant techniques of php parsing XML in the form of examples. Friends in need can refer to the following

This article describes in detail the method of php parsing xml in the form of examples. Share it with everyone for your reference. The specific analysis is as follows:

books.xml file is as follows:

<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
 <book category="children">
 <title lang="en">Harry Potter</title>
 <author>J K. Rowling</author>
 <year>2005</year>
 <price>29.99</price>
 </book>
 <book category="cooking">
 <title lang="en">Everyday Italian</title>
 <author>Giada De Laurentiis</author>
 <year>2005</year>
 <price>30.00</price>
 </book>
 <book category="web" cover="paperback">
 <title lang="en">Learning XML</title>
 <author>Erik T. Ray</author>
 <year>2003</year>
 <price>39.95</price>
 </book>
</bookstore>

1. DOM parsing XML

<?php
 //创建一个DOMDocument对象
 $doc=new DOMDocument();
 //加载XML文件
 $doc->load("books.xml");
 //获取所有的book标签
 $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."<br>";
  echo "author:".$author."<br>";
  echo "year:".$year."<br>";
  echo "price:".$price ."<br>";
  echo "***********************************<br>";
 }
?>

2, xml_parse_into_struct

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

<?php
 // 读取xml文件
 $file = "books.xml";
 $data = file_get_contents($file);
 // 创建解析器
 $parser = xml_parser_create();
 // 将 XML 数据解析到数组中
 xml_parse_into_struct($parser, $data, $vals, $index);
 // 释放解析器
 xml_parser_free($parser);
 // 数组处理
 $arr = array();
 $t=0;
 foreach($vals as $value) {
 $type = $value[&#39;type&#39;];
 $tag = $value[&#39;tag&#39;];
 $level = $value[&#39;level&#39;];
 $attributes = isset($value[&#39;attributes&#39;])?$value[&#39;attributes&#39;]:"";
 $val = isset($value[&#39;value&#39;])?$value[&#39;value&#39;]:"";
 switch ($type) {
  case &#39;open&#39;:
  if ($attributes != "" || $val != "") {
   $arr[$t][&#39;tag&#39;] = $tag;
   $arr[$t][&#39;attributes&#39;] = $attributes;
   $arr[$t][&#39;level&#39;] = $level;
   $t++;
  } 
  break;
  case "complete":
  if ($attributes != "" || $val != "") {
   $arr[$t][&#39;tag&#39;] = $tag;
   $arr[$t][&#39;attributes&#39;] = $attributes;
   $arr[$t][&#39;val&#39;] = $val;
   $arr[$t][&#39;level&#39;] = $level;
   $t++;
  } 
  break;
 } 
 } 
 echo "<pre class="brush:php;toolbar:false">";
 print_r($arr);
 echo "
"; ?>

3. Read XML with SAX parser-----XML Simple API(SAX) parser

<?php
 $file="books.xml";
 $xml = simplexml_load_file($file);
 echo "<pre class="brush:php;toolbar:false">";
 print_r($xml);
 echo "
"; ?>

The above is the detailed content of PHP parsing xml method example (with code) detailed description. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn