PHP的SimpleXML,如何设置属性? [英] PHP SimpleXML, how to set attributes?

查看:166
本文介绍了PHP的SimpleXML,如何设置属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你有类似的东西,

<hello id="1" name="myName1">
 <anotherTag title="Hello">
 </anotherTag>
</hello>
<hello id="2" name="myName2">
 <anotherTag title="Hi">
 </anotherTag>
</hello>

如何更改的属性,例如,你好ID 2,名称=威廉?或标题喜来喂?

How to change the attributes of, for example, hello id 2, to name="William" ? Or the title hi to hello ?

非常感谢您atention,
H'

Thanks a lot for your atention, H'

推荐答案

记住,你的XML文档必须有一个根元素:

Remember, your XML document has to have a root element:

$xml = simplexml_load_string("<root>$string</root>");
$xml->hello[1]['name'] = 'John Doe';
$xml->hello[1]->anotherTag['title'] = 'Hello';
echo $xml->asXml();

要保存文件使用 asXML($文件名)

To save the file use asXML($filename)

这篇关于PHP的SimpleXML,如何设置属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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