php XML file parsing class (with demo code)

WBOY
Release: 2016-07-25 08:55:21
Original
767 people have browsed it
  1. /**XML file analysis class

  2. * Date: 2013-02-01
  3. * Author: fdipzone
  4. * Ver: 1.0
  5. * Edition bbs.it-home.org
  6. * func:
  7. * loadXmlFile($xmlfile) reads xml file and outputs Array
  8. * loadXmlString($xmlstring) reads xmlstring and outputs Array
  9. */
  10. class XMLParser{
  11. /**Read xml file
  12. * @param String $xmlfile
  13. * @return Array
  14. */
  15. public function loadXmlFile($xmlfile){
  16. // get xmlfile content
  17. $xmlstring = file_exists($xmlfile)? file_get_contents($xmlfile) : '';
  18. // parser xml
  19. list($flag, $data) = $this->parser($xmlstring);
  20. return $this->response($flag, $data);
  21. }
  22. /**读取xmlstring
  23. * @param String $xmlstring
  24. * @return Array
  25. */
  26. public function loadXmlString($xmlstring){
  27. // parser xml
  28. list($flag, $data) = $this->parser($xmlstring);
  29. return $this->response($flag, $data);
  30. }
  31. /**Interpret xml content
  32. * @param String $xmlstring
  33. * @return Array
  34. */
  35. private function parser($xmlstring){
  36. $flag = false;
  37. $data = array();
  38. // check xml format
  39. if($this->checkXmlFormat($xmlstring)){
  40. $flag = true;
  41. // xml to object
  42. $data = simpleXML_load_string($xmlstring, 'SimpleXMLElement', LIBXML_NOCDATA);
  43. // object to array
  44. $this->objectToArray($data);
  45. }
  46. return array($flag, $data);
  47. }
  48. /**Check whether the xml format is correct
  49. * @param String $xmlstring
  50. * @return boolean
  51. */
  52. private function checkXmlFormat($xmlstring){
  53. if($xmlstring==''){
  54. return false;
  55. }
  56. $xml_parser_obj = xml_parser_create();
  57. if(xml_parse_into_struct($xml_parser_obj, $xmlstring, $vals, $indexs)===1){ // 1:success 0:fail
  58. return true;
  59. }else{
  60. return false;
  61. }
  62. }

  63. /**object 转 Array

  64. * @param object $object
  65. * @return Array
  66. */
  67. private function objectToArray(&$object){
  68. $object = (array)$object;
  69. foreach($object as $key => $value){
  70. if($value==''){
  71. $object[$key] = "";
  72. }else{
  73. if(is_object($value) || is_array($value)){
  74. $this->objectToArray($value);
  75. $object[$key] = $value;
  76. }
  77. }
  78. }
  79. }

  80. /**Output returns

  81. * @param boolean $flag true:false
  82. * @param Array $data converted data
  83. * @return Array
  84. */
  85. private function response($flag=false, $data=array()){
  86. return array($flag, $data);
  87. }
  88. }
  89. ?>

复制代码

2,演示示例

  1. require "XMLParser.class.php";
  2. $xmlfile = 'file.xml';
  3. $xmlstring = '
  4. 1000
  5. 100
  6. fdipzone
  7. 1
  8. 28
  9. ';
  10. echo '
    ';  </li>
    <li>  </li>
    <li>$xml_parser = new XMLParser();  </li>
    <li>echo "response xmlfilern";  </li>
    <li>list($flag, $xmldata) = $xml_parser->loadXmlFile($xmlfile);  </li>
    <li>if($flag){  </li>
    <li>    print_r($xmldata);  </li>
    <li>}  </li>
    <li>  </li>
    <li>echo "response xmlstringrn";  </li>
    <li>list($flag, $xmldata) = $xml_parser->loadXmlString($xmlstring);  </li>
    <li>if($flag){  </li>
    <li>    print_r($xmldata);  </li>
    <li>}  </li>
    <li>  </li>
    <li>echo '
    ';
  11. ?>
复制代码

附,PHP XML预定义常量: http://bbs.it-home.org/shouce/php5/libxml.constants.html



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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!