在网页中显示分层xml数据

时间:2013-10-21 03:07:11

标签: javascript php python xml web

<Pathways species="homo sapiens">
<Pathway dbId="109581" displayName="Apoptosis">
 <Pathway dbId="109607" displayName="Extrinsic Pathway for Apoptosis">
  <Pathway dbId="73887" displayName="Death Receptor  Signalling">
    <Pathway dbId="75157" displayName="FasL/ CD95L signaling">
      <Reaction dbId="75244" displayName="FASL binds FAS Receptor" />
      <Reaction dbId="71050" displayName="Trimerization of the FASL:FAS receptor complex" />
      <Reaction dbId="83650" displayName="FasL:Fas binds FADD" />
      <Reaction dbId="83586" displayName="FASL:FAS Receptor Trimer:FADD complex binds pro-Caspase-8" />
      <Reaction dbId="141310" displayName="FASL:FAS Receptor Trimer:FADD complex binds pro-Caspase-10" />
    </Pathway>
  </Pathway>
</Pathway>
  </Pathway>
<Pathway dbId="109581" displayName="Signaling pathway">
</Pathway>
</Pathways>

任何知道如何在网站上显示它们的人: 如下所示:

-Apoptosis
--Extrinsic Pathway for Apoptosis
---Death Receptor  Signalling
----FasL/ CD95L signaling
-----FASL...
-----Trimerizaiton of the FASL....
.  .
.  .
.  .
-Signaling pathway

我不知道树的深度,但不是太多。 感谢。

2 个答案:

答案 0 :(得分:0)

查找PHP的函数xml_parse_into_struct,您可以使用它将这样的文件拆分为包含适当键的数组。手册中包含了您需要的所有信息和示例。

或者,here's an article explaining how to use SimpleXML。也很容易使用。

答案 1 :(得分:0)

您可以使用支持标准PHP RecursiveIterator树遍历的PHP's SimpleXMLIterator

以下示例输出此文本树,例如,它与您在问题中列出的输出类型非常接近:

|-Pathway: Apoptosis
| \-Pathway: Extrinsic Pathway for Apoptosis
|   \-Pathway: Death Receptor  Signalling
|     \-Pathway: FasL/ CD95L signaling
|       |-Reaction: FASL binds FAS Receptor
|       |-Reaction: Trimerization of the FASL:FAS receptor complex
|       |-Reaction: FasL:Fas binds FADD
|       |-Reaction: FASL:FAS Receptor Trimer:FADD complex binds pro-Caspase-8
|       \-Reaction: FASL:FAS Receptor Trimer:FADD complex binds pro-Caspase-10
\-Pathway: Signaling pathway

以下是代码的摘录:

<?php
/**
 * Iterator Garden Example
 *
 * Display hierarchical xml data in web page
 *
 * @link http://stackoverflow.com/q/19485654/367456
 */

require __DIR__ . '/iterator_garden.php';

$file = __DIR__ . '/data.xml';
$xml  = file_get_contents($file);

$it  = new SimpleXMLIterator($xml);

$decor = new RecursiveDecoratingIterator($it, function($item) {
    return $item['displayName'] ?: $item['species'];
}, RecursiveDecoratingIterator::DECORATE_NODES);


$tree = new RecursiveTreeIterator($decor, RecursiveTreeIterator::BYPASS_CURRENT);

foreach($tree as $key => $item)
{
    echo $key, ': ', $item, "\n";
}

来自Iterator Garden is available on Github的代码,此处使用了RecursiveDecoratingIterator