PHP XML File parsing class (demo code included)

Source: Internet
Author: User
  1. /** XML File parsing classes

  2. * DATE:2013-02-01
  3. * Author:fdipzone
  4. * ver:1.0
  5. * Edition bbs.it-home.org
  6. * Func:
  7. * Loadxmlfile ($xmlfile) read in XML file output array
  8. * Loadxmlstring ($xmlstring) read into xmlstring output array
  9. */
  10. Class xmlparser{
  11. /** reading an 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. /** 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. /** Interpreting 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 that 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 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. ?>

Copy Code

2, demo example

  1. Require "XMLParser.class.php";
  2. $xmlfile = ' file.xml ';
  3. $xmlstring = '
  4. +
  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. ?>
Copy Code

Attached, PHP XML pre-defined constants: 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.