tags:

views:

60

answers:

3

I have an XML file that looks like this

<library>
   <book>
      <title>Title</title>
      <author>author</author>
      <price>20</price>
   <book>
<library>

I want to be able to add a book and be able to change the price.

Does anybody have any incite on how to do this?

Everywhere were i look keeps referring to "simpleXML" however I can only figure out how to create the original file i cant seem to figure out how to edit.

+1  A: 

Use simplexml_load_string or simplexml_load_file to read a file into an object you can work with.

bemace
+1  A: 

I use:

$simpleXML = new SimpleXMLElement($stringXML);

To add a value you could:

$simpleXML->addChild('Value', $value);

then to get a string back:

$stringXML = $simpleXML->asXML();
Ryan Bavetta
So lets say I have this saved in a file called test.XML I would say:........................................................................... $SimpleXML = new SimpleXMLElement( 'text.XML' );
Forgoten Dynasty
+1  A: 

Yeah, i kinda agree that xml libraries documentation in PHP is not so intuitive to get going with.

$xml = simplexml_load_file("file.xml"); 

//add   
$book = $xml->addChild("book"); 
$book->addChild("title", "TTT"); 
$book->addChild("author", "AAA"); 
$book->addChild("price", 123); 

//edit 1st:
$xml->book[0]->price = "priceless";

//write result
$xml->asXML("result.xml");
Imre L
Thank you so much :)
Forgoten Dynasty