数组到 XML:如何将 HTML 作为对象导入到 DOMDocument?

发布于 2024-12-15 03:19:27 字数 2225 浏览 0 评论 0原文

可能的重复:
如何将 HTML 插入 PHP DOMNode?
PHP 和 DOMDocument

我正在尝试编写一个使用 DOMDocument 将数组转换为 XML 的类 - 并且在顶部其中将 HTML 导入到 DOM 文档中。问题是 HTML 没有导入到 DOM 文档中 - 它作为文本字符串导入(例如,HTML 标记显示为 &lt;p&gt; 而不是 < ;p> 在生成的 XML 文档的源中)。

更新: 按照 Hakre 的要求,代码直接添加到这个问题中。该代码有点被黑客攻击,但可以工作——尽管按照 Hakre 的建议摆脱 DomDocument 的扩展会很有趣。

class xmlizer extends DomDocument {

    function __construct() {
        parent::__construct();
    }

    function node_create($arr, $items = null) {
        if (is_null($items))
            $items = $this->appendChild($this->createElement("items"));

        // Loop the array values.
        foreach($arr as $element => $value) {
            // If Array has numeric keys, use "node - else use $element.
            $element = is_numeric( $element ) ? "node" : $element;
            // Create element, add $value unless $value is an array - and append to main object ($items).
            $fragment = $this->createElement($element, (is_array($value) ? null : $value));
            $items->appendChild($fragment);

            // Iterate if $value is an array, .
            if (is_array($value)) {
                self::node_create($value, $fragment);
            }
        }
    }

    public function __toString() {
        // html_entity_decode() added by Micha. Thanks.
        return html_entity_decode($this->saveXML());
    }

}

// Build test Array with HTML string (for testing purposes only).
for($i=0;$i<3;$i++) {
    $j = $i+1;
    $array['example'][] = array(
        "id" => $j,
        "title" => "Title $j",
        "description" => "<p>Text <strong>string</strong> nr. $j with <em>some</em> <code>HTML code</code>.</p>",
        );
}


// Test: Run the code.
header("Content-Type:text/xml");
$xml = new xmlizer();
$xml->node_create($array);
echo $xml;

PS:请不要关闭问题,因为我认为这不是重复的。谢谢。

Possible Duplicate:
How to insert HTML to PHP DOMNode?
PHP and DOMDocument

I am trying to write a Class that converts an array to XML using DOMDocument - and on top of that imports HTML into the DOM document. Problem is that the HTML is not imported into the DOM document - it gets imported as a text string (for instance, HTML tags are shown as <p> instead of <p> in the source for the resulting XML document).

Update:
Code added directly to this Question as requested by Hakre. The code is a bit hacked but works - it would be interesting though to get rid of the extend from DomDocument as suggested by Hakre.

class xmlizer extends DomDocument {

    function __construct() {
        parent::__construct();
    }

    function node_create($arr, $items = null) {
        if (is_null($items))
            $items = $this->appendChild($this->createElement("items"));

        // Loop the array values.
        foreach($arr as $element => $value) {
            // If Array has numeric keys, use "node - else use $element.
            $element = is_numeric( $element ) ? "node" : $element;
            // Create element, add $value unless $value is an array - and append to main object ($items).
            $fragment = $this->createElement($element, (is_array($value) ? null : $value));
            $items->appendChild($fragment);

            // Iterate if $value is an array, .
            if (is_array($value)) {
                self::node_create($value, $fragment);
            }
        }
    }

    public function __toString() {
        // html_entity_decode() added by Micha. Thanks.
        return html_entity_decode($this->saveXML());
    }

}

// Build test Array with HTML string (for testing purposes only).
for($i=0;$i<3;$i++) {
    $j = $i+1;
    $array['example'][] = array(
        "id" => $j,
        "title" => "Title $j",
        "description" => "<p>Text <strong>string</strong> nr. $j with <em>some</em> <code>HTML code</code>.</p>",
        );
}


// Test: Run the code.
header("Content-Type:text/xml");
$xml = new xmlizer();
$xml->node_create($array);
echo $xml;

PS: Please don't close the Question as I don't think this is a duplicate. Thanks.

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

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

发布评论

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

评论(1

咋地 2024-12-22 03:19:27

在第二个代码的第 15 行尝试 html_entity_decode($value) 但为什么您希望 HTML 为 HTML,因为那样它就会被解释为 XML。

更新
抱歉,上面的方法不起作用,这个也不起作用:

$this
    ->createElement($element)
    ->createTextNode(is_array($value) ? null : $value ));

最后我自己尝试了:

我认为这是最好的解决方案:http://codepad.org/PpyewkVd

Try html_entity_decode($value) on line 15 in the second code but why you want the HTML as HTML because then it would be interpreted as XML.

Update
Sorry the one above doesn't work and this doesn't work too:

$this
    ->createElement($element)
    ->createTextNode(is_array($value) ? null : $value ));

Finaly I tryed it my self:

I think this is the best solution: http://codepad.org/PpyewkVd

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