使用 PHP 编辑和删除 XML 节点

发布于 2024-10-04 02:29:38 字数 1074 浏览 7 评论 0原文

我一直在四处寻找,但仍然找不到在不使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

随风而去 2024-10-11 02:29:38

2 种快速方法,均使用正则表达式:

  • 使用正则表达式将 XML 分解为数组,以entryid 的值作为键
    • 更新时只需构造新值并替换指定键处的值
    • 删除时只需取消设置
  • 使用正则表达式来替换节点

不知道这两种情况的性能。我会尝试提供正则表达式来帮助您。

加布里埃尔

2 quick ways, both using regular expressions:

  • break the XML to array using regular expressions, with the value of the entryid as key
    • on update just construct the new value and replace the value at the specified key
    • on delete just unset
  • use regular expression to replace nodes

No idea on the performance for this two cases. I'll try and provide an regular expression to help you.

Gabriel

辞取 2024-10-11 02:29:38

好吧,抱歉,做了一些进一步的研究,并在 此网站 使用:

$xml_file = "thefile.xml";
if(!$xml=simplexml_load_file($xml_file)){
    trigger_error('Error reading XML file',E_USER_ERROR);
}

simplexml_load_file 而不是 simplexml_load_string 现在就像一个魅力!难怪你们很困惑——感谢你们所有的回复和容忍我的笨蛋!

Ok, sorry, did some further research and found a good example at this site using:

$xml_file = "thefile.xml";
if(!$xml=simplexml_load_file($xml_file)){
    trigger_error('Error reading XML file',E_USER_ERROR);
}

simplexml_load_file instead of simplexml_load_string works like a charm now! No wonder you guys were confused - thanks for all your replies and for tolerating my noobishness!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文