没有<?xml version ="1.0"的PHP DomDocument输出; encoding ="UTF-8"? [英] PHP DomDocument output without <?xml version="1.0" encoding="UTF-8"?>

查看:116
本文介绍了没有<?xml version ="1.0"的PHP DomDocument输出; encoding ="UTF-8"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DomDocument是否有删除第一行的选项:

is there an option with DomDocument to remove the first line:

<?xml version="1.0" encoding="UTF-8"?>

类实例化会自动将其添加到输出中,但是有可能摆脱它吗?

The class instantiation automatically adds it to the output, but is it possible to get rid of it?

推荐答案

如果要输出HTML,请使用saveHTML()函数.它会自动避免使用大量的XML习惯用法,并正确处理关闭/未关闭的HTML习惯用法.

If you want to output HTML, use the saveHTML() function. It automatically avoids a whole lot of XML idiom and handles closed/unclosed HTML idiom properly.

如果要输出XML,则可以使用DOMDocumentDOMNode(即XPath表达式中的'/')这一事实,因此可以在其上使用DOMNode API调用来遍历子节点.并在每个子节点上调用saveXML().这不会输出XML声明,并且会正确输出所有其他XML内容.

If you want to output XML you can use the fact that DOMDocument is a DOMNode (namely: '/' in XPath expression), thus you can use DOMNode API calls on it to iterate over child nodes and call saveXML() on each child node. This does not output the XML declaration, and it outputs all other XML content properly.

示例:

$xml = get_my_document_object();
foreach ($xml->childNodes as $node) {
   echo $xml->saveXML($node);
}

这篇关于没有&lt;?xml version ="1.0"的PHP DomDocument输出; encoding ="UTF-8"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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