草庐IT

php - XML "tree"到多级 html 列表

coder 2024-07-02 原文

我有一个 XML 文件,我想将它显示为一个多级列表

<items>
  <item>
    <parent_id>1</parent_id>  
    <id>1876</id>
    <name>foobar1</name>
  </item>
  <item>
    <parent_id>1876</parent_id>
    <id>1877</id>
    <name>foobar11</name>
  </item>
  <item>
    <parent_id>1877</parent_id>
    <id>1878</id>
    <name>foobar111</name>
  </item>
  <item>
    <parent_id>1877</parent_id>
    <id>1879</id>
    <name>foobar112</name>
  </item>
  <item>
    <parent_id>1877</parent_id>
    <id>1880</id>
    <name>foobar113</name>
  </item>
...
</items>

我想显示如下:

<ul>
    <li>foobar1</li>
    <li>
      <ul>
        <li>foobar11</li>
        <li>
          <ul>
            <li>foobar111</li>
            <li>foobar112</li>
            <li>foobar113</li>
          </ul>
        </li>
      </ul>
    </li>
...
</ul>

我尝试循环执行(我使用的是 simplexml):

    $catxml = simplexml_load_file('file.xml');  
    $nodes=$catxml->xpath("//item");    
    $gile=0;
    $ile=0;

    foreach($nodes as $node) {
        $par = $node->parent_id;
        $id = $node->id;
        $naz = $node->name;

        if($par=='1'){ $gile++;  $ile++; echo '<li><a href="#url">'.$id.'-'.$naz.'</a></li>';
        settype($id,'integer');         
            foreach($nodes as $nodea) {
                $apar = $nodea->parent_id;
                $aid = $nodea->id;
                $anaz = $nodea->name;
                settype($aid,'integer');
                $apoczatek='';
                if($apar!='1' AND $apar==$id AND $aid>$id){ $ile++; if($apoczatek!=''){echo '</ul></li>';}echo '<li><ul><li><a href="#url">'.$aid.'-'.$anaz.'</a></li>';
                    foreach($nodes as $nodea) {
                        $bpar = $nodea->parent_id;
                        $bid = $nodea->id;
                        $bnaz = $nodea->name;
                        $bpoczatek='';
                        settype($bid,'integer');
                        if($bpar==$aid AND $bid>$aid){ $ile++; if($bpoczatek!=''){echo '</ul>';}echo '<ul><li><a href="#url">'.$bid.'-'.$bnaz.'</a></li>';
                            foreach($nodes as $nodea) {
                                $cpar = $nodea->parent_id;
                                $cid = $nodea->id;
                                $cnaz = $nodea->name;
                                $cpoczatek='';
                                settype($cid,'integer');
                                if($cpar==$bid AND $cid>$bid){ $ile++; if($cpoczatek!=''){echo '</ul>';}echo '<ul><li><a href="#url">'.$cid.'-'.$cnaz.'</a></li>';
                                    foreach($nodes as $nodea) {
                                        $dpar = $nodea->parent_id;
                                        $did = $nodea->id;
                                        $dnaz = $nodea->name;
                                        $dpoczatek='';
                                        settype($did,'integer');
                                        if($dpar==$cid AND $did>$cid){ $ile++; if($dpoczatek!=''){echo '</ul>';}echo '<ul><li><a href="#url">'.$did.'-'.$dnaz.'</a></li>';
                                            foreach($nodes as $nodea) {
                                                $epar = $nodea->parent_id;
                                                $eid = $nodea->id;
                                                $enaz = $nodea->name;
                                                $epoczatek='';
                                                settype($eid,'integer');
                                                if($epar==$did AND $eid>$did){ $ile++; if($epoczatek!=''){echo '</ul>';}echo '<ul><li><a href="#url">'.$eid.'-'.$enaz.'</a></li>';

                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

但它没有用 - 错过了 <ul></ul>标签

对我的方法有什么建议吗?还有其他方法吗?

最佳答案

我对递归数据使用了递归函数。 我也注释掉了 // echo "</li>\n<li>"; : 它是您预期的 HTML 结构所必需的,但我不喜欢它。

$catxml = simplexml_load_file('file.xml');  
$nodes=$catxml->xpath("//item");

$children = array();

foreach($nodes as $node) {
    $parent_id = (int) $node->parent_id;
    if(! array_key_exists ($parent_id, $children)) {
        $children[$parent_id] = array();
    }
    $children[$parent_id][] = $node;
}

function renderChildrenList( &$children, $id) {
    echo "\n<ul>";
    foreach($children[$id] as $node) {
        echo "\n<li>" . $node->id . '-' . $node->name;

        $child_id = (int) $node->id;
        if(array_key_exists($child_id, $children)) {
            // echo "</li>\n<li>";
            renderChildrenList($children, $child_id);
        }

        echo "</li>";
    }
    echo "\n</ul>";
}

// I assume root_id=1
renderChildrenList( $children, 1, 0);

关于php - XML "tree"到多级 html 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13093975/

有关php - XML "tree"到多级 html 列表的更多相关文章

  1. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  2. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  3. ruby - 使用 ruby​​ 将 HTML 转换为纯文本并维护结构/格式 - 2

    我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h

  4. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  5. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  6. ruby - RVM 使用列表[0] - 2

    是否有类似“RVMuse1”或“RVMuselist[0]”之类的内容而不是键入整个版本号。在任何时候,我们都会看到一个可能包含5个或更多ruby的列表,我们可以轻松地键入一个数字而不是X.X.X。这也有助于rvmgemset。 最佳答案 这在RVM2.0中是可能的=>https://docs.google.com/document/d/1xW9GeEpLOWPcddDg_hOPvK4oeLxJmU3Q5FiCNT7nTAc/edit?usp=sharing-知道链接的任何人都可以发表评论

  7. ruby-on-rails - Rails HTML 请求渲染 JSON - 2

    在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这

  8. ruby-on-rails - 使用 Sublime Text 3 突出显示 HTML 背景语法中的 ERB? - 2

    所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择

  9. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

    我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

  10. ruby-on-rails - 相关表上的范围为 "WHERE ... LIKE" - 2

    我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que

随机推荐