我有一个多级节点的 xml。我可以找到节点,但需要知道如何更新节点值。在下面的文件中,我可以通过其属性找到节点通道,但是如何更新通道类型或其他节点的值?
这是我的 XML 文件,其中包含我所做的代码。
<?xml version="1.0"?>
<systemConfigs>
<systemConfig cnfId="1">
<moduleName>Module 1</moduleName>
<channeles ch="1">
<chId>1</chId>
<channelName>Channel 1 of Module 1</channelName>
<channelType>myFunc 1</channelType>
<channelFunc>conFig</channelFunc>
<eu>myFunc 1</eu>
<custScale>myFunc 1</custScale>
<rawMin>myFunc 1</rawMin>
<rawMax>myFunc 1</rawMax>
<euMin>myFunc 1</euMin>
<euMax>myFunc 1</euMax>
<dspFormat>myFunc 1</dspFormat>
<digOfPrec>myFunc 1</digOfPrec>
</channeles>
<channeles ch="2">
<chId>2</chId>
<channelName>Channel 2 of Module 1</channelName>
<channelType>myFunc 2</channelType>
<channelFunc>conFig</channelFunc>
<eu>myFunc 2</eu>
<custScale>myFunc 2</custScale>
<rawMin>myFunc 2</rawMin>
<rawMax>myFunc 2</rawMax>
<euMin>myFunc 2</euMin>
<euMax>myFunc 2</euMax>
<dspFormat>myFunc 2</dspFormat>
<digOfPrec>myFunc 2</digOfPrec>
</channeles>
</systemConfig>
<systemConfig cnfId="2">
<moduleName>Module 2</moduleName>
<channeles ch="3">
<chId>3</chId>
<channelName>Channel 1 of Module 2</channelName>
<channelType>myFunc 1</channelType>
<channelFunc>conFig</channelFunc>
<eu>myFunc 1</eu>
<custScale>myFunc 1</custScale>
<rawMin>myFunc 1</rawMin>
<rawMax>myFunc 1</rawMax>
<euMin>myFunc 1</euMin>
<euMax>myFunc 1</euMax>
<dspFormat>myFunc 1</dspFormat>
<digOfPrec>myFunc 1</digOfPrec>
</channeles>
<channeles ch="4">
<chId>4</chId>
<channelName>Channel 2 of Module 2</channelName>
<channelType>myFunc 2</channelType>
<channelFunc>conFig</channelFunc>
<eu>myFunc 2</eu>
<custScale>myFunc 2</custScale>
<rawMin>myFunc 2</rawMin>
<rawMax>myFunc 2</rawMax>
<euMin>myFunc 2</euMin>
<euMax>myFunc 2</euMax>
<dspFormat>myFunc 2</dspFormat>
<digOfPrec>myFunc 2</digOfPrec>
</channeles>
</systemConfig>
</systemConfigs>
现在我想更新通道 ch=4 的子节点的值
在更新后,只有属性 ch=4 的节点通道将被保存为这样的新值。
<channeles ch="4">
<chId>4</chId>
<channelName>Channel New Name</channelName>
<channelType>New Channel Type</channelType>
<channelFunc>New Func</channelFunc>
<eu>New Eu</eu>
<custScale>New Cust Scale</custScale>
<rawMin>1</rawMin>
<rawMax>10</rawMax>
<euMin>1</euMin>
<euMax>10</euMax>
<dspFormat>scintific</dspFormat>
<digOfPrec>10</digOfPrec>
</channeles>
我可以这样找到节点。
$doc = new DOMDocument();
$doc->load(BASEPATH.'data/sysConf.xml');
$selector = new DOMXPath($doc);
$query = '//channeles[@ch="4"]/*';
$list = $selector->query($query);
$node = $list->item(0);
$module = $node->parentNode->getElementsByTagName( "channelName" );
$channelName = $module->item(0)->nodeValue;
$module = $node->parentNode->getElementsByTagName( "channelType" );
$channelType = $module->item(0)->nodeValue;
请帮助我提供代码示例。如何用所有子节点更新我的 XML 节点?
在此先感谢您的帮助。