使用 php Xpath 更新 xml 文件 [英] update xml file with php Xpath

查看:28
本文介绍了使用 php Xpath 更新 xml 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 Xpath (php) 更新文件?我的文件结构:

How to use use Xpath (php) update file? My file structure:

<?xml version="1.0" encoding="ISO-8859-1"?>
<PersonList>
  <Person>
    <Name>Sonu Kapoor</Name>
    <Age>24</Age>
    <Gender>M</Gender>
    <PostalCode>54879</PostalCode>
  </Person>
  <Person>
    <Name>Jasmin</Name>
    <Age>28</Age>
    <Gender>F</Gender>
    <PostalCode>78745</PostalCode>
  </Person>
   <Person>
    <Name>Josef</Name>
    <Age>232</Age>
    <Gender>F</Gender>
    <PostalCode>53454</PostalCode>
  </Person>
</PersonList>

而且我需要更改名称为Jasmin"的年龄和性别值.我尝试使用谷歌,但没有找到任何好处:(

And i need change values Age and Gender where name is "Jasmin". I try use google, but nothing good no found :(

推荐答案

你可以试试simplexml

$xml = simplexml_load_string($str);
$obj = $xml->xpath('//Person[Name="Jasmin"]');
$obj[0]->Age = 30;
$obj[0]->Gender = 'M';
echo $xml->asXml();

/* or */ 
$xml->asXml($filename); <-- save xml into file

这篇关于使用 php Xpath 更新 xml 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆