Total members 11793 |It is currently Fri Aug 23, 2019 1:00 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





The XML file :
Code:
<!--
<contact id="43956">
     <personal>
          <name>
               <first>J</first>
               <middle>J</middle>
               <last>J</last>
          </name>
          <title>Manager</title>
          <employer>National</employer>
          <dob>1971-12-22</dob>
     </personal>
</contact>
-->

The php file :
Code:
<html>
<body>
<?php
     $cdata 
= '';
     $contact_id = -1;
     $contacts = null;
   
     function start_elem 
($parser, $name, $attrs) {
          global $contact_id;
          if ($name == 'contact') {
               $contact_id = $attrs['id'];
               $contacts[$contact_id] = array();
          }
     }
     function end_elem ($parser, $name) {
          global $cdata, $contacts, $contact_id;
          switch ($name)
          {
               case ("first"):
                    $contacts[$contact_id]['name']['first'] = $cdata; 
                    break
;
               case ("last"):
                    $contacts[$contact_id]['name']['last'] = $cdata; 
                    break
;
               case ("middle"):
                    $contacts[$contact_id]['name']['middle'] = $cdata; 
                    break
;
               case ("title"):
                    $contacts[$contact_id]['title'] = $cdata; 
                    break
;
               case ("employer"):
                    $contacts[$contact_id]['employer'] = $cdata; 
                    break
;
               case ("dob"):
                    $contacts[$contact_id]['dob'] = $cdata; 
                    break
;
          }
          $cdata = '';
     }
     function cdata ($parser, $data) {
          global $cdata;
          $cdata .= trim ($data);
     }
     $file = "contact.xml";
     $parser = xml_parser_create ();
     xml_parser_set_option ($parser, XML_OPTION_CASE_FOLDING, false);
     xml_set_element_handler ($parser, "start_elem", "end_elem");
     xml_set_character_data_handler ($parser, "cdata");
     $fp = fopen ($file, "r");
     if (!$fp){
          die ("Couldn't open $file for reading");
     }
     while ($xml_input = fread ($fp, 2048)) {
          $ok = xml_parse ($parser, $xml_input, feof ($fp));
          if (!$ok){
               die (sprintf ("Error in $file: '%s' at line %d",
                             xml_error_string (xml_get_error_code ($parser)),
                             xml_get_current_line_number ($parser)));
          }
     }
     xml_parser_free ($parser);
     print ("<pre>\n");
     print_r ($contacts);
     print ("</pre>\n");
?>
</body>
</html>





Author:
Mastermind
User avatar Posts: 2707
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : XML Document into an Array in php
 Document Object Write Function in JavaScript     -  
 Array difference for associate array     -  
 compare an array with another array?     -  
 Array Passing     -  
 Imploding an Array     -  
 array unshift     -  
 Average of an array. Please help     -  
 Add elements to the end of an array     -  
 Get array chunk     -  
 Array sort     -  



Topic Tags

PHP XML, PHP Arrays






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com