dongshimao7115 2019-02-17 16:09
浏览 32
已采纳

复杂循环通过复杂的SimpleXMLElement

I need to save some values from XML.

First step - I get the structure:

 $xml = $dom_xml->saveXML();
 $xml_ = new \SimpleXMLElement($xml);
 dd($xml_);

Here TextFrame has 8 arrays. Each of them has PathPointType, which has 4 more arrays with 3 attributes each. And these attributes I need from each TextFrame.

enter image description here

I can get, for instance, Anchor value doing this:

$res = $xml_
        ->Spread
        ->TextFrame
        ->Properties
        ->PathGeometry
        ->GeometryPathType
        ->PathPointArray
        ->PathPointType
        ->attributes();
    dd($res['Anchor']);

(BTW: is there more prettier way to get it?)

But the question is - how is it possible to loop through all arrays and save values separately for each array?

I assume here has to be a multidimensional foreach loop in conjunction with for loop?

Or is better to achieve it using DOMDocument?

  • 写回答

2条回答 默认 最新

  • duanqin9631 2019-02-17 16:55
    关注

    As it looks as though you are starting off with DOMDocument (as you are using $dom_xml->saveXML() to generate the XML), it may be easier to continue using it and it also has some easy features for getting the details your after.

    Using getElementsByTagName() allows you to get a list of the elements with a specific tag name from a start point, so starting with $dom_xml, get all of the <TextFrame> elements. Then foreach() over this list and using this element as a start point, use getElementsByTagName("PathPointType") to get the nested <PathPointType> elements. At this point you can then use getAttribute("Anchor") for each of the attributes you need from the <PathPointType> elements...

    $textFrames = $dom_xml->getElementsByTagName("TextFrame");
    
    foreach ( $textFrames as $frame )   {
        $pathPointTypes = $frame->getElementsByTagName("PathPointType");
        foreach ( $pathPointTypes as $type )    {
            echo $type->getAttribute("Anchor").PHP_EOL;
        }
    }
    

    Edit

    You can extend the code to build an array of frames and then the anchors within that. This code also stores the anchor in an associative array so that if you add the other attributes, you can add them here (or remove it if you don't need another layer of detail)...

    $frames =[];
    foreach ( $textFrames as $frame )   {
        $anchors = [];
        $pathPointTypes = $frame->getElementsByTagName("PathPointType");
        foreach ( $pathPointTypes as $type )    {
            $anchors[] = ['Anchor' => $type->getAttribute("Anchor")];
        }
        $frames[] = $anchors;
    }
    

    Also if you have some way of identifying the frames, you could create an associative array at that level as well...

    $frames[$frameID] = $anchors;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么