Solved

In PHP4 versus PHP5, use simplexml, domxml or xml?

Posted on 2008-06-13
4
1,212 Views
Last Modified: 2008-06-19
Im creating a script that pulls xml data from a central server, then stores it locally to be read. The script will resides on 100's of different servers. Some of these servers have php4, some have php5, some have domxml, some have simplexml, some have regular xml... Ive done what i needed to in domxml and in simple xml... My question is does anyone have a easy way to match the following examples...

CODE EXAMPLE 1: SERVER WITH SIMPLEXML RUNS...
  $domaininfo = simplexml_load_file($localxmlfile);

CODE EXAMPLE 2: SERVER WITH DOM XML RUNS...
  $xml = file_get_contents($localxmlfile);
  $doc = domxml_open_mem($xml); //(PHP 4 >= 4.2.0)
  $domaininfo = xmlnode2array($doc->root());

I need domaininfo to be an array containing the xml data... the xml data has children, its not just one level deep... heres an example of the data being pulled...

EXAMPLE XML:
<domaininfo>
<nid>378</nid>
<title>Best+Web</title>
<created>1212617890</created>
<changed>1212799071</changed>
<url>http://www.domain.com</url>

<flash_header>
<nid>376</nid>
<title>Digital+Media+2</title>
<created>1212617812</created>
<changed>1212787780</changed>
<movie>
/flash/flas2.swf
</movie>
<checksum>fb539422b5f4cbd1e0b1437f98859857</checksum>
<size>529991</size>
<width>780</width>
<height>300</height>
</flash_header>

<style_sheet>
<nid>370</nid>
<title>Basic</title>
<created>1212612552</created>
<changed>1212788566</changed>
<style>
some style sheet code
</style>
</style_sheet>

<pages>

<page>
<nid>379</nid>
<title>Media+Design</title>
<field_weight>2</field_weight>
<url>mediadesign.html</url>
</page>

<page>
<nid>380</nid>
<title>Design+Services</title>
<field_weight>0</field_weight>
<url>designservices.html</url>
</page>

<page>
<nid>381</nid>
<title>Design+Tools</title>
<field_weight>5</field_weight>
<url>graphic-design-tools.html</url>
</page>

</pages>

</domaininfo>


WHAT I NEED:
i need a NON domxml and NON simplexml example that will work in php4 of how to convert xml into an array... can you help point me in the right direction and/or provide an example??
0
Comment
Question by:cimmer
  • 2
4 Comments
 
LVL 10

Assisted Solution

by:ray-solomon
ray-solomon earned 100 total points
ID: 21784067
0
 

Accepted Solution

by:
cimmer earned 400 total points
ID: 21789564
i looked at that class and used part of it to write my own... i added lines to the start and data functions to check for the term page since i had more that 1 "pages" and stored them as their own array 0,1,2,3,4, etc... otherwise each page just overwrites the last one in the array since the key would be the same...  my solution is as follows....

$xmlArray=array();
startParser($localxmlfilepath);
print_r($xmlArray);

// Starts the XML parser
   function startParser($xmlFile)
   {

      $xmlParser = xml_parser_create();

      xml_parser_set_option($xmlParser, XML_OPTION_CASE_FOLDING, false);
      xml_set_element_handler($xmlParser, startElement, endElement);
      xml_set_character_data_handler($xmlParser, characterData);

      $fp = fopen($xmlFile, "r");

      while($data = fread($fp, filesize($xmlFile))){
         xml_parse($xmlParser, $data, feof($fp));}

      xml_parser_free($xmlParser);
     

   }

   // Sets the current XML element, and pushes itself onto the element hierarchy
   function startElement($parser, $name, $attrs)
   {

      global $currentElements, $itemCount;

      array_push($currentElements, $name);
      if($name == "page"){
      $itemCount += 1;
      }

   }

   // Checks and adds XML elements to array
   function characterData($parser, $data)
   {

      global $currentElements, $xmlArray, $itemCount;

      $currentCount = count($currentElements)-1;
      $parentElement = $currentElements[$currentCount-1];
      $thisElement = $currentElements[$currentCount];

      if($parentElement == "page"){
         $xmlArray[$parentElement][$itemCount-1][$thisElement] = $data;
      }else{
        $xmlArray[$parentElement][$thisElement] = $data;
      }
         
   }

   // If the XML element has ended, it is poped off the hierarchy
   function endElement($parser, $name)
   {

      global $currentElements;

      $currentCount = count($currentElements)-1;
      if($currentElements[$currentCount] == $name){
         array_pop($currentElements);}

   }
0
 
LVL 10

Expert Comment

by:ray-solomon
ID: 21790342
I have no objection. I am happy figured it out.
:-)

-Ray
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
mysql update statement 3 23
AWS EC2 & RDS Instance 5 36
Eloquent ORM manual paginator defaults to simple 2 25
how to use Initialization Vector for openssl_encrypt() 5 28
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question