Php XML file Interpretation Application Example

Source: Internet
Author: User

Php XML file Interpretation Application Example

The XMLParser. class. php class 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

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

<? Php

/** XML file analysis class

* Date: 2013-02-01

* Author: fdipzone

* Ver: 1.0

*

* Func:

* LoadXmlFile ($ xmlfile) reads the xml file and outputs the Array

* LoadXmlString ($ xmlstring) reads the xmlstring output Array

*/

Class XMLParser {

/** Read xml files

* @ Param String $ xmlfile

* @ Return Array

*/

Public function loadXmlFile ($ xmlfile ){

// Get xmlfile content

$ Xmlstring = file_exists ($ xmlfile )? File_get_contents ($ xmlfile ):'';

// Parser xml

List ($ flag, $ data) = $ this-> parser ($ xmlstring );

Return $ this-> response ($ flag, $ data );

}

/** Read xmlstring

* @ Param String $ xmlstring

* @ Return Array

*/

Public function loadXmlString ($ xmlstring ){

// Parser xml

List ($ flag, $ data) = $ this-> parser ($ xmlstring );

Return $ this-> response ($ flag, $ data );

}

/** Interpret xml content

* @ Param String $ xmlstring

* @ Return Array

*/

Private function parser ($ xmlstring ){

$ Flag = false;

$ Data = array ();

// Check xml format

If ($ this-> checkXmlFormat ($ xmlstring )){

$ Flag = true;

// Xml to object

$ Data = simpleXML_load_string ($ xmlstring, 'simplexmlelement', LIBXML_NOCDATA );

// Object to array

$ This-> objectToArray ($ data );

}

Return array ($ flag, $ data );

}

/** Check whether the xml format is correct

* @ Param String $ xmlstring

* @ Return boolean

*/

Private function checkXmlFormat ($ xmlstring ){

If ($ xmlstring = ''){

Return false;

}

$ Xml_parser_obj = xml_parser_create ();

If (xml_parse_into_struct ($ xml_parser_obj, $ xmlstring, $ vals, $ indexs) === 1) {// 1: success 0: fail

Return true;

} Else {

Return false;

}

}

/** Convert object to Array

* @ Param object $ object

* @ Return Array

*/

Private function objectToArray (& $ object ){

$ Object = (array) $ object;

Foreach ($ object as $ key => $ value ){

If ($ value = ''){

$ Object [$ key] = "";

} Else {

If (is_object ($ value) | is_array ($ value )){

$ This-> objectToArray ($ value );

$ Object [$ key] = $ value;

}

}

}

}

/** Output return

* @ Param boolean $ flag true: false

* @ Param Array $ data converted data

* @ Return Array

*/

Private function response ($ flag = false, $ data = array ()){

Return array ($ flag, $ data );

}

}

?>

The Demo sample program is as follows:

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

<? Php

Require "XMLParser. class. php ";

$ Xmlfile = 'file. xml ';

$ Xmlstring = '<? Xml version = "1.0" encoding = "UTF-8"?>

<Xmlroot>

<Status> 1000 </status>

<Info> </info>

<Result> <id> 100 </id>

<Name> fdipzone </name>

<Gender> 1 </gender>

<Age> 28 </age>

</Result>

</Xmlroot> ';

Echo '<pre> ';

$ Xml_parser = new XMLParser ();

Echo "response xmlfile \ r \ n ";

List ($ flag, $ xmldata) = $ xml_parser-> loadXmlFile ($ xmlfile );

If ($ flag ){

Print_r ($ xmldata );

}

Echo "response xmlstring \ r \ n ";

List ($ flag, $ xmldata) = $ xml_parser-> loadXmlString ($ xmlstring );

If ($ flag ){

Print_r ($ xmldata );

}

Echo '</pre> ';

?>

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.