我在尝试更改XML值,然后将其保存为XML。 当我更改没有命名空间的元素时工作正常。问题是,当我想要改变的值在一个名字空间中时,我可以找到并打印出来,但任何更改都会被忽略;像这样:如何使用simplexml更改名称空间元素的值?
$ns = $xmlsingle->children('mynamespace');
foreach ($ns as $myelement)
{
echo "my element is: [$myelement]";
//I can change it:
$myelement = "something else";
echo "my element is now: [$myelement]"; //yay, value is changed!
}
//GREAT!
//But when I save the XML back, the value is not changed... apparently the children method creates a new object; not a link to the existing object
//So if I copy/paste the code above, I have the original value, not the changed value
$ns2 = $xmlsingle->children('mynamespace');
foreach ($ns2 as $myelement)
{
echo "my element is UNCHANGED! [$myelement]";
}
//So my change's not done when I save the XML.
$xmlsingle->asXML(); //This XML is exacly the same as the original XML, without changes to the namespaced elements.
**请忽略可能无法编译任何愚蠢的错误,我重新输入从我的原代码的文本,否则会过大;代码起作用,只要NAMESPACED元素的值在我将其放回到XML时保持不变。
我不是PHP专家,我不知道如何以任何其他方式访问命名空间元素......我如何更改这些值?我到处搜索,但只找到如何读取值的说明。
显示您的XML,请 – michi 2013-03-23 00:05:45