Home > Backend Development > PHP Tutorial > Detailed explanation of php parsing xml method example_PHP tutorial

Detailed explanation of php parsing xml method example_PHP tutorial

WBOY
Release: 2016-07-13 09:53:42
Original
1001 people have browsed it

Detailed explanation of php parsing xml method examples

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

The books.xml file is as follows:

 ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

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

2

3

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

//创建一个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."
";

echo "author:".$author."
";

echo "year:".$year."
";

echo "price:".$price ."
";

echo "***********************************
";

}

?>

4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
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  ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <🎜>//Create a DOMDocument object<🎜> <🎜>$doc=new DOMDocument();<🎜> <🎜>//Load 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 xml data into an array, release the parser, and then extract the desired value from the array.

 ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

// 读取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['type'];

$tag = $value['tag'];

$level = $value['level'];

$attributes = isset($value['attributes'])?$value['attributes']:"";

$val = isset($value['value'])?$value['value']:"";

switch ($type) {

case 'open':

if ($attributes != "" || $val != "") {

$arr[$t]['tag'] = $tag;

$arr[$t]['attributes'] = $attributes;

$arr[$t]['level'] = $level;

$t ;

}

break;

case "complete":

if ($attributes != "" || $val != "") {

$arr[$t]['tag'] = $tag;

$arr[$t]['attributes'] = $attributes;

$arr[$t]['val'] = $val;

$arr[$t]['level'] = $level;

$t ;

}

break;

}

}

echo "

";</p>
            <p>print_r($arr);</p>
            <p>echo "
";

?>

1

2

3

1

2

3

4

5

6

7

$file="books.xml";

$xml = simplexml_load_file($file);

echo "

";</p>
            <p>print_r($xml);</p>
            <p>echo "
";

?>

4

5

6

8 9 10 11 12
13 14
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
<🎜>//Read xml file<🎜> <🎜>$file = "books.xml";<🎜> <🎜>$data = file_get_contents($file);<🎜> <🎜>//Create parser<🎜> <🎜>$parser = xml_parser_create();<🎜> <🎜>//Parse XML data into an 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['attributes'])?$value['attributes']:"";<🎜> <🎜>$val = isset($value['value'])?$value['value']:"";<🎜> <🎜>switch ($type) {<🎜> <🎜>case 'open':<🎜> <🎜>if ($attributes != "" || $val != "") {<🎜> <🎜>$arr[$t]['tag'] = $tag;<🎜> <🎜>$arr[$t]['attributes'] = $attributes;<🎜> <🎜>$arr[$t]['level'] = $level;<🎜> <🎜>$t ;<🎜> <🎜>}<🎜> <🎜>break;<🎜> <🎜>case "complete":<🎜> <🎜>if ($attributes != "" || $val != "") {<🎜> <🎜>$arr[$t]['tag'] = $tag;<🎜> <🎜>$arr[$t]['attributes'] = $attributes;<🎜> <🎜>$arr[$t]['val'] = $val;<🎜> <🎜>$arr[$t]['level'] = $level;<🎜> <🎜>$t ;<🎜> <🎜>}<🎜> <🎜>break;<🎜> <🎜>}<🎜> <🎜>}<🎜> <🎜>echo "
";
            print_r($arr);
            echo "
"; ?>
 3. Use SAX parser to read XML-----XML Simple API (SAX) parser  ?
1 2 3 4 5 6 7 <🎜>$file="books.xml";<🎜> <🎜>$xml = simplexml_load_file($file);<🎜> <🎜>echo "
";
            print_r($xml);
            echo "
"; ?>
I hope this article will be helpful to everyone’s PHP programming design. http://www.bkjia.com/PHPjc/1000122.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1000122.htmlTechArticleDetailed Example of PHP Parsing XML Method This article explains the PHP parsing XML method in detail in the form of examples. Share it with everyone for your reference. The specific analysis is as follows: The books.xml file is as follows: ? 1 2 3 4 5...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template