如何获取 simpleXML 来创建 ID
我有一个 extiing xml 文件,其 id 为 1 - 20,当我创建新的 xml 文件时,我需要它来将 ID 添加到文件中,我该怎么做?
我知道你会如何使用 SQL 来做到这一点,但不知道如何使用 xml
请帮忙
I have an exctiing xml file which has id's 1 - 20 when I create a new xml file I need it to add the ID to the file how can I do this ?
I know how you would do it with SQL but not with xml
please help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你的xml已经从最低到最高排序,你可以执行这个xpath查询:
否则,你可以像这样找到最大id(假设id是一个属性):
上面打印“3”,所以你的新id是4
或使用 Veger 的解决方案 也可以
if your xml is already ordered from lowest to highest, you can do this xpath query:
otherwise, you can find the max id like so (assuming id is an attribute):
the above prints "3", so your new id is 4
or use Veger's solution which will also work
使用一些 XPath 查询和 php 代码查找现有文件中的最高 ID。
我对 xpath 查询不是很了解,因此我想在查询期间可能有一些技巧对其进行排序,但如果可以优化此示例,其他人可能会指出这一点。
接下来创建新的 XML 文档并向其添加最高 ID。
Find the highest ID in your existing file, using some XPath query and php code.
I am not very knowledgeable about xpath queries, so there might be some trick to sort it during the query I suppose, but someone else will probably point this out if this example can be optimised.
Next create your new XML document and add the highest ID to it.