在PHP中,如何从DOMNode获取外部XML? [英] In PHP, how can I get the Outer XML from a DOMNode?

查看:80
本文介绍了在PHP中,如何从DOMNode获取外部XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您在PHP中具有 DOMNode ,如何获取外部xml(即该元素内的所有XML加上元素本身)?

If you have DOMNode in PHP, how can you get the outer xml (i.e. the all of the XML that is inside this element plus the element itself)?

例如,可以说这是结构

<car>
 <tire>Michelin</tire>
 <seats>Leather</seats>
 <type>
   <color>red</color>
   <make>Audi</make>
 </type>
</car>

我有一个指向< type>的指针.节点...我想回来

And I have a pointer to the <type> node... I want to get back

<type>
    <color>red</color>
    <make>Audi</make>
</type>

如果我只想输入文字,我会得到"redAudi".

If I just ask for the text, I get back "redAudi".

推荐答案

您需要DOMDocument:

You need a DOMDocument:

// If you don't have a document already:
$doc = new DOMDocument('1.0', 'UTF-8');

echo $doc->saveXML($node); // where $node is your DOMNode

在文档中检查DOMDocument :: saveXML以获取更多信息.

Check DOMDocument::saveXML in the docs for more info.

当将DOMNode传递到saveXML()时,您只会获取该节点的内容,而不是整个文档.

When you pass a DOMNode to saveXML() you get back only the contents of that node not the whole document.

这篇关于在PHP中,如何从DOMNode获取外部XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆