rick
rick

Reputation: 33

retrieving xml data

i want to retrieve data from xml file, i need to echo the attribute value of product

data.xml file----

<products>
<product  id="123"   />
</products>

php file---

$xml = new DomDocument();
$xmlFile = "data.xml";          
$xml= DOMDocument::load($xmlFile);          
$product = $xml->getElementsByTagName("product");    
foreach($product as $node)            
  {          
$id = $node->getElementsByAttributeName("id");         
$id = $address->item(0)->nodeValue;           
echo"$id";             
  } 

Upvotes: 0

Views: 219

Answers (2)

tradyblix
tradyblix

Reputation: 7579

Use getAttribute:

$id = $node->getAttribute("id");
echo $id;

You may also want to refer to the manual for other functions that you need ;)

Upvotes: 0

Blender
Blender

Reputation: 298562

I've never heard of getElementsByAttributeName(), but if you want to just get the attribute of an element, the function is quite simple:

$xml = new DomDocument();
$xmlFile = "data.xml";          
$xml= DOMDocument::load($xmlFile);          
$product = $xml->getElementsByTagName("product");

foreach($product as $node) {          
  $id = $node->getAttribute("id");          
  echo $id;             
} 

Upvotes: 1

Related Questions