Php xml file parsing class (with demo code)

Source: Internet
Author: User
Php xml file parsing class (with demo code)

  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 the xml file and outputs the Array
  8. * LoadXmlString ($ xmlstring) reads the xmlstring output Array
  9. */
  10. Class XMLParser {
  11. /** Read xml files
  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. /** Read 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. /** Convert object to 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 return

  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. Demo

  1. Require "XMLParser. class. php ";
  2. $ Xmlfile = 'File. XML ';
  3. $ Xmlstring ='
  4. 1000
  5. 100
  6. Fdipzone
  7. 1
  8. 28
  9. ';
  10. Echo'
    ';  
  11. $xml_parser = new XMLParser();
  12. echo "response xmlfile\r\n";
  13. list($flag, $xmldata) = $xml_parser->loadXmlFile($xmlfile);
  14. if($flag){
  15. print_r($xmldata);
  16. }
  17. echo "response xmlstring\r\n";
  18. list($flag, $xmldata) = $xml_parser->loadXmlString($xmlstring);
  19. if($flag){
  20. print_r($xmldata);
  21. }
  22. echo '
  23. ';
  24. ?>

Php xml predefined constant: http://bbs.it-home.org/shouce/php5/libxml.constants.html

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.