0

我有一个多级节点的 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 节点?

在此先感谢您的帮助。

4

1 回答 1

-1

这是一个使用PHP SimpleXMLElement 类和一点 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);
于 2013-04-24T23:52:53.457 回答