Help with extracting xml attributes using php

Hi, I’m having trouble extracting attributes from an xml file. I have found a script to extract data however in my xml file there are tags with attributes that I would like to extract separtely.

Here’s the xml structure:

<?xml version=“1.0” encoding=“utf-8”?>
<zanox>
<product id=“1234567890” number=“000.000.000”>
<affiliateprogram id=“100”/>
<info>
<name>Product name 1</name>
<description state=“short”>Short description 1</description>
<description state =“long”>Long description 1</description>
<manufacturer>Manufacturer 1</manufacturer>
</info>
<category>
<merchant>Merchant 1</merchant>
</category>
<links>
<deeplink>Link 1</deeplink>
<image state=“small”>
<url>Image Small 1</url>
</image>
<image state=“medium”>
<url>Image Large 1</url>
</image>
</links>
<state>1</state>
<date state=“update”>2009-01-20T16:11:00</date>
<offer>
<currentprice currency=“EUR”>120.00</currentprice>
</offer>
</product>
<product id=“1234567891” number=“000.000.001”>
<affiliateprogram id=“101”/>
<info>
<name>Product name 2</name>
<description state=“short”>Short description 2</description>
<description state =“long”>Long description 2</description>
<manufacturer>Manufacturer 2</manufacturer>
</info>
<category>
<merchant>Merchant 2</merchant>
</category>
<links>
<deeplink>Link 2</deeplink>
<image state=“small”>
<url>Image Small 2</url>
</image>
<image state=“medium”>
<url>Image Large 2</url>
</image>
</links>
<state>1</state>
<date state=“update”>2009-01-20T16:11:00</date>
<offer>
<currentprice currency=“EUR”>120.00</currentprice>
</offer>
</product>
</zanox>

and here’s the script I’m using to extract data:

<?php
$doc = new DOMDocument();
$doc->load( ‘ProductData3.xml’ );

$products = $doc->getElementsByTagName( “product” );
foreach( $products as $product )

{
$names = $product->getElementsByTagName( “name” );
$name = $names->item(0)->nodeValue;

$descriptions = $product->getElementsByTagName( “description” );
$description = $descriptions->item(0)->nodeValue;

$deeplinks = $product->getElementsByTagName( “deeplink” );
$deeplink = $deeplinks->item(0)->nodeValue;

$urls = $product->getElementsByTagName( “url” );
$url = $urls->item(0)->nodeValue;

echo "<table width=‘500’><tr style=‘background-color:silver;’><td>$name</td></tr><tr><td>$description</td></tr><tr><td><a href=‘$deeplink’ target=‘_blank’><img src=‘$url’ border=‘0’/></a></td></tr><tr><td><a href=‘$deeplink’ target=‘_blank’>Buy</a></td></tr></table>
";
}

?>

It would be great if any one could help or maybe you’ve got a much better idea of how to extract the data, but I need to be able to define which data is to be extracted. Thanks!

You can use attribute-related method of DOMElement: http://php.net/manual/en/class.domelement.php


$id = $product->getAttribute('id');

should give you the “id” attribute value of the product.

Thanks! I’ll give it a try