is there an option with DomDocument to remove the first line:
是否有一个选项与DomDocument删除第一行:
<?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?
类实例化自动将它添加到输出中,但是可以摆脱它吗?
3 个解决方案
#1
10
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.
如果要输出HTML,请使用saveHTML()函数。它自动避免了大量的XML习惯用法并正确处理封闭/未封闭的HTML习惯用法。
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,可以使用DOMDocument是DOMNode(即XPath表达式中的'/')这一事实,因此您可以在其上使用DOMNode API调用迭代子节点并在每个子节点上调用saveXML() 。这不会输出XML声明,它会正确输出所有其他XML内容。
Example:
例:
$xml = get_my_document_object();
foreach ($xml->childNodes as $node) {
echo $xml->saveXML($node);
}
#2
19
I think using DOMDocument is a universal solution for valid XML files:
我认为使用DOMDocument是有效XML文件的通用解决方案:
If you have xml allready loaded in a variable:
如果你已经在变量中加载了xml:
$t_xml = new DOMDocument();
$t_xml->loadXML($xml_as_string);
$xml_out = $t_xml->saveXML($t_xml->documentElement);
For XML file from disk:
对于磁盘中的XML文件:
$t_xml = new DOMDocument();
$t_xml->load($file_path_to_xml);
$xml_out = $t_xml->saveXML($t_xml->documentElement);
This comment helped: http://www.php.net/manual/en/domdocument.savexml.php#88525
这条评论帮助:http://www.php.net/manual/en/domdocument.savexml.php#88525
#3
0
You can use output buffering to remove it. A bit of a hack but it works.
您可以使用输出缓冲将其删除。有点黑客,但它的工作原理。
ob_start();
// dom stuff
$output = ob_get_contents();
ob_end_clean();
$clean = preg_replace("/(.+?\n)/","",$output);
#1
10
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.
如果要输出HTML,请使用saveHTML()函数。它自动避免了大量的XML习惯用法并正确处理封闭/未封闭的HTML习惯用法。
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,可以使用DOMDocument是DOMNode(即XPath表达式中的'/')这一事实,因此您可以在其上使用DOMNode API调用迭代子节点并在每个子节点上调用saveXML() 。这不会输出XML声明,它会正确输出所有其他XML内容。
Example:
例:
$xml = get_my_document_object();
foreach ($xml->childNodes as $node) {
echo $xml->saveXML($node);
}
#2
19
I think using DOMDocument is a universal solution for valid XML files:
我认为使用DOMDocument是有效XML文件的通用解决方案:
If you have xml allready loaded in a variable:
如果你已经在变量中加载了xml:
$t_xml = new DOMDocument();
$t_xml->loadXML($xml_as_string);
$xml_out = $t_xml->saveXML($t_xml->documentElement);
For XML file from disk:
对于磁盘中的XML文件:
$t_xml = new DOMDocument();
$t_xml->load($file_path_to_xml);
$xml_out = $t_xml->saveXML($t_xml->documentElement);
This comment helped: http://www.php.net/manual/en/domdocument.savexml.php#88525
这条评论帮助:http://www.php.net/manual/en/domdocument.savexml.php#88525
#3
0
You can use output buffering to remove it. A bit of a hack but it works.
您可以使用输出缓冲将其删除。有点黑客,但它的工作原理。
ob_start();
// dom stuff
$output = ob_get_contents();
ob_end_clean();
$clean = preg_replace("/(.+?\n)/","",$output);