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

发布于 2024-10-07 06:07:14 字数 3317 浏览 3 评论 0原文

我有一个 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);

    ?>

I have an XML file which contains family tree data in a nested structure, and I'm wanting to parse it into a nested list.

I have the following code

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

Which loads in the following XML file and prints it as-is

<?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>

However I want to parse it into the following format:

<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>

I'm pretty new to php, so if this is an incredibly simple problem I apologise! Would really appreciate any ideas.

EDIT

I'm now using the following recursive loop but still having problems

$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);

    ?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅浅 2024-10-14 06:07:14

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

RECURSION!

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”的所有信息,循环遍历 的每个孩子。,并以此为基础进行调用。这有道理吗?

Here's your code. You'll need to add the rest of the attributes (dday, spouse)

RECURSION!

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);

You see, it outputs all information about an "indi", loops through each child of <family>, and calls itself on that. Does that make sense?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文