DOM attribute problem

I want to collect the data from an XML feed to insert into a MySQL database, but I can’t seem to get the Node value of an attribute to insert in to a database, as it is an object and not a string although the variable will echo and print. I can not seem to convert this to a string…


$years = $findit->getElementsByTagName("vehicle")->item(0);
$year =  $years->getAttribute('year');

This produces… Catchable fatal error: Object of class DOMAttr could not be converted to string…

But the element by tag name works without any problem…


$manufacturers = $findit->getElementsByTagName( "manufacturer" );
$manufacturer = $manufacturers->item(0)->nodeValue;

Can someone help or explain why the element object and attribute object work differently.

Thanks


$years = $findit->getElementsByTagName("vehicle")->item(0);
$year =  $years->getAttribute('year')->value;

The getAttribute method call is returning a DOMAttr object, as this has no __toString magic method you cannot cast it.

Hi Anthony

Thanks for a quick reply but…

$year =  $years->getAttribute('year')->value;

Still returns the same Error Code? So any ideas or options how to get around this?

Can you modify the following to suit?


<?php
$xml = '<root>
  <users>
    <user name="Anthony" />
  </users>
</root>
';

$dom = new DOMDocument;
$dom->loadXML($xml);

$users = $dom->getElementsByTagName('user');

for($index = 0; $index < $users->length; $index++){

  $user = $users->item($index);

  printf(
    '$user is a "%s" and the attribute "name" contains the value "%s", and it is a "%s".' . "\
",
    get_class($user),
    $user->getAttribute('name'),
    gettype($user->getAttribute('name'))
  );

}

/*
  $user is a "DOMElement" and the attribute "name" contains the value "Anthony", and it is a "string".
*/

Welcome to SitePoint BTW. :stuck_out_tongue:

Anthony

Yes only just joined SitePoint, well we all need some help sometime and this is annoying me!

Your code obviously worked fine on it’s own so this is how I tried it to work within my script.

 <?php

$xml = '
<root>  
	<users>    
		<user name="Anthony" />  
	</users>
</root>';

$dom = new DOMDocument;$dom->loadXML($xml);
 
 $users = $dom->getElementsByTagName('user');
 for($index = 0; $index < $users->length; $index++){  
 $user = $users->item($index);  
  get_class($user);    
  $user->getAttribute('name');    
  gettype($user->getAttribute('name'))  ;
  }
$test = $user->getAttribute('name');  
	echo $test;
  
  
  ?>

But, I still have the same problem, prints OK but still get the Catchable fatal error: when inserting into MySQL.

Hi

Sorry I’ll take that back the above works fine, but not working when transcribed to my script!

I need to look a bit harder!

Thanks Anthony your code does work but made me realise the problem was not where I was looking.

It seems that any fault produces Error warnings about objects and strings when the answer is much easier.

This works if it helps anyone else…

<?php  $xml = ' 
 <root>       
  <users>             
   <user name="Anthony" name2="Smith" />       
   <user name="Tony" name2="Sykes" />       
   </users> 
  </root>';  
  
    $dom = new DOMDocument;$dom->loadXML($xml);  
  

  $user = $dom->getElementsByTagName('user')->item(0);
  $user = $dom->getElementsByTagName( "user" );
	foreach( $user as $user )
	{

  $name = $user->getAttribute('name');
  $name2 = $user->getAttribute('name2');

  
  echo $name. " " .$name2. "<br />";        
}     

 ?>