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条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料