-1

我需要以下 xml 搜索的帮助。我无法弄清楚如何从按属性搜索的 xml 文件中搜索所有子节点。详情如下。

这是我的 XML 文件

<?xml version="1.0"?>
<systemConfigs>
    <systemConfig cnfId="1">
        <moduleName>Module 1</moduleName>
        <channeles ch="1">
            <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>
        </channeles>
        <channeles ch="2">
            <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>
        </channeles>
    </systemConfig>
    <systemConfig cnfId="2">
        <moduleName>Module 2</moduleName>
    <channeles ch="3">
        <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>
        </channeles>
        <channeles ch="4">
            <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>
    </channeles>
    </systemConfig>
    <systemConfig cnfId="3">
        <moduleName>NI DAQmx Task</moduleName>
        <channeles ch="5">
            <channelName>my Voltage</channelName>
            <channelFunc>conFig</channelFunc>
            <channelType>thermo</channelType>
            <eu>EF</eu>
            <custScale>custPol</custScale>
            <rawMin>1</rawMin>
            <rawMax>10</rawMax>
            </channeles>
    </systemConfig>
</systemConfigs>

示例:我需要通过属性 ch=4 从上述 xml 中搜索所有数据我该怎么做?

<channeles ch="4">
    <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>
</channeles>

我真的被困在这里,请任何人帮我举个例子。提前致谢。

4

1 回答 1

-1

你去:

$xml = '<?xml version="1.0"?>
<systemConfigs>
    <systemConfig cnfId="1">
        <moduleName>Module 1</moduleName>
            <channeles ch="1">
                <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>
            </channeles>
            <channeles ch="2">
                <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>
            </channeles>
        </systemConfig>
    <systemConfig cnfId="2">
        <moduleName>Module 2</moduleName>
    <channeles ch="3">
        <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>
        </channeles>
        <channeles ch="4">
            <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>
    </channeles>
    </systemConfig>
    <systemConfig cnfId="3">
        <moduleName>NI DAQmx Task</moduleName>
        <channeles ch="5">
            <channelName>my Voltage</channelName>
            <channelFunc>conFig</channelFunc>
            <channelType>thermo</channelType>
            <eu>EF</eu>
            <custScale>custPol</custScale>
            <rawMin>1</rawMin>
            <rawMax>10</rawMax>
            </channeles>
    </systemConfig>
</systemConfigs>';

$doc = new DOMDocument();

 $doc->loadXML($xml);

$selector = new DOMXPath($doc);
$list = $selector->query('//channeles[@ch="4"]');
$node = $list->item(0);

echo($doc->saveXML($node));
于 2013-04-18T23:13:00.833 回答