使用 simpleXML 编辑 XML
如何使用 simpleXML 编辑 xml 文件中的值?
我知道如何创建文件,但不知道如何编辑现有文件中的值?
How can I edit the value's in a xml file using simpleXML ?
I know how to create the file, but not how to edit the value in an existing file ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当然,您可以使用 SimpleXML 进行编辑:
看看 示例。
Sure you can edit with SimpleXML:
Take a look at the examples.
使用 SimpleXML 加载 XML 并进行更改。然后您可以使用 asXML 方法将 XML 保存到文件中 (您传递文件名作为参数):
Load your XML with SimpleXML and make the changes. Then you can use the asXML method to save the XML to a file (you pass the filename as the argument):
请记住,虽然您可以使用 SimpleXML 编辑 XML,但也有一些限制。例如,您可以移除或删除节点或元素。您可以将其清除,使其变为空白,但无法完全消除它。为此,您需要 DOM 或类似的东西。
Keep in mind that although you can edit XML with SimpleXML, there are limitations. For example, you can remove or delete a node or element. You can clear it so that its blank, but you can't eliminate it altogether. For that, you need DOM, or something like that.
我正在这样工作(它完全相同,但它可能有帮助):
文件 test.xml 可以是任何扩展名,只要它是纯 xml 文本即可。
test.xml:
和 PHP 代码:
结果 test.xml:
希望有帮助!
I am working like this (it's quite the same but it could help):
The file test.xml could be any extension as long as it's a plain xml text.
test.xml:
And the PHP code:
Results test.xml:
Hope it helps!