dqf2015 2018-06-18 21:05
浏览 149
已采纳

XPath解析成数组

$xml = simplexml_load_file('Request.xml');
$xml->registerXPathNamespace("ess", "localhost/ESS");
$partnums = array();

$xp = "descendant::ess:ProPartList/ess:ProPart/ess:SelectedPart";
$selected = $xml->xpath($xp);        
$x = 0;
foreach($selected as $part) {
    $partnuminfo = $part->xpath("//ess:PartNumInfo");
    foreach($partnuminfo as $p) {
        echo $p->asXML();
        $_Type = (string)$part->children("ess",true)->PartNumType;
        $partnums[$x] = array($_Type => (string)$part->children("ess",true)->PartNum);
        $x++;
    }
}
print_r($partnums);

Using the code above, I cannot seem to get my head around parsing the XML below:

<ess:ProInfo>
    <ess:ProPartList>
        <ess:ProPart>
            <ess:SelectedPart>
                <ess:PartNumInfo>
                    <ess:PartNumType>OE</ess:PartNumType>
                    <ess:PartNum>04715SNAA90ZZ</ess:PartNum>
                </ess:PartNumInfo>
                <ess:PartNumInfo>
                    <ess:PartNumType>IC</ess:PartNumType>
                    <ess:PartNum>536-01037</ess:PartNum>
                </ess:PartNumInfo>
                <ess:PartNumInfo>
                    <ess:PartNumType>PType</ess:PartNumType>
                    <ess:PartNum>536</ess:PartNum>
                </ess:PartNumInfo>
            </ess:SelectedPart>
        </ess:ProPart>
        <ess:ProPart>
            <ess:SelectedPart>
                <ess:PartNumInfo>
                    <ess:PartNumType>OE</ess:PartNumType>
                    <ess:PartNum>71570SNAA00</ess:PartNum>
                </ess:PartNumInfo>
                <ess:PartNumInfo>
                    <ess:PartNumType>IC</ess:PartNumType>
                    <ess:PartNum>536-01036</ess:PartNum>
                </ess:PartNumInfo>
            </ess:SelectedPart>
        </ess:ProPart>
        <ess:ProPart>
            <ess:SelectedPart>
                <ess:PartNumInfo>
                    <ess:PartNumType>OE</ess:PartNumType>
                    <ess:PartNum>66100SNEA00ZZ</ess:PartNum>
                </ess:PartNumInfo>
                <ess:PartNumInfo>
                    <ess:PartNumType>IC</ess:PartNumType>
                    <ess:PartNum>117-50338</ess:PartNum>
                </ess:PartNumInfo>                                
            </ess:SelectedPart>
        </ess:ProPart>
        <ess:ProPart>
            <ess:SelectedPart>
                <ess:PartNumInfo>
                    <ess:PartNumType>OE</ess:PartNumType>
                    <ess:PartNum>04655SNE305ZZ</ess:PartNum>
                </ess:PartNumInfo>
            </ess:SelectedPart>
        </ess:ProPart>
    </ess:ProPartList>
</ess:ProInfo>

And creating this array()

array(
    0 => array("OE" => "04715SNAA90ZZ", "IC" => "536-01037", "PType" => "536"),
    1 => array("OE" => "71570SNAA00",   "IC" => "536-01036"),
    2 => array("OE" => "66100SNEA00ZZ", "IC" => "117-50338"),
    3 => array("OE" => "04655SNE305ZZ")
)
  • 写回答

1条回答 默认 最新

  • douwuying4709 2018-06-18 21:43
    关注

    I think the main problem is using //ess:PartNumInfo in the second XPath expression, this can cause other elements to be found as well (// means any element). If you change that to use the descendant:: axis as in the first XPath expression, it will only look for elements inside the start point.

    I've changed the code to also group the elements at the next level, so...

    $xp = "descendant::ess:ProPartList/ess:ProPart/ess:SelectedPart";
    $selected = $xml->xpath($xp);
    foreach($selected as $part) {
        $partnuminfo = $part->xpath("descendant::ess:PartNumInfo");
        $group = array();
        foreach($partnuminfo as $p) {
            $_Type = (string)$p->children("ess",true)->PartNumType;
            $group[$_Type] =(string)$p->children("ess",true)->PartNum;
        }
        $partnums[] = $group;
    }
    print_r($partnums);
    

    gives...

    Array
    (
        [0] => Array
            (
                [OE] => 04715SNAA90ZZ
                [IC] => 536-01037
                [PType] => 536
            )
    
        [1] => Array
            (
                [OE] => 71570SNAA00
                [IC] => 536-01036
            )
    
        [2] => Array
            (
                [OE] => 66100SNEA00ZZ
                [IC] => 117-50338
            )
    
        [3] => Array
            (
                [OE] => 04655SNE305ZZ
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。