You are here:  » How to get information from XML where fields repeat


How to get information from XML where fields repeat

Submitted by websuperman on Wed, 2008-10-29 15:45 in

<SpecificationList>
- <Specification>
  <SpecificationLabel>Capacity</SpecificationLabel>
  <SpecificationValue>73.4 GB</SpecificationValue>
  </Specification>
- <Specification>
  <SpecificationLabel>Interface</SpecificationLabel>
  <SpecificationValue>2Gb Fibre Channel</SpecificationValue>
  </Specification>
- <Specification>
  <SpecificationLabel>Rotational Speed</SpecificationLabel>
  <SpecificationValue>15000 rpm</SpecificationValue>
  </Specification>
- <Specification>
  <SpecificationLabel>Access Time</SpecificationLabel>
  <SpecificationValue>3.6 ms</SpecificationValue>
  </Specification>
- <Specification>
  <SpecificationLabel>Type of Drive</SpecificationLabel>
  <SpecificationValue>Hot-swap</SpecificationValue>
  </Specification>
  </SpecificationList>

I need help figuring out how to get from this portion of the XML each SpecificationLabel and SpecificationValue.

Submitted by support on Wed, 2008-10-29 15:53

Hi,

I'm assuming that this is part of the record you are interested in, so it is not practical to parse the XML at this level. In that case, based on the sample of XML above, the easiest way to handle this within a Magic Parser record handler function is to loop through every value in $record, picking out pairs of SpecificationLabel and SpecificationValue; for example (non-complete):

<?php
  
function myRecordHandler($record)
  {
    
$specificationLabel "";
    
$specificationValue "";
    foreach(
$record as $key => $value)
    {
      if (
strpos($key,"SPECIFICATIONLABEL")!==FALSE$specificationLabel $value;
      if (
strpos($key,"SPECIFICATIONVALUE")!==FALSE$specificationValue $value;
      if (
$specificationLabel && $specificationValue)
      {
        
// use $specificationLabel and $specificationValue here
        // reset for next pair
        
$specificationLabel "";
        
$specificationValue "";
      }
    }
  }
?>

Hope this helps!
Cheers,
David.

Submitted by websuperman on Wed, 2008-10-29 19:13

That worked great. Thanks, all I had to do was get it to display and it worked well. Now to figure out how to put all this data into the database. As I am learning php this will certainly prove to be fun! :) Thanks again!