Table of Contents
php的XML文件解释类应用实例
Home php教程 php手册 php的XML文件解释类应用实例

php的XML文件解释类应用实例

Jun 13, 2016 am 09:25 AM
php xml Example application document of kind explain

php的XML文件解释类应用实例

 XMLParser.class.php类文件如下:

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

/** XML 文件分析类 

*  Date:  2013-02-01 

*  Author: fdipzone 

*  Ver:  1.0 

*  func: 

*  loadXmlFile($xmlfile)   读入xml文件输出Array 

*  loadXmlString($xmlstring) 读入xmlstring 输出Array 

*/ 

   

class XMLParser{ 

   

  /** 读取xml文件 

  * @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); 

  } 

   

  /** 读取xmlstring 

  * @param String $xmlstring 

  * @return Array 

  */ 

  public function loadXmlString($xmlstring){ 

    // parser xml 

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

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

  } 

   

  /** 解释xml内容 

  * @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); 

  } 

   

  /** 检查xml格式是否正确 

  * @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; 

    } 

  } 

   

  /** object 转 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; 

        } 

      } 

    } 

  } 

   

  /** 输出返回 

  * @param boolean $flag true:false 

  * @param Array  $data 转换后的数据 

  * @return Array 

  */ 

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

    return array($flag, $data); 

  } 

?>

Demo示例程序如下:

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

require "XMLParser.class.php"; 

   

$xmlfile = 'file.xml'; 

$xmlstring = ' 

 

1000 

 

100 

fdipzone 

1 

28 

 

'; 

echo '

'; 
            <p>$xml_parser = new XMLParser(); </p>
            <p>echo "response xmlfile\r\n"; </p>
            <p>list($flag, $xmldata) = $xml_parser->loadXmlFile($xmlfile); </p>
            <p>if($flag){ </p>
            <p>  print_r($xmldata); </p>
            <p>} </p>
            <p>echo "response xmlstring\r\n"; </p>
            <p>list($flag, $xmldata) = $xml_parser->loadXmlString($xmlstring); </p>
            <p>if($flag){ </p>
            <p>  print_r($xmldata); </p>
            <p>} </p>
            <p>echo '</p>
Copy after login
'; 

?>

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

See all articles