用 PHP 操作 XML [英] Manipulating XML with PHP

查看:30
本文介绍了用 PHP 操作 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的 XML 文件

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?

我所看到的每个地方都在提到simpleXML",但是我只能弄清楚如何创建原始文件,我似乎无法弄清楚如何编辑.

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.

推荐答案

是的,我有点同意 PHP 中的 xml 库文档不太直观.

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");

这篇关于用 PHP 操作 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆