<legend id='pk7rF'><style id='pk7rF'><dir id='pk7rF'><q id='pk7rF'></q></dir></style></legend>
    <tfoot id='pk7rF'></tfoot>
  1. <small id='pk7rF'></small><noframes id='pk7rF'>

  2. <i id='pk7rF'><tr id='pk7rF'><dt id='pk7rF'><q id='pk7rF'><span id='pk7rF'><b id='pk7rF'><form id='pk7rF'><ins id='pk7rF'></ins><ul id='pk7rF'></ul><sub id='pk7rF'></sub></form><legend id='pk7rF'></legend><bdo id='pk7rF'><pre id='pk7rF'><center id='pk7rF'></center></pre></bdo></b><th id='pk7rF'></th></span></q></dt></tr></i><div id='pk7rF'><tfoot id='pk7rF'></tfoot><dl id='pk7rF'><fieldset id='pk7rF'></fieldset></dl></div>
      <bdo id='pk7rF'></bdo><ul id='pk7rF'></ul>
    1. 使用 XMLReader 选择父节点

      时间:2023-11-30
        <bdo id='rrTX5'></bdo><ul id='rrTX5'></ul>
        <legend id='rrTX5'><style id='rrTX5'><dir id='rrTX5'><q id='rrTX5'></q></dir></style></legend>
            <tbody id='rrTX5'></tbody>
          <tfoot id='rrTX5'></tfoot>

                <small id='rrTX5'></small><noframes id='rrTX5'>

              1. <i id='rrTX5'><tr id='rrTX5'><dt id='rrTX5'><q id='rrTX5'><span id='rrTX5'><b id='rrTX5'><form id='rrTX5'><ins id='rrTX5'></ins><ul id='rrTX5'></ul><sub id='rrTX5'></sub></form><legend id='rrTX5'></legend><bdo id='rrTX5'><pre id='rrTX5'><center id='rrTX5'></center></pre></bdo></b><th id='rrTX5'></th></span></q></dt></tr></i><div id='rrTX5'><tfoot id='rrTX5'></tfoot><dl id='rrTX5'><fieldset id='rrTX5'></fieldset></dl></div>
                本文介绍了使用 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 节点:

                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>,例如?

                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 选择父节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  <bdo id='8wG5F'></bdo><ul id='8wG5F'></ul>
                  <legend id='8wG5F'><style id='8wG5F'><dir id='8wG5F'><q id='8wG5F'></q></dir></style></legend>
                  <i id='8wG5F'><tr id='8wG5F'><dt id='8wG5F'><q id='8wG5F'><span id='8wG5F'><b id='8wG5F'><form id='8wG5F'><ins id='8wG5F'></ins><ul id='8wG5F'></ul><sub id='8wG5F'></sub></form><legend id='8wG5F'></legend><bdo id='8wG5F'><pre id='8wG5F'><center id='8wG5F'></center></pre></bdo></b><th id='8wG5F'></th></span></q></dt></tr></i><div id='8wG5F'><tfoot id='8wG5F'></tfoot><dl id='8wG5F'><fieldset id='8wG5F'></fieldset></dl></div>

                      <tbody id='8wG5F'></tbody>

                    <small id='8wG5F'></small><noframes id='8wG5F'>

                      <tfoot id='8wG5F'></tfoot>