我有一个函数返回一个xml但返回一个带有两个
的xml <?xml version="1.0"?>
标签
我无法通过一次通话来修复通话。我试图使用SimpleXMLElement所以当我将这个xml加载到simplexml时,由于重复的标签,它会出错。任何人都知道如何在加载到xml之前删除标记 p>
我尝试过做
$new_xml = preg_replace('/<?xml(.*)?>(.*)?<\/?>/', '', $xml);
我也已经完成了(顺便说一下。但是不确定它是否是最好的方式,因为我不确定是否有时会在
$xml = str_replace('<?xml version="1.0"?>', '', $xml);
答案 0 :(得分:0)
这将删除截止的第一行&gt;和尾随新行字符。
<?php
$xml = "<?xml version=\"1.0\"?>\n\t<tag></tag>";
$xml = preg_replace('!^[^>]+>(\r\n|\n)!','',$xml);
echo $xml;
?>
它也适用于不符合1.0版本的其他XML文件。
答案 1 :(得分:0)
@AbsoluteƵERØ 的回答对我有帮助。
就我而言,我自己创建了 XML。
$doc = new DOMDocument();
$doc->formatOutput = true;
$root = $doc->createElement('TAG1');
$doc->appendChild($root);
// ... some loop that inserts more children of TAG1 with attributes
$xml_file = 'relative/path/to/file.xml';
$doc->save($xml_file); // Here the XML file is saved along with the line that poses problems
// Read created XML
$t_xml = file_get_contents($xml_file);
// Remove first line
$t_xml = preg_replace('!^[^>]+>(\r\n|\n)!', '', $t_xml);
// Save into same file as original
file_put_contents($xml_file, $t_xml);