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 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色
  • ¥15 求一份华为esight平台V300R009C00SPC200这个型号的api接口文档
  • ¥15 字符串比较代码的漏洞
  • ¥15 欧拉系统opt目录空间使用100%
  • ¥15 ul做导航栏格式不对怎么改?