gpt4 book ai didi

php - 在 SimpleXML 中美化/格式化输出

转载 作者:可可西里 更新时间:2023-11-01 00:06:25 24 4
gpt4 key购买 nike

我有这个 simplexml 脚本,我用它来发布从表单输入的数据。

$xml = simplexml_load_file("links.xml");

$sxe = new SimpleXMLElement($xml->asXML());

$person = $sxe->addChild("link");
$person->addChild("title", "Q: ".$x_title);
$person->addChild("url", "questions_layout.php#Question$id");

$sxe->asXML("links.xml");

当它出来时,它在一行中看起来像这样:

<link><title>Alabama State</title><url>questions_layout.php#Question37</url></link><link><title>Michigan State</title><url>questions_layout.php#Question37</url></link></pages>

但我已经尝试了找到的方法 HERETHIS AS WELL但都没有在行中正确格式化 XML,因为它们应该是这样的

<link>
<title></title>
<url></url>
</link>

在第一个引用链接中,我什至将 loadXML 更改为 load,因为 loadXML 需要一个字符串作为 XML。有人可以帮我找到解决方案吗?

最佳答案

AFAIK simpleXML 无法自行完成。

但是,DOMDocument 可以。

$dom = dom_import_simplexml($sxe)->ownerDocument;
$dom->formatOutput = TRUE;
$formatted = $dom->saveXML();

关于php - 在 SimpleXML 中美化/格式化输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10133157/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com