使用 PHP 编辑和删除 XML 节点
我一直在四处寻找,但仍然找不到在不使用 simpleXML 的情况下使用 php 编辑和删除 xml 的可行答案(我希望我可以使用它)。我希望有人能为我简单地分解它,因为我的大脑无法处理这些东西!我必须使用的 xml 文件看起来像这样:
<allentries>
<entry>
<entryid>1</entryid>
<title>This is the title</title>
<date>2010</date>
<author>Some Guy</author>
</entry>
<entry>
<entryid>2</entryid>
<title>This is Another title</title>
<date>2011</date>
<author>Some Other Guy</author>
</entry>
<entry>
<entryid>3</entryid>
<title>This is the other title</title>
<date>2012</date>
<author>And Another Guy</author>
</entry>
</allentries>
我需要能够使用 PHP 做两件事 - 首先,我需要能够接受从表单发布的值,根据条目 ID 号选择正确的条目并修改该条目中的每个元素都包含新数据。例如。如果entryid = 2,则跳至第二个条目并用新数据替换标题、日期和作者中的文本。
我需要做的第二件事(显然具有不同的功能)是根据条目 ID 号选择条目并删除整个条目、标签、数据、子项等。
听起来应该不是太难,但到目前为止我使用的每个示例都没有做任何事情。有人可以建议任何可能对我有帮助的东西或任何其他教程吗?
非常感谢您的宝贵时间!
I've been looking around but still can't find a workable answer to editing and deleting xml with php without the use of simpleXML (how I wish i could use it). I was hoping someone could break it down simply for me, as my brain doesn't cope with this stuff! The xml file I have to work with looks something like this:
<allentries>
<entry>
<entryid>1</entryid>
<title>This is the title</title>
<date>2010</date>
<author>Some Guy</author>
</entry>
<entry>
<entryid>2</entryid>
<title>This is Another title</title>
<date>2011</date>
<author>Some Other Guy</author>
</entry>
<entry>
<entryid>3</entryid>
<title>This is the other title</title>
<date>2012</date>
<author>And Another Guy</author>
</entry>
</allentries>
I need to be able to do two things with PHP - Firstly I need to be able to accept values posted from a form, pick the correct entry based on the entryid number and modify each element within that entry with the new data. For eg. if entryid = 2, skip to the second entry and replace the text within title, date and author with the new data.
The second thing I need to be able to do (with a different function obviously) is pick the entry based on the entryid number and delete the whole entry, tag, data, children and all.
It sounds like it shouldn't be too hard, but every example I've used so far fails to do anything. Can anybody suggest anything or any other tutorials somewhere that may help me?
Thanks muchly for your time!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
2 种快速方法,均使用正则表达式:
不知道这两种情况的性能。我会尝试提供正则表达式来帮助您。
加布里埃尔
2 quick ways, both using regular expressions:
No idea on the performance for this two cases. I'll try and provide an regular expression to help you.
Gabriel
好吧,抱歉,做了一些进一步的研究,并在 此网站 使用:
simplexml_load_file
而不是simplexml_load_string
现在就像一个魅力!难怪你们很困惑——感谢你们所有的回复和容忍我的笨蛋!Ok, sorry, did some further research and found a good example at this site using:
simplexml_load_file
instead ofsimplexml_load_string
works like a charm now! No wonder you guys were confused - thanks for all your replies and for tolerating my noobishness!