Jump to content

Fatal error: Call to a member function getAttributeNode() on a non-object in D:


hmdnawaz

Recommended Posts

here is my php code. i want to remove the attribute of a tag and then add a new attribure to the tag. simply i want attribute validation.

 

but this code gives me the following error.

"Fatal error: Call to a member function getAttributeNode() on a non-object in D:\wamp\www\ReadXml\new.php on line 35"

 

 

 

 

<?php

$xml = new DOMDocument();

$doc->recover = true;

$xml->load('test.xml');

 

$node = $xml->getElementsByTagName('*');

 

$nodename = "";

$node_name = array();

$att = "";

$attr_name = "";

$attr_value = "";

 

foreach($node as $value)

{

 

echo "<HTML><Head>";

echo "<title> Getting Attribute Example</title>";

echo "</Head><body><B>";

echo "Node Name is :".$value->nodeName;

 

echo "<br /><br /></B></body></HTML>";

 

 

 

$nodename = $value->nodeName;

$node_name = $xml->getElementsByTagName('$nodename')->item(0);

echo  $nodename;

 

$att = $node_name->getAttributeNode('name'); //Line No 35

 

 

echo "<HTML><Head>";

echo "<title> Getting Attribute Example</title>";

echo "</Head><body><B>";

echo "Node Name is :".$att->name;

echo "<BR>Attribute Value is :".$att->value;

echo "<br /><br /></B></body></HTML>";

 

$attr_name = $att->name;

$attr_value = '"' . $att->value . '"';

$nodename->removeAttributeNode('$attr_name');

 

$nodename->setAttributeNode(new DOMAttr('$attr_name', 'hello'));

$nodename->setAttribute(new DOMAttr('$attr_name', '$attr_name'));

 

 

$xml->save('temp2.xml');

}

?>

Remove the single quotes from line 33, there is no need to put variables in quotes.

 

Remember that a variable in single quotes will never work because php does not recognise variables in single quotes. Php treats the variable as a string literal.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.