我有一个xml的多级节点。 我可以找到节点,但需要知道如何更新节点值。 在我的文件中,我可以通过其属性找到节点通道,但是如何更新nodeType或其他节点的值?
这是我的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
的子节点的值其中After Update只有属性为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节点?
提前感谢您的帮助。
答案 0 :(得分:-1)
这是使用PHP SimpleXMLElement class和一点XPath的解决方案:
<?php
$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>
XML;
// Create the SXE object
// You can read from file using the simplexml_load_file function
$sxe = new SimpleXMLElement($xml);
// Fetch the right channel using XPath
$channele4 = $sxe->xpath('//channeles[contains(@ch, 4)]');
// Update the values you want
$channele4[0]->channelName = 'Channel New Name';
$channele4[0]->channelType = 'New Channel Type';
// Retrieve the parent (..) node
$systemConfig = $sxe->xpath('//channeles[contains(@ch, 4)]/..');
// Update the moduleName value
$systemConfig[0]->moduleName = 'New module name';
// Store the updated values in the $xml variable
$xml = $sxe->asXML();
// Print the updated XML
echo $xml;
注意:如果您从远程源加载XML,那么SXE对象的实例化应该像这样完成:
$url = "http://domain.com/abc.xml";
$sxe = new SimpleXMLElement($url, NULL, TRUE);