Jump to content

simple dom xml


prometheos

Recommended Posts

Hi, i have an xml in the following format (ratings as root). I want to increment the 'stars' attribute.

<ratings stars="0">
   <rating>
      <experience></experience>
      <comment></comment>
   </rating>
</ratings>

 

Here's what i have to increment the stars attribute, but it's not working? ($experience is a posted variable)

$dom = new DOMDocument();							
$dom->load($forfilename);
if($experience=="Positive"){
	$val += 1;
	$dom->setAttribute('stars', $val);
}else{
	$val -= 1;
	$dom->setAttribute('stars', $val);
}
$dom->save($forfilename);

Can any1 help me :S this should work right? What am i missing?

Thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/198593-simple-dom-xml/
Share on other sites

Check if th file is loading correctly:

 

echo $dom->saveXML();

 

Use that before and after the if condition.

 

Also, are you using some special class, i dont see a 'setAttribute' method listed in the php manual for the default DOMDocument class.

 

-cb-

Link to comment
https://forums.phpfreaks.com/topic/198593-simple-dom-xml/#findComment-1042128
Share on other sites

Thanks for the reply, i realised that about the document class so i changed it to this and its working now!:) thanks

here's what i have :

$xmlDoc = new DOMDocument();
$xmlDoc->load($filename); 

if($experience=="Positive"){
	$searchNode = $xmlDoc->getElementsByTagName("ratings"); 
	$val += 1;
	$ratings = $searchNode->item(0);
	$ratings->setAttribute('stars', $val);

}

 

 

Check if th file is loading correctly:

 

echo $dom->saveXML();

 

Use that before and after the if condition.

 

Also, are you using some special class, i dont see a 'setAttribute' method listed in the php manual for the default DOMDocument class.

 

-cb-

Link to comment
https://forums.phpfreaks.com/topic/198593-simple-dom-xml/#findComment-1042130
Share on other sites

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.