使用XMLReader选择父节点 [英] Selecting parent nodes with XMLReader

查看:100
本文介绍了使用XMLReader选择父节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须重写程序的一部分才能使用XMLReader选择要处理的XML文件的一部分.

I had to rewrite part of a programme to use XMLReader to select parts of an XML file for processing.

以这个简化的XML为例:

Take this simplified XML as an example:

<odds>
    <sport>
        <region>
            <group>
                <event name="English Championship 2014-15" eventid="781016.1">
                    <bet name="Kazanan" betid="12377108.1">
                        <selection selectionid="52411062.1"/>
                        </selection>
                    </bet>
                </event>
            </group>
        </region>
    </sport>
</odds> 

此呼叫xpath():

$bets = $xml->xpath(
    "//odds/sport/region/group/event/bet/selection[contains(@selectionid,'".$selectionToFind."')]/.."
    );

将选择整个<bet>节点及其子节点(<selection>节点).

would select the whole <bet> node and its children (<selection> nodes).

但是,我的代码将只选择一个具有给定selectionid<selection>节点:

My code, however, would select only one <selection> node with a given selectionid:

$reader = new XMLReader;
$reader->open('file.xml');

while($reader->read()) {
    $event = $reader->getAttribute($value); 

    if ($event == 781016.1 ) {
        $node = new SimpleXMLElement($reader->readOuterXML());
        var_dump($node);
        break;
    }
}

如何使用XMLReader复制xpath()的行为,以便我选择<bet>节点及其子节点,而不是仅选择一个<selection>子节点?

How can replicate the behaviour of xpath() with XMLReader so that I select the <bet> node and its children and not only one <selection> child?

我想这个问题可以归结为:我可以通过子元素的属性值选择整个父节点<bet>吗,例如<selection selectionid="[some_value]">?

I guess the question boils down to: Can I select the whole parent node <bet> by the attribute value of a child, e.g. <selection selectionid="[some_value]">?

推荐答案

[忽略SimpleXML解决方案,请看一看XMLReader]

[Ignore the SimpleXML solution and look down at the XMLReader one]

我建议使用SimpleXMLElement :: xpath方法.

I would suggest using the SimpleXMLElement::xpath method.

http://php.net/manual/en/simplexmlelement.xpath.php

$xml = new SimpleXMLElement($xml_string);

/* Search for <a><b><c> */
$result = $xml->xpath("/odds/sport/region/group/event/bet");

$ result将包含所有'bet'音符的子代.

$result will contain all children of 'bet' note.

//XMLReader解决方案**********************

// XMLReader solution **********************

$reader = new XMLReader;
$reader->open('file.xml');
$parent_element = null;

while($reader->read()) {
    $selectionid = $reader->getAttribute('selectionid'); 

    if ($selectionid == '52411062.1' ) {
        // use the parent of the node with attribute 'selectionid' = '52411062.1'
        $node = $parent_element;
        var_dump($node);
        break;
    }
    elseif ($reader->name === 'bet') { )
    {
        // store parent element
        $parent_element = new SimpleXMLElement($reader->readOuterXML());
    }
}

这篇关于使用XMLReader选择父节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆