Re: Problem with SimpleXML

From: Date: Wed, 24 Aug 2005 19:04:28 +0000
Subject: Re: Problem with SimpleXML
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-221297@lists.php.net to get a copy of this message
Yes, simplexml can do this easily. See: http://www.php.net/simplexml
Example 7. Setting values Data in SimpleXML doesn't have to be constant. The object allows for manipulation of all of its elements. <?php include 'example.php'; $xml = simplexml_load_string($xmlstr); $xml->movie[0]->characters->character[0]->name = 'Miss Coder'; echo $xml->asXML(); ?> The above code will output a new XML document, just like the original, except that the new XML will change Ms. Coder to Miss Coder.
Uros, it looks like you want to change one of the attributes; you will need to use some of this code to handle the attributes:
Example 4. Using attributes So far, we have only covered the work of reading element names and their values. SimpleXML can also access element attributes. Access attributes of an element just as you would elements of an array. <?php include 'example.php'; $xml = simplexml_load_string($xmlstr); /* Access the <rating> nodes of the first movie. * Output the rating scale, too. */ foreach ($xml->movie[0]->rating as $rating) { switch((string) $rating['type']) { // Get attributes as element indices case 'thumbs':
       echo $rating, ' thumbs up';
       break;
case 'stars':
       echo $rating, ' stars';
       break;
} } ?>
regards, Jordan On Aug 24, 2005, at 3:20 AM, Uroš Gruber wrote:
Hi! I have XML and I would like to set some values. I almost done the whole thing but have some problems when looping through some tags <foo> <bar id="1" name="bar1" /> <bar id="2" name="bar2" /> ... </foo> I would like to set value for tag "bar" in some loop and then export this back to XML. Is this even possible or it's better to use SimpleXML only for read and create new XML from it. XML is about 20 rows of data and I'm using PHP 5.0.4 with all XML included. regards Uros --PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


« previous php.general (#221297) next »