【问题标题】:xpath select and save specific indexxpath 选择并保存特定索引
【发布时间】:2012-07-31 04:36:20
【问题描述】:

我的 php (simplexml) 中有一个 xml 解析器,我想在 xpath 下选择并保存一个索引元素

function startXML(&$resarrary, $path, $lang) {

if(file_exists($path)) {
    $xml = simplexml_load_file($path);
    if($xml) {      
        foreach($xml->xpath("//languageKey") as $cos) {
        utf8_encode($cos);

        // Select required languagekey 

        if ($cos[index] == $lang) {
        foreach($xml->xpath("//languageKey[@index='$lang']//label[@type='js']") as $cos) {
            ?>
                <table>
                    <tr>
                        <td><?php echo $cos ?></td>
                    </tr>
                </table>    
            <?php 
            array_push($resarrary,$cos);

            }

        } 

      }

    } 
    else {
        FALSE;
    }
}

}

表 echo $cos 仅用于测试....它为我提供了从我的 xml 文件中搜索的正确结果(在这种情况下,结果是“模式 1”)

所以选择有效,但我怎样才能将结果(“模式 1”)保存到数组中?如果我尝试这个 array_push(...) 并使用 print_r 查看 resarray 我只能得到 simplexml xpath 搜索的全部内容

Array ([0] => SimpleXMLElement Object ([@attributes] => Array ([index] => list_mode_1 [type] => js) [0] => Mode 1))

如果我改变线 array_push($resarrary,$cos->attributes());我明白了

数组([0] => SimpleXMLElement 对象([@attributes] => 数组([index] => list_mode_1 [type] => js)))

【问题讨论】:

    标签: php xml xpath


    【解决方案1】:

    XML

    <lKey index="default" type="array">
        <a index="list_mode_1" type="js">Mode 1</a>
        <b index="list_mode_2">Mode 2</b>
        <c index="list_mode_3">Mode 3</c>
    </lKey>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-02
      • 1970-01-01
      • 2010-10-27
      • 1970-01-01
      • 2019-01-21
      • 1970-01-01
      • 2014-06-24
      相关资源
      最近更新 更多