使用 PHP 更新 XML 文件 [英] Updating a XML file using PHP
本文介绍了使用 PHP 更新 XML 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用 PHP 更新 XML 标记中的单个属性而不重写和保存文件的最简单方法是什么?有没有办法只使用常规的 DOM 东西来做到这一点?
What is the easiest way to update a single attribute in a XML tag using PHP without rewriting and saving the file? Any way to do this just using regular DOM stuff?
推荐答案
如果您的服务器上有 PHP5,您可以尝试:
If you have PHP5 on your server you could try:
$string = "<?xml version='1.0'?>
<doc>
<title>XML Document</title>
<date timezone=\"GMT+1\">2008-01-01 13:42:53</date>
<message>Daylight savings starting soon!</message>
</doc>";
$xml = simplexml_load_string($string);
// Show current timezone
echo $xml->date['timezone'].'<br>';
// Set a new timezone
$xml->date['timezone'] = 'GMT+10';
echo $xml->date['timezone'];
注意:注意空格——XML 需要格式正确,SimpleXML 才能正确解析.
Note: Watch the whitespace -- the XML needs to be well-formed for SimpleXML to parse correctly.
替代方案包括 simplexml_load_file()
和 simplexml_import_dom()
.
Alternatives include simplexml_load_file()
and simplexml_import_dom()
.
这篇关于使用 PHP 更新 XML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文