选择父节点的所有子节点,其中xml文件中的attribute = value

时间:2013-04-18 22:50:49

标签: php xml

我需要bellow 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>

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

1 个答案:

答案 0 :(得分:-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));