You are here:  » XML with Parent & Child values


XML with Parent & Child values

Submitted by dougb on Thu, 2007-02-08 12:54 in

Hi there,

I'm new to this parsing thing, I have an XML file that has Products with options

http://www.irishop.com/objinfo.xml

I want to print out the product with the option value like:

Description: 10ct Celtic Wedding Set
Options: Ladies Size 4 4.5 5 6 7 8 Gents Size 9 10 11 12 13

Is this possible?

Thanks,
Doug

Submitted by support on Thu, 2007-02-08 12:59

Hello Doug,

If the options are child elements of each product record it is normally straight forward to do this.

Can you post an example of one of the records in your XML?
(use the <code> tags when posting XML in the forum)

Alternatively, if you can email me the file (reply to your reg code or forum registration email is the easiest way) I will take a look and post some example code for you...

Cheers,
David.

Submitted by dougb on Thu, 2007-02-08 14:48

Hi David,

Thanks for your quick reply. Here is an example

<Product Id="10ceweset">
<Code>800-10R593-10R593</Code>
<Description>10ct Celtic Wedding Set</Description>
<Url>http://www.irishshop.com/10ceweset.html</Url>
<Thumb>
<img border=0 width=70 height=42 src=http://us.st11.yimg.com/us.st.yimg.com/I/irishop_1933_121242>
</Thumb>
<Picture>
<img border=0 width=600 height=360 src=http://us.st11.yimg.com/us.st.yimg.com/I/irishop_1933_123713>
</Picture>
<Orderable>YES</Orderable>
<Taxable>YES</Taxable>
<Pricing>
<BasePrice>759.98</BasePrice>
<LocalizedBasePrice>759.98</LocalizedBasePrice>
</Pricing>
<Path>
<ProductRef Id="jewelry" Url="http://www.irishshop.com/jewelry.html">Irish and Celtic Jewelry</ProductRef>
<ProductRef Id="weddingsets" Url="http://www.irishshop.com/weddingsets.html">Wedding Ring Sets</ProductRef>
</Path>
<Caption>
An incricate Celtic Design accentuates the beauty of this 10ct Gold wedding set. The Ladies ring measures approx. 5mm wide and the Gents measures approx. 6.5mm wide.
</Caption>
<OptionLists>
<OptionList name="Ladies Size">
<OptionValue>4</OptionValue>
<OptionValue>4.5</OptionValue>
<OptionValue>5</OptionValue>
<OptionValue>5.5</OptionValue>
<OptionValue>6</OptionValue>
<OptionValue>6.5</OptionValue>
<OptionValue>7</OptionValue>
<OptionValue>7.5</OptionValue>
<OptionValue>8</OptionValue>
<OptionValue>8.5</OptionValue>
<OptionValue>9</OptionValue>
</OptionList>
<OptionList name="Gents Size">
<OptionValue>9</OptionValue>
<OptionValue>9.5</OptionValue>
<OptionValue>10</OptionValue>
<OptionValue>10.5</OptionValue>
<OptionValue>11</OptionValue>
<OptionValue>11.5</OptionValue>
<OptionValue>12</OptionValue>
<OptionValue>12.5</OptionValue>
<OptionValue>13</OptionValue>
</OptionList>
</OptionLists>
</Product>

Submitted by support on Thu, 2007-02-08 15:08

Hi Doug,

The trick with XML such as this is to loop through each key => value pair in $record looking for the key names of the values you want. For example, to extract the option list from your sample XML, you could do it like this:

<?php
  
require("MagicParser.php");
  function 
myRecordHandler($record)
  {
    foreach(
$record as $k => $v)
    {
      if (
substr($k,0,11)=="OPTIONLISTS")
      {
        if (
strpos($k,"NAME"))
        {
          print 
"<br />".$v." : ";
        }
        if (
strpos($k,"VALUE"))
        {
          print 
$v."&nbsp;";
        }
      }
    }
  }
  
MagicParser_parse("products.xml","myRecordHandler","xml|PRODUCTS/PRODUCT/");
?>

Click here to see the output of this script running on this server

To make this work, I have saved your XML snippet into a file called products.xml; enclosing the product record in a top level element called "products". If the top level element in your source file is different, simply adjust the format string as required.

Hope this helps!
Cheers,
David.

Submitted by dougb on Thu, 2007-02-08 16:09

Thanks so much David, that's perfect!