Php 如何创建没有XML打开标记的XML文件
我正在使用PHP创建一个XML文件,这对我来说是一个相当大的挑战,因为我不是PHP方面的专家,但是输出结果并不理想。问题:如何创建所需的输出 澄清:我需要它,因为此文件将被合并到另一个动态创建的XML文件中,其中XML打开标记将导致以下错误:Php 如何创建没有XML打开标记的XML文件,php,xml,simplexml,Php,Xml,Simplexml,我正在使用PHP创建一个XML文件,这对我来说是一个相当大的挑战,因为我不是PHP方面的专家,但是输出结果并不理想。问题:如何创建所需的输出 澄清:我需要它,因为此文件将被合并到另一个动态创建的XML文件中,其中XML打开标记将导致以下错误:仅允许在文档开头使用XML声明 输出: <?xml version="1.0"?> <items> <page description="template4" type="4" duration="5000" on="sat
仅允许在文档开头使用XML声明
输出:
<?xml version="1.0"?>
<items>
<page description="template4" type="4" duration="5000" on="saturday"/>
<page description="template4" type="4" duration="5000" on="sunday"/>
<page description="template4" type="4" duration="5000" on="sunday"/>
<page description="template4" type="4" duration="5000" on="sunday"/>
<page description="template4" type="4" duration="5000" on="today"/>
</items>
$xmldata = new SimpleXMLElement('<items />');
for ($i=0; $i < count($_POST['data']); $i++) {
$xmlpage = $xmldata->addChild('page');
$xmlpage->addAttribute('description', 'template4');
$xmlpage->addAttribute('type', '4');
$xmlpage->addAttribute('duration', '5000');
$xmlpage->addAttribute('on', $_POST['data']['bday' . $i . '']['when']);
}
$xmldata->asXML('xml/playlistbdays.xml');
PHP:
<?xml version="1.0"?>
<items>
<page description="template4" type="4" duration="5000" on="saturday"/>
<page description="template4" type="4" duration="5000" on="sunday"/>
<page description="template4" type="4" duration="5000" on="sunday"/>
<page description="template4" type="4" duration="5000" on="sunday"/>
<page description="template4" type="4" duration="5000" on="today"/>
</items>
$xmldata = new SimpleXMLElement('<items />');
for ($i=0; $i < count($_POST['data']); $i++) {
$xmlpage = $xmldata->addChild('page');
$xmlpage->addAttribute('description', 'template4');
$xmlpage->addAttribute('type', '4');
$xmlpage->addAttribute('duration', '5000');
$xmlpage->addAttribute('on', $_POST['data']['bday' . $i . '']['when']);
}
$xmldata->asXML('xml/playlistbdays.xml');
$xmldata=新的SimpleXMLElement(“”);
对于($i=0;$iaddChild('page');
$xmlpage->addAttribute('description','template4');
$xmlpage->addAttribute('type','4');
$xmlpage->addAttribute('duration','5000');
$xmlpage->addAttribute('on',$_POST['data']['bday.$i.']['when']);
}
$xmldata->asXML('xml/playlibdays.xml');
我试过:
<?xml version="1.0"?>
<items>
<page description="template4" type="4" duration="5000" on="saturday"/>
<page description="template4" type="4" duration="5000" on="sunday"/>
<page description="template4" type="4" duration="5000" on="sunday"/>
<page description="template4" type="4" duration="5000" on="sunday"/>
<page description="template4" type="4" duration="5000" on="today"/>
</items>
$xmldata = new SimpleXMLElement('<items />');
for ($i=0; $i < count($_POST['data']); $i++) {
$xmlpage = $xmldata->addChild('page');
$xmlpage->addAttribute('description', 'template4');
$xmlpage->addAttribute('type', '4');
$xmlpage->addAttribute('duration', '5000');
$xmlpage->addAttribute('on', $_POST['data']['bday' . $i . '']['when']);
}
$xmldata->asXML('xml/playlistbdays.xml');
$xmldata=新的SimpleXMLElement(“”)
出现错误,提示:字符串无法解析为XML
$str = $xmldata->asXML(); // get as string instead of saving file
$str = str_replace(['<?xml version="1.0"?>','<items>','</items>'],'',$str); // remove tags you don't want.
file_put_contents('xml/playlistbdays.xml', trim($str)) ; // save file
$str=$xmldata->asXML();//以字符串形式获取,而不是保存文件
$str=str_替换(['','','','','','','$str);//删除不需要的标记。
文件内容('xml/playlibdays.xml',trim($str));//保存文件
由于OP的总体目标是合并XML文档(这个文件将被合并成另一个动态创建的XML文件),考虑PHP的合并XML文件,而不需要剥离XML声明头。以下示例将项目导入订单xml:
$input\u str='1〕
41412
';
$input=新的DOMDocument('1.0','UTF-8');
$input->loadXML($input\u str);
$items\u str=
';
$items=新的DOMDocument('1.0','UTF-8');
$items->loadXML($items\u str);
$new_node=$input->importNode($items->documentElement,TRUE);
$input->documentElement->appendChild($new_节点);
echo$input->saveXML();
#
#
# 41412
#
#
#
#
#
#
#
#
您最好将其构建为一个字符串—如果您不想要一个合适的XML文件,为什么还要使用SimpleXMLElement呢?这是一个很好的例子。为什么需要这样的输出?这是您对X问题提出的Y解决方案,您没有解释。@Barskey。。。对于未来的读者和最佳实践标准,我必须恳求您不要按照下面接受的答案将XML文档视为字符串。请解释为什么需要无根XML。@Parfait很好,添加了一些说明。哈哈!正是我所想的。经典XY问题。您绝对不需要删除标题。通过使用load
解析文档并使用PHP或XSLT,您可以轻松地合并XML。下一次,请解释完整的问题,以获得可靠的答案,可能是因为您的需要而遭到否决的原因。