![](/img/trans.png)
[英]XML with xpath and PHP: How to access the text value of an attribute of an entry
[英]Access and update attribute value in XML using XPath and PHP
这是我今天上一篇帖子的扩展,可以在这里找到: 单击此处
现在,我想基于id属性访问另一个属性,并对其进行更改。 这是我修改后的xml文件:
<?xml version="1.0" encoding="ISO-8859-1"?>
<document>
<item id="a12sd">
<name>James</name>
<age years="25"/>
<pdf>0023.pdf</pdf>
</item>
<item id="rdf23">
<name>Alex</name>
<age years="35"/>
<pdf>0178.pdf</pdf>
</item>
<item id="2we34">
<name>Tom</name>
<age years="25"/>
<pdf>0886.pdf</pdf>
</item>
<item id="123de">
<name>Robby</name>
<age years="28"/>
<pdf>1239.pdf</pdf>
</item>
</document>
我已经可以使用以下代码更新标签:
$id = "a12sd";
$xml = simplexml_load_file('items.xml');
$itemsList = $xml->xpath('/document/item[@id = "a12sd"]');
$itemsList[0]->name = "Arnold";
$xml->asXml("items.xml");
现在,我需要访问id =“ a12sd”的属性age并更新年龄,例如:26
任何想法,我如何可以达到给定ID的属性并更改该值。
注意:在更改值之前,用户仅知道“ id”值,因此基于id值,我想更改该特定id的years属性。
谢谢
$itemsList[0]->age->attributes()->years = 26;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.