在 php 中创建 xml 时删除 xml 版本标记

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5947695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:59:29  来源:igfitidea点击:

remove xml version tag when a xml is created in php

phpxml

提问by Dhiraj

I'm creating a xml using this

我正在使用这个创建一个 xml

$customXML = new SimpleXMLElement('<abc></abc>');

after adding some attributes onto this, when I try to print it it appears like this,

在上面添加一些属性后,当我尝试打印它时,它看起来像这样,

<?xml version="1.0"?>
<abc id="332"><params><param name="aa">33</param></params></abc>

Is there a way to remove the xml version node ? Thank you

有没有办法删除xml版本节点?谢谢

回答by Gordon

In theory you can provide the LIBXML_NOXMLDECLoptionto drop the XML declaration when saving a document, but this is only available in Libxml >= 2.6.21 (and buggy). An alternative would be to use

理论上,您可以提供在保存文档时删除 XML 声明的LIBXML_NOXMLDECL选项,但这仅在 Libxml >= 2.6.21(和buggy)中可用。另一种方法是使用

$customXML = new SimpleXMLElement('<abc></abc>');
$dom = dom_import_simplexml($customXML);
echo $dom->ownerDocument->saveXML($dom->ownerDocument->documentElement);

回答by Jon

A practical solution: you know that the first occurrence of ?>in the result string is going to be then end of the xml version substring. So:

一个实用的解决方案:您知道?>结果字符串中的第一次出现将是 xml 版本子字符串的结尾。所以:

$customXML = new SimpleXMLElement('<abc></abc>');
$customXML = substr($customXML, strpos($customXML, '?'.'>') + 2);

Note that ?>is split into two parts because otherwise some poor syntax highlighter may have problems parsing at this point.

请注意,?>它分为两部分,否则一些糟糕的语法荧光笔此时可能会出现解析问题。

回答by hrvoj3e

I have a simmilar solution to the accepted answer:

我对接受的答案有一个类似的解决方案:

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

回答by hakre

As SimpleXMLElementalways uses "\n"to separate the XML-Declaration from the rest of the document, it can be split at that position and the remainder taken:

由于SimpleXMLElement始终使用"\n"的XML-宣言从文档的其余部分分开,它可以在该位置拆分,而其余采取:

explode("\n", $customXML->asXML(), 2)[1];

Example:

例子:

<?php

$customXML = new SimpleXMLElement('<!-- some comment -->
<abc>
</abc>');

echo explode("\n", $customXML->asXML(), 2)[1];

Output:

输出:

<!-- some comment -->
<abc>
</abc>

回答by Soliman

Try:

尝试:

$xmlString = $doc->saveXML();
$xmlString = str_replace("<?xml version=\"1.0\"?>\n", '', $xmlString);
file_put_contents($filename, $xmlString);

回答by Hitman_99

There's another way without the replacing xml header. I prefer this:

还有另一种没有替换 xml 标头的方法。我更喜欢这个:

$xml = new xmlWriter();
$xml->openMemory();
$xml->startElement('abc');
$xml->writeAttribute('id', 332);
$xml->startElement('params');
$xml->startElement('param');
$xml->writeAttribute('name', 'aa');
$xml->text('33');
$xml->endElement();
$xml->endElement();
echo $xml->outputMemory(true);

Gives output:

给出输出:

<abc id="332"><params><param name="aa">33</param></params></abc>

回答by CubFuzzy

echo preg_replace("/<\?xml.*\?>/",'',$doc->saveXML(),1);

回答by Wahid Masud

$customXML = new SimpleXMLElement('<source><abc>hello</abc></source>');
$result = $customXML->xpath('//abc');
$result = $result[0];
var_dump($result->asXML());

回答by pintxo

If this is a problem, this should do it:

如果这是一个问题,应该这样做:

$xml = str_replace(' version="1.0"', '', $xml);`

回答by Jahmic

XML header tags and PHP short tags are incompatible. So, this may be an issue of using the short tags in PHP (i.e. <?=instead of <?php echo...). So, turning off short tags in PHP.INI (and updating your code appropriately) may resolve this issue.

XML 标头标签和 PHP 短标签不兼容。因此,这可能是在 PHP 中使用短标签(即<?=代替<?php echo...)的问题。因此,关闭 PHP.INI 中的短标签(并适当更新您的代码)可能会解决此问题。