PHP深化XML结构

发布于 2024-08-18 19:55:51 字数 1021 浏览 4 评论 0原文

给定以下 XML 结构(我将其放在 XML 文件中,其中包含许多其他内容 -

标签只是表明可能跟随其他标签):

<TITEL1>...</TITEL1>  
<p>..</p>
<TITEL2>...</TITEL2>  
<TITEL3>...</TITEL3>
<TITEL3>...</TITEL3>  
<P>...<P>  

有没有办法使用 PHP 来实现这一点(将其写入新文件):

<TITEL1>
    <TITEL>...</TITEL>  
    <p>...</p>
    <TITEL2>
        <TITEL>...</TITEL>  
        <TITEL3>
            <TITEL>...</TITEL>
            <P>...</P>
        </TITEL3>
        <TITEL3>
            <TITEL>...</TITEL>
            <P>...</P>
        </TITEL3>
    </TITEL2>
</TITEL1>

或者换句话说,是否有一种方法可以让较高级别的标题包含较低级别的标题及其后面的所有内容,从而创建嵌套结构。每个 TITEL1,2 和 3 标签的内容应该进入一个新的 元素

我已经在论坛的 XSLT 端问了同样的问题,但得到了尝试使用 c# 的建议或java。因为我不了解这些语言,并且了解的不仅仅是 PHP 的基础知识,所以我想尝试这种方式。谁能带我走上我的路吗?

given the following XML structure (i have this in an XML-file, with lots of other content - the <p> tags are just there to indicate that other tags may follow):

<TITEL1>...</TITEL1>  
<p>..</p>
<TITEL2>...</TITEL2>  
<TITEL3>...</TITEL3>
<TITEL3>...</TITEL3>  
<P>...<P>  

is there a way to get to this using PHP (write it to a new file):

<TITEL1>
    <TITEL>...</TITEL>  
    <p>...</p>
    <TITEL2>
        <TITEL>...</TITEL>  
        <TITEL3>
            <TITEL>...</TITEL>
            <P>...</P>
        </TITEL3>
        <TITEL3>
            <TITEL>...</TITEL>
            <P>...</P>
        </TITEL3>
    </TITEL2>
</TITEL1>

or in other words,is there a way to have higher level titels inclose lower level titels and all content that follows them, thus creating a nested structure. The content of each TITEL1,2 and 3 tag should go into a new <TITEL>-element

I already asked the same question on the XSLT-side of the forum but got the advise to try with c# or java. Since I don't know those languages and know somewhat more than the basics of PHP i thought of trying it that way. Can anyone set me on my way?

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

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

发布评论

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

评论(2

与君绝 2024-08-25 19:55:51

PHP 还具有非常好的内置 DOM 支持,您可以使用它来构建此类结构。开始记录此扩展的位置是 https://www.php.net/dom

在您的情况下,您首先必须创建一个文档,然后使用 DOMDocument::createElement DOMElement::appendChild 将该元素附加到另一个元素。

完成后,调用 DOMDocument::save 将 DOM 保存到指定文件中。

PHP also has a very well built in DOM support which you can use to build such structures. A place to start documenting about this extension would be https://www.php.net/dom.

In your case, you first have to create a document then use DOMDocument::createElement DOMElement::appendChild to append that element to another element

After you're done, call DOMDocument::save to save the DOM into a specified file.

七色彩虹 2024-08-25 19:55:51

这是我不久前为此编写的一些代码:

function array2xml($array) {
    static $index = 0;
    $pre = '';
    for ($i = 0; $i < $index;$i++) {
        $pre .= "\t";
    }
    $text = '';
    if (is_array($array)){
        foreach ($array as $k=>$v) {
            if (is_int($k)){
                if (is_array($v)){
                    $text = $text . array2xml($v);
                }
            } else if (is_string($k)){
                if (!is_array($v)){
                    $text .=  "$pre<$k>$v</$k>\n";
                } else if (is_array($v)) {
                    $index++;
                    $text .= "$pre<$k>\n".array2xml($v)."$pre</$k>\n";
                    $index--;
                }
            } else {
                if (is_array($v)){
                    $index++;
                    $text .= array2xml($v);
                    $index--;
                }
            }
        }
        return "$text";
    } else {
        return $text . $array;
    }
}
$a = array(
    "hello" => array(
        "world" => " Yay!",
    ),
    "messages" => array(
        array(
            array(
                "message" => "Goodby cruel world!"
            ),
            "message" => array(
                "text1" => "Yo",
                "text2" => "dude!",
            ),
        )
    ),
);
echo array2xml($a);

输出如下:

<hello >
    <world> Yay!</world>
</hello>
<messages >
    <message>Goodby cruel world!</message>
    <message >
        <text1>Yo</text1>
        <text2>dude!</text2>
    </message>
</messages>

不能 100% 确定为什么第一个标签缩进,但它从未真正困扰我。

Here some code I wrote awhile ago for just this:

function array2xml($array) {
    static $index = 0;
    $pre = '';
    for ($i = 0; $i < $index;$i++) {
        $pre .= "\t";
    }
    $text = '';
    if (is_array($array)){
        foreach ($array as $k=>$v) {
            if (is_int($k)){
                if (is_array($v)){
                    $text = $text . array2xml($v);
                }
            } else if (is_string($k)){
                if (!is_array($v)){
                    $text .=  "$pre<$k>$v</$k>\n";
                } else if (is_array($v)) {
                    $index++;
                    $text .= "$pre<$k>\n".array2xml($v)."$pre</$k>\n";
                    $index--;
                }
            } else {
                if (is_array($v)){
                    $index++;
                    $text .= array2xml($v);
                    $index--;
                }
            }
        }
        return "$text";
    } else {
        return $text . $array;
    }
}
$a = array(
    "hello" => array(
        "world" => " Yay!",
    ),
    "messages" => array(
        array(
            array(
                "message" => "Goodby cruel world!"
            ),
            "message" => array(
                "text1" => "Yo",
                "text2" => "dude!",
            ),
        )
    ),
);
echo array2xml($a);

Which outputs this:

<hello >
    <world> Yay!</world>
</hello>
<messages >
    <message>Goodby cruel world!</message>
    <message >
        <text1>Yo</text1>
        <text2>dude!</text2>
    </message>
</messages>

Not 100% sure why the first tag is indented, but it never really bothered me.

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