如何在没有 XML 开始标签的情况下创建 XML 文件

How to create XML file without XML opening tags

我正在使用 PHP 创建一个 XML 文件,这对我来说非常具有挑战性,因为我不是 PHP 方面的专家,但是输出不尽如人意。问题:如何创建我想要的输出?

说明: 我需要这个,因为这个文件将被合并到另一个动态创建的 XML 文件中,其中 XML 开始标记会导致以下错误: XML declaration allowed only at the start of the document.

输出:

<?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>

期望的输出:(我知道这是无效的 XML 但根据定义它仍然被称为 .xml 文件)。

<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"/>

PHP:

$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 = new SimpleXMLElement(''); 给出错误提示:String could not be parsed as 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
    

    由于 OP 的总体目标是合并 XML 文档(此文件将合并到另一个动态创建的 XML 文件),请考虑 PHP 的 importNode 合并 XML 文件而不需要删除 xml 声明 header。下面的示例将 items 导入 orders xml:

    $input_str = '<?xml version="1.0"?>
                  <orders>
                    <order_id>41412</order_id>
                  </orders>';
    
    $input = new DOMDocument('1.0', 'UTF-8'); 
    $input->loadXML($input_str);
    
    $items_str = '<?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>';
    
    $items = new DOMDocument('1.0', 'UTF-8'); 
    $items->loadXML($items_str);
    
    $new_node = $input->importNode($items->documentElement, TRUE);    
    $input->documentElement->appendChild($new_node);
    
    echo $input->saveXML();
    #    <?xml version="1.0"?>
    #    <orders>
    #       <order_id>41412</order_id>
    #       <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>
    #   </orders>