Php parsing xml method example _ PHP Tutorial

Source: Internet
Author: User
Php parsing xml method example details. Php parsing xml method example description this article describes the php parsing xml method in the form of an example. Share it with you for your reference. The specific analysis is as follows: the books. xml file is as follows :? 12345 php parsing xml method example

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:

?

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. 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 the XML file

$ Doc-> load ("books. xml ");

// Obtain 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.

?

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

// Read the xml file

$ File = "books. xml ";

$ Data = file_get_contents ($ file );

// Create a parser

$ Parser = xml_parser_create ();

// Parse XML data into an array

Xml_parse_pai_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

?

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 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: the books. xml file is as follows :? 1 2 3 4 5...

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.