使用PHP将XML解析为嵌套列表

时间:2010-12-10 22:38:26

标签: php xml list recursion html-lists

我有一个XML文件,其中包含嵌套结构中的族谱数据,我想将其解析为嵌套列表。

我有以下代码

<?php 
    $doc = new DOMDocument();
    $doc->load('armstrong.xml');
    echo $doc->saveXML();
?>

在以下XML文件中加载并按原样打印

<?xml version="1.0" encoding="UTF-8"?>
<indi>
    <id>id1</id>
    <fn>Matt</fn>
    <bday>1919</bday>
    <dday>2000</dday>
    <spouse>Evelyn Ross</spouse>
    <family>
        <indi>
        <id>id2</id>
        <fn>Jane</fn>
        <bday></bday>
        <dday></dday>
        <spouse></spouse>
        <family>

        </family>
    </indi>
    <indi>
        <id>id3</id>
        <fn>Jason</fn>
        <bday></bday>
        <dday></dday>
        <spouse></spouse>
        <family>

        </family>
    </indi>
    <indi>
        <id>id4</id>
        <fn>Samuel</fn>
        <bday></bday>
        <dday></dday>
        <spouse></spouse>
        <family>
            <indi>
                <id>id5</id>
                <fn>John</fn>
                <bday></bday>
                <dday></dday>
                <spouse></spouse>
                <family>

                </family>
            </indi>
            <indi>
                <id>id6</id>
                <fn>John</fn>
                <bday></bday>
                <dday></dday>
                <spouse></spouse>
                <family>

                </family>
            </indi>
        </family>
    </indi>
</family>

但我想将其解析为以下格式:

<ul>
   <li> 
    <span class="vcard person" id="id1">
            <span class="edit fn">Matt</span> 
        <span class="edit bday">1956</span> 
        <span class="edit dday"></span>
            <span class="edit spouse">Eunace Fulton</span>
        </span> 
    <ul> ... List of Family ... </ul>
   </li>
</ul>

我对php很新,所以如果这是一个非常简单的问题,我道歉!真的很感激任何想法。

修改

我现在使用以下递归循环但仍有问题

$doc = new DOMDocument();
    $doc->load('armstrong.xml');

    function outputIndi($indi) {
        $i = new DOMDocument();
        $i = $indi;

        echo '<li>';

        echo '<span class="edit fn">' . $indi->getElementsByTagName("fn") . '</span>'; // name not a real attribute, must access through DOM
        echo '<span class="edit bday">' . $indi->getElementsByTagName("bday") . '</span>'; // ditto
        // ...

        echo '<ul>';
        foreach ($indi->getElementsByTagName("family") as $subIndi) { // again, family not a real attribute
            outputIndi($subIndi);
        }
        echo '</ul>';

        echo '</li>';
    }

    outputIndi($doc->documentRoot);

    ?>

1 个答案:

答案 0 :(得分:1)

这是你的代码。您需要添加其余属性(dday,配偶)

<强>递推!

function outputIndi($indi) {
    echo '<li>';
    $id = $indi->getElementsByTagName('id')->item(0)->nodeValue;
    echo '<span class="vcard person" id="' . $id . '">';

    $fn = $indi->getElementsByTagName('fn')->item(0)->nodeValue;
    $bday = $indi->getElementsByTagName('bday')->item(0)->nodeValue;

    echo '<span class="edit fn">' . $fn . '</span>';
    echo '<span class="edit bday">' . $bday . '</span>';
    // ...

    echo '<ul>';
    $family = $indi->getElementsByTagName('family')->item(0)->childNodes;
    foreach ($family as $subIndi) {
        outputIndi($subIndi);
    }
    echo '</ul>';
    echo '</span>';
    echo '</li>';
}

$doc = new DOMDocument();
$doc->load('armstrong.xml');

outputIndi($doc->documentElement);

你看,它输出了关于“indi”的所有信息,循环遍历<family>的每个孩子,然后调用它。这有意义吗?