Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 13th, 2012  |  syntax: None  |  size: 16.72 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Help me xml parsing in php
  2. <?xml version="1.0" encoding="UTF-8" ?>
  3.  
  4. <response uri="/ats/private/xml/Candidates/getRecords">
  5. <result>
  6. <Candidates>
  7. <row no="1">
  8. <FL val="RESUMEID"><![CDATA[93327000000109297]]></FL>
  9. <FL val="Modified by"><![CDATA[Not specified]]></FL>
  10. <FL val="Modified time"><![CDATA[Yesterday]]></FL>
  11. <FL val="Candidate ID"><![CDATA[677]]></FL>
  12. <FL val="First name"><![CDATA[TEst1]]></FL>
  13. <FL val="Last name"><![CDATA[TEst2]]></FL>
  14. <FL val="Residental Address"><![CDATA[TESTing res address]]></FL>
  15. <FL val="Email ID"><![CDATA[test1@email.com]]></FL>
  16. <FL val="Mobile Phone"><![CDATA[123456789]]></FL>
  17. <FL val="Created On"><![CDATA[Yesterday]]></FL>
  18. <FL val="Owner"><![CDATA[0]]></FL>
  19. <FL val="Source"><![CDATA[Embed]]></FL>
  20. <FL val="Employment Preference"><![CDATA[Contract (Full Time), Permanent (Full Time), Contract (Part Time), Permanent (Part Time)]]></FL>
  21. <FL val="Cover Letter" type="url" url="https://recruit.zoho.com/ats/viewPhoto?filename=tVmTJJk2PIGQSgJSgY2fTzZfMgYE*W1ZBQ.KWiE6q68UOoUiAiY8ElcJtvxybS4bvD4mCWuA8aE-"><![CDATA[samplecoverletter.txt]]></FL>
  22. <FL val="Total work exp (year)"><![CDATA[10]]></FL>
  23. <FL val="Current Role"><![CDATA[Administrative Officer]]></FL>
  24. <FL val="Current Role if Other"><![CDATA[current_role_if_other]]></FL>
  25. <FL val="Current Employer/Customer"><![CDATA[Current_employer_name]]></FL>
  26. <FL val="Skill sets"><![CDATA[skill_sets_goes_here]]></FL>
  27. <FL val="Roles"><![CDATA[Consultant, 93327000000068819, Administrative Officer, Application Developer, Business Analyst, Change Manager, Business Development, Chief Technology Officer, Chief Information Officer, Chief Executive Officer, Data Analyst, Director, IT Manager, Implementation Mgr, Functional Analyst, PMO Manager, Other, Network Admin, Portfolio Manager, Program Manager, Project Admin, Project Manager (Jnr), Project Lead, Project Director, Project Manager (Mid), Project Manager (Snr/Principal), Project Officer, Project Scheduler, Service Delivery Mgr, Release Manager, Quality Manager, Solutions Architect, Systems / Technical Analyst, Systems Administrator, Technical Support, Technical Architect, Systems Engineer, Test Analyst, Test Manager]]></FL>
  28. <FL val="Role (if "Other")"><![CDATA[Role_other]]></FL>
  29. <FL val="Industries"><![CDATA[93327000000068909, 93327000000070037, Aviation, Consulting, Contact Centres, Construction / Engineering, Council, Defense, Education, Energy, Finance/Banking, Gaming, Government, Health / Medical, Hospitality, ICT, Insurance, Manufacturing, Media, Mining, Other, Rail, Retail, Phamacetical, Telecommunications, Transportation, Utilities]]></FL><FL val="Expected Rate/Salary"><![CDATA[147852369]]></FL>
  30. <FL val="Qualifications, Credentials, Memberships"><![CDATA[93327000000068871, 93327000000101075, AABA Membership, Agile PM Certification, AIPM   Membership, CMI Membership, Degree, IIBA Membership, ITIL, Masters Degree, MBA, MSP, PMI Membership, PMP, PRINCE2, RegPM]]></FL>
  31. <FL val="Additional Information"><![CDATA[addational_information_goes_here]]></FL>
  32. <FL val="Attach resume" type="url" url="https://recruit.zoho.com/ats/viewPhoto?filename=tVmTJJk2PIGQSgJSgY2fTzZfMgYE*W1ZBQ.KWiE6q69pGZ2uc5*.nlcJtvxybS4bvD4mCWuA8aE-"><![CDATA[sampleresume.txt]]></FL>
  33. <FL val="Photo (for CV)" type="url" url="https://recruit.zoho.com/ats/viewPhoto?filename=tVmTJJk2PIGQSgJSgY2fTzZfMgYE*W1ZBQ.KWiE6q685xDP3Iw.5GlcJtvxybS4bvD4mCWuA8aE-"><![CDATA[microsoft-gold-partner.png]]></FL>
  34. <FL val="Candidate status"><![CDATA[New]]></FL></row></Candidates></result></response>
  35.        
  36. <?php
  37.  
  38. $result = <<<HERE
  39. <?xml version="1.0" encoding="UTF-8" ?>
  40.  
  41. <response uri="/ats/private/xml/Candidates/getRecords">
  42. <result>
  43. <Candidates>
  44. <row no="1">
  45. <FL val="RESUMEID"><![CDATA[93327000000109297]]></FL>
  46. <FL val="Modified by"><![CDATA[Not specified]]></FL>
  47. <FL val="Modified time"><![CDATA[Yesterday]]></FL>
  48. <FL val="Candidate ID"><![CDATA[677]]></FL>
  49. <FL val="First name"><![CDATA[TEst1]]></FL>
  50. <FL val="Last name"><![CDATA[TEst2]]></FL>
  51. <FL val="Residental Address"><![CDATA[TESTing res address]]></FL>
  52. <FL val="Email ID"><![CDATA[test1@email.com]]></FL>
  53. <FL val="Mobile Phone"><![CDATA[123456789]]></FL>
  54. <FL val="Created On"><![CDATA[Yesterday]]></FL>
  55. <FL val="Owner"><![CDATA[0]]></FL>
  56. <FL val="Source"><![CDATA[Embed]]></FL>
  57. <FL val="Employment Preference"><![CDATA[Contract (Full Time), Permanent (Full Time), Contract (Part Time), Permanent (Part Time)]]></FL>
  58. <FL val="Cover Letter" type="url" url="https://recruit.zoho.com/ats/viewPhoto?filename=tVmTJJk2PIGQSgJSgY2fTzZfMgYE*W1ZBQ.KWiE6q68UOoUiAiY8ElcJtvxybS4bvD4mCWuA8aE-"><![CDATA[samplecoverletter.txt]]></FL>
  59. <FL val="Total work exp (year)"><![CDATA[10]]></FL>
  60. <FL val="Current Role"><![CDATA[Administrative Officer]]></FL>
  61. <FL val="Current Role if Other"><![CDATA[current_role_if_other]]></FL>
  62. <FL val="Current Employer/Customer"><![CDATA[Current_employer_name]]></FL>
  63. <FL val="Skill sets"><![CDATA[skill_sets_goes_here]]></FL>
  64. <FL val="Roles"><![CDATA[Consultant, 93327000000068819, Administrative Officer, Application Developer, Business Analyst, Change Manager, Business Development, Chief Technology Officer, Chief Information Officer, Chief Executive Officer, Data Analyst, Director, IT Manager, Implementation Mgr, Functional Analyst, PMO Manager, Other, Network Admin, Portfolio Manager, Program Manager, Project Admin, Project Manager (Jnr), Project Lead, Project Director, Project Manager (Mid), Project Manager (Snr/Principal), Project Officer, Project Scheduler, Service Delivery Mgr, Release Manager, Quality Manager, Solutions Architect, Systems / Technical Analyst, Systems Administrator, Technical Support, Technical Architect, Systems Engineer, Test Analyst, Test Manager]]></FL>
  65. <FL val="Role (if "Other")"><![CDATA[Role_other]]></FL>
  66. <FL val="Industries"><![CDATA[93327000000068909, 93327000000070037, Aviation, Consulting, Contact Centres, Construction / Engineering, Council, Defense, Education, Energy, Finance/Banking, Gaming, Government, Health / Medical, Hospitality, ICT, Insurance, Manufacturing, Media, Mining, Other, Rail, Retail, Phamacetical, Telecommunications, Transportation, Utilities]]></FL><FL val="Expected Rate/Salary"><![CDATA[147852369]]></FL>
  67. <FL val="Qualifications, Credentials, Memberships"><![CDATA[93327000000068871, 93327000000101075, AABA Membership, Agile PM Certification, AIPM   Membership, CMI Membership, Degree, IIBA Membership, ITIL, Masters Degree, MBA, MSP, PMI Membership, PMP, PRINCE2, RegPM]]></FL>
  68. <FL val="Additional Information"><![CDATA[addational_information_goes_here]]></FL>
  69. <FL val="Attach resume" type="url" url="https://recruit.zoho.com/ats/viewPhoto?filename=tVmTJJk2PIGQSgJSgY2fTzZfMgYE*W1ZBQ.KWiE6q69pGZ2uc5*.nlcJtvxybS4bvD4mCWuA8aE-"><![CDATA[sampleresume.txt]]></FL>
  70. <FL val="Photo (for CV)" type="url" url="https://recruit.zoho.com/ats/viewPhoto?filename=tVmTJJk2PIGQSgJSgY2fTzZfMgYE*W1ZBQ.KWiE6q685xDP3Iw.5GlcJtvxybS4bvD4mCWuA8aE-"><![CDATA[microsoft-gold-partner.png]]></FL>
  71. <FL val="Candidate status"><![CDATA[New]]></FL></row></Candidates></result></response>
  72. HERE;
  73.  
  74. // First deal with problem with Simple XML and CDATA
  75. $sxo = simplexml_load_string($result, null, LIBXML_NOCDATA);
  76.  
  77. // Use these two lines instead of the above line if you are forced to use PHP < 5.1.0 or LibXML < 2.6.0
  78. // $result = preg_replace("/<![CDATA[(.*?)]]>/ies", 'htmlspecialchars("$1")', $result);
  79. // $sxo = simplexml_load_string($result);
  80.  
  81.  
  82. // Find First name
  83. $fn = $sxo->xpath('//FL[@val="First name"]');
  84.  
  85. // Set the child content
  86. $fn[0][0] = "Here's a new name";
  87.  
  88. // Show XML
  89. print_r(htmlentities($sxo->asXML()));
  90.  
  91. ?>
  92.        
  93. $test = '<?xml version="1.0" encoding="UTF-8" ?>
  94.  
  95. <response uri="/ats/private/xml/Candidates/getRecords">
  96. <result>
  97. <Candidates>
  98. <row no="1">
  99. <FL val="RESUMEID"><![CDATA[93327000000109297]]></FL>
  100. <FL val="Modified by"><![CDATA[Not specified]]></FL>
  101. <FL val="Modified time"><![CDATA[Yesterday]]></FL>
  102. <FL val="Candidate ID"><![CDATA[677]]></FL>
  103. <FL val="First name"><![CDATA[TEst1]]></FL>
  104. <FL val="Last name"><![CDATA[TEst2]]></FL>
  105. <FL val="Residental Address"><![CDATA[TESTing res address]]></FL>
  106. <FL val="Email ID"><![CDATA[test1@email.com]]></FL>
  107. <FL val="Mobile Phone"><![CDATA[123456789]]></FL>
  108. <FL val="Created On"><![CDATA[Yesterday]]></FL>
  109. <FL val="Owner"><![CDATA[0]]></FL>
  110. <FL val="Source"><![CDATA[Embed]]></FL>
  111. <FL val="Employment Preference"><![CDATA[Contract (Full Time), Permanent (Full Time), Contract (Part Time), Permanent (Part Time)]]></FL>
  112. <FL val="Cover Letter" type="url" url="https://recruit.zoho.com/ats/viewPhoto?filename=tVmTJJk2PIGQSgJSgY2fTzZfMgYE*W1ZBQ.KWiE6q68UOoUiAiY8ElcJtvxybS4bvD4mCWuA8aE-"><![CDATA[samplecoverletter.txt]]></FL>
  113. <FL val="Total work exp (year)"><![CDATA[10]]></FL>
  114. <FL val="Current Role"><![CDATA[Administrative Officer]]></FL>
  115. <FL val="Current Role if Other"><![CDATA[current_role_if_other]]></FL>
  116. <FL val="Current Employe Customer"><![CDATA[Current_employer_name]]></FL>
  117. <FL val="Skill sets"><![CDATA[skill_sets_goes_here]]></FL>
  118. <FL val="Roles"><![CDATA[Consultant, 93327000000068819, Administrative Officer, Application Developer, Business Analyst, Change Manager, Business Development, Chief Technology Officer, Chief Information Officer, Chief Executive Officer, Data Analyst, Director, IT Manager, Implementation Mgr, Functional Analyst, PMO Manager, Other, Network Admin, Portfolio Manager, Program Manager, Project Admin, Project Manager (Jnr), Project Lead, Project Director, Project Manager (Mid), Project Manager (Snr/Principal), Project Officer, Project Scheduler, Service Delivery Mgr, Release Manager, Quality Manager, Solutions Architect, Systems / Technical Analyst, Systems Administrator, Technical Support, Technical Architect, Systems Engineer, Test Analyst, Test Manager]]></FL>
  119. <FL val="Role (if Other)"><![CDATA[Role_other]]></FL>
  120. <FL val="Industries"><![CDATA[93327000000068909, 93327000000070037, Aviation, Consulting, Contact Centres, Construction / Engineering, Council, Defense, Education, Energy, Finance/Banking, Gaming, Government, Health / Medical, Hospitality, ICT, Insurance, Manufacturing, Media, Mining, Other, Rail, Retail, Phamacetical, Telecommunications, Transportation, Utilities]]></FL><FL val="Expected Rate/Salary"><![CDATA[147852369]]></FL>
  121. <FL val="Qualifications, Credentials, Memberships"><![CDATA[93327000000068871, 93327000000101075, AABA Membership, Agile PM Certification, AIPM   Membership, CMI Membership, Degree, IIBA Membership, ITIL, Masters Degree, MBA, MSP, PMI Membership, PMP, PRINCE2, RegPM]]></FL>
  122. <FL val="Additional Information"><![CDATA[addational_information_goes_here]]></FL>
  123. <FL val="Attach resume" type="url" url="https://recruit.zoho.com/ats/viewPhoto?filename=tVmTJJk2PIGQSgJSgY2fTzZfMgYE*W1ZBQ.KWiE6q69pGZ2uc5*.nlcJtvxybS4bvD4mCWuA8aE-"><![CDATA[sampleresume.txt]]></FL>
  124. <FL val="Photo (for CV)" type="url" url="https://recruit.zoho.com/ats/viewPhoto?filename=tVmTJJk2PIGQSgJSgY2fTzZfMgYE*W1ZBQ.KWiE6q685xDP3Iw.5GlcJtvxybS4bvD4mCWuA8aE-"><![CDATA[microsoft-gold-partner.png]]></FL>
  125. <FL val="Candidate status"><![CDATA[New]]></FL>
  126. </row>
  127. </Candidates>
  128. </result>
  129. </response>';
  130.  
  131. $xml = simplexml_load_string($test, null, LIBXML_NOCDATA);
  132.  
  133. foreach($xml->result->Candidates as $candidate){
  134.     foreach($candidate->row->FL as $FL){
  135.         print "Attributes:n";
  136.         foreach($FL->attributes() as $attribute => $value){
  137.             print $attribute .':'.$value."n";
  138.         }
  139.         print "Value:n";
  140.         print (string)$FL."n";
  141.     }
  142. }
  143.        
  144. if (window.XMLHttpRequest)
  145.   xmlhttp=new XMLHttpRequest();//for IE7+, Firefox, Chrome, Opera, Safari
  146. else
  147.   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");//for IE
  148.  
  149. xmlhttp.open("GET","your_file.xml",false);
  150. xmlhttp.send();
  151. xmlDoc=xmlhttp.responseXML;
  152.        
  153. function xml2array($contents, $get_attributes=1, $priority = 'tag') {
  154.     if(!$contents) return array();
  155.  
  156.     if(!function_exists('xml_parser_create')) {
  157.         //print "'xml_parser_create()' function not found!";
  158.         return array();
  159.     }
  160.  
  161.     //Get the XML parser of PHP - PHP must have this module for the parser to work
  162.     $parser = xml_parser_create('');
  163.     xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); # http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss
  164.     xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
  165.     xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
  166.     xml_parse_into_struct($parser, trim($contents), $xml_values);
  167.     xml_parser_free($parser);
  168.  
  169.     if(!$xml_values) return; //Hmm...
  170.  
  171.     //Initializations
  172.     $xml_array = array();
  173.     $parents = array();
  174.     $opened_tags = array();
  175.     $arr = array();
  176.  
  177.     $current = &$xml_array; //Refference
  178.  
  179.     //Go through the tags.
  180.     $repeated_tag_index = array(); //Multiple tags with same name will be turned into an array
  181.     foreach($xml_values as $data) {
  182.         unset($attributes,$value); //Remove existing values, or there will be trouble
  183.  
  184.         //This command will extract these variables into the foreach scope
  185.         // tag(string), type(string), level(int), attributes(array).
  186.         extract($data); //We could use the array by itself, but this cooler.
  187.  
  188.         $result = array();
  189.         $attributes_data = array();
  190.  
  191.         if(isset($value)) {
  192.             if($priority == 'tag') $result = $value;
  193.             else $result['value'] = $value; //Put the value in a assoc array if we are in the 'Attribute' mode
  194.         }
  195.  
  196.         //Set the attributes too.
  197.         if(isset($attributes) and $get_attributes) {
  198.             foreach($attributes as $attr => $val) {
  199.                 if($priority == 'tag') $attributes_data[$attr] = $val;
  200.                 else $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
  201.             }
  202.         }
  203.  
  204.         //See tag status and do the needed.
  205.         if($type == "open") { //The starting of the tag ''
  206.             $parent[$level-1] = &$current;
  207.             if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag
  208.                 $current[$tag] = $result;
  209.                 if($attributes_data) $current[$tag. '_attr'] = $attributes_data;
  210.                 $repeated_tag_index[$tag.'_'.$level] = 1;
  211.  
  212.                 $current = &$current[$tag];
  213.  
  214.             } else { //There was another element with the same tag name
  215.  
  216.             if(isset($current[$tag][0])) { //If there is a 0th element it is already an array
  217.                 $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
  218.                 $repeated_tag_index[$tag.'_'.$level]++;
  219.             } else { //This section will make the value an array if multiple tags with the same name appear together
  220.                 $current[$tag] = array($current[$tag],$result);//This will combine the existing item and the new item together to make an array
  221.                 $repeated_tag_index[$tag.'_'.$level] = 2;
  222.  
  223.                 if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
  224.                     $current[$tag]['0_attr'] = $current[$tag.'_attr'];
  225.                     unset($current[$tag.'_attr']);
  226.                 }
  227.  
  228.             }
  229.             $last_item_index = $repeated_tag_index[$tag.'_'.$level]-1;
  230.             $current = &$current[$tag][$last_item_index];
  231.         }
  232.  
  233.     } elseif($type == "complete") { //Tags that ends in 1 line ''
  234.         //See if the key is already taken.
  235.         if(!isset($current[$tag])) { //New Key
  236.         $current[$tag] = $result;
  237.         $repeated_tag_index[$tag.'_'.$level] = 1;
  238.         if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data;
  239.  
  240.         } else { //If taken, put all things inside a list(array)
  241.             if(isset($current[$tag][0]) and is_array($current[$tag])) { //If it is already an array...
  242.  
  243.             // ...push the new element into that array.
  244.             $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
  245.  
  246.             if($priority == 'tag' and $get_attributes and $attributes_data) {
  247.                 $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
  248.             }
  249.             $repeated_tag_index[$tag.'_'.$level]++;
  250.  
  251.             } else { //If it is not an array...
  252.                 $current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value
  253.                 $repeated_tag_index[$tag.'_'.$level] = 1;
  254.                 if($priority == 'tag' and $get_attributes) {
  255.                     if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
  256.  
  257.                     $current[$tag]['0_attr'] = $current[$tag.'_attr'];
  258.                     unset($current[$tag.'_attr']);
  259.                 }
  260.  
  261.                 if($attributes_data) {
  262.                     $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
  263.                     }
  264.                 }
  265.                 $repeated_tag_index[$tag.'_'.$level]++; //0 and 1 index is already taken
  266.                 }
  267.             }
  268.         } elseif($type == 'close') { //End of tag ''
  269.         $current = &$parent[$level-1];
  270.         }
  271.     }
  272.     return($xml_array);
  273. }