hmdnawaz Posted December 24, 2010 Share Posted December 24, 2010 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'); } ?> Link to comment https://forums.phpfreaks.com/topic/222565-fatal-error-call-to-a-member-function-getattributenode-on-a-non-object-in-d/ Share on other sites More sharing options...
the182guy Posted December 24, 2010 Share Posted December 24, 2010 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. Link to comment https://forums.phpfreaks.com/topic/222565-fatal-error-call-to-a-member-function-getattributenode-on-a-non-object-in-d/#findComment-1151103 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.