dongzhao3040 2013-05-23 18:13
浏览 26
已采纳

PHP XML DOM - 基于父属性/元素区分节点

First of all, thanks for the time reading this :)

I need help reordering an XML feed. I've tried many things and researched, but can't come up with a solution.

<xml>
<group>
   <result>
      <title>Title</title>
      <url>URL</url>
      <text>Text</text>
   </result>
   <result>
      <title>Title</title>
      <url>URL</url>
      <text>Text</text>
   </result>
</group>
<group region=top>
   <result>
      <title>Title</title>
      <url>URL</url>
      <text>Text</text>
   </result>
</group>
<group type=bottom>
   <result>
      <title>Title</title>
      <url>URL</url>
      <text>Text</text>
      <moreinfo>
             <result>
             <title>Title</title>
             </result>
             <result>
             <title>Title</title>
             </result>
      </moreinfo>
   </result>
</group>
</xml>

What I am trying to do is re-order the XML feed to display each node inside 'result'. However, I need the feed reordered so the 'result' from 'group region=top' is at the top, then the results from 'group', then results from 'group region=bottom'.

You might notice there is another 'result' tag nested inside of a 'result' tag in 'group region=bottom', which is causing most of the issues. The way I envision a solution to this is with the following pseudo-code:

$books = $doc->getElementsByTagName( "result" );

    foreach( $books as $book )
    {           
    if (parent_attribute = top){    

        $toptitle = $book->getElementsByTagName( "title" );
        $toptitle = $toptitle->item(0)->nodeValue;

            $topnew[]  =array("title"=>$toptitle);  
       }

            if (parent_attribute = null){

            $middletitle = $book->getElementsByTagName( "title" );
        $middletitle = $middletitle->item(0)->nodeValue;

            $middlenew[]  =array("title"=>$middletitle);            

        }

            if (parent_attribute = bottom){

            $bottomtitle = $book->getElementsByTagName( "title" );
        $bottomtitle = $bottomtitle->item(0)->nodeValue;

                if (parent_element = moreinfo){

                $moretitle = $book->getElementsByTagName( "title" );
                $moretitle = $moretitle->item(0)->nodeValue;
                }
            $bottomnew[]  =array("title"=>$bottomtitle, "more"=>$moretitle);
        } 

}
  • 写回答

1条回答 默认 最新

  • dsf55s1233 2013-05-27 22:11
    关注

    The XML you posted is invalid and not consistent, as attribute values have to be enclosed in "", for consistency see my comment above.

    Here's how I do it using simplexml and xpath, it can be easily adapted to DOM as well.

    $xml = simplexml_load_string($x); // assuming XML in $x
    
    $regions['top'] = $xml->xpath("group[@region='top']/result");
    $regions['middle'] = $xml->xpath("group[not(@*)]/result"); // <group> with no attribute
    $regions['bottom'] = $xml->xpath("group[@region='bottom']/result");
    
    // output:
    foreach ($regions as $region => $results) {
    
        echo "$region:<br />";
        foreach ($results as $result) {
        echo $result->title . "<br />";
        if (count($result->moreinfo) > 0)
            foreach ($result->moreinfo->result as $subresult) 
                        echo "____$subresult->title<br />";
        } // foreach $results
    } // foreach $regions
    

    see it working: http://codepad.viper-7.com/5adWxC

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘