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 ref开发者_StackOverflow中文版erring to "simpleXML" however I can only figure out how to create the original file i cant seem to figure out how to edit.
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");
Use simplexml_load_string or simplexml_load_file to read a file into an object you can work with.
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();
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论