drevls8138 2015-10-07 15:47
浏览 36
已采纳

使用PHP domXML写入xml文件

I am trying to create a xml file and save it using domXML. However when it creates the xml file it does not create a parent node. This is how the xml file writes now.

<?xml version="1.0"?>
<feed>
 <headline></headline>
 <author></author>
 <link></link>    
 <image></image>
 <description></description>
</feed>

I need the xml file to look like this when it it is created and need to be able to insert more items into <feeds>

<?xml version="1.0"?>
<feeds>
 <feed>
  <headline></headline>
  <author></author>
  <link></link>    
  <image></image>
  <description></description>
 </feed>
</feeds>

Here is the script I am using. I'd appreciate it if anyone could help point me in the right direction.

$mainheadline = $_POST['headline'];
$mainauthor = $_POST['author'];
$mainlink = $_POST['link'];
$mainimage = $_POST['image'];
$maindescription = $_POST['description'];

$doc = new DOMDocument('1.0');
// we want a nice output
$doc->load("newsfeed/newsfeed.xml");

$root = $doc->createElement('feeds');
$root = $doc->appendChild($root);

$xml = $doc->createElement('feed');
$xml = $root->appendChild($xml);

$headline = $doc->createElement('headline', $mainheadline);
$headline = $xml->appendChild($headline);

$author = $doc->createElement('author', $mainauthor);
$author = $xml->appendChild($author);

$link = $doc->createElement('link', $mainlink);
$link = $xml->appendChild($link);

$image = $doc->createElement('image', $mainimage);
$image = $xml->appendChild($image);

$description = $doc->createElement('description', $maindescription);
$description = $xml->appendChild($description);

$doc->save("newsfeed/newsfeed.xml");
  • 写回答

1条回答 默认 最新

  • dongyangzhi0687 2015-10-07 16:50
    关注

    To append to an existing XML file, simply locate the root node and add children from there. The only challenge is to build the first file versus all the rest. Below uses a conditional if logic to decide depending if newsfeed.xml file exists or not.

    Below includes lines needed for very first xml and all xml files thereafter.

    $mainheadline = $_POST['headline'];
    $mainauthor = $_POST['author'];
    $mainlink = $_POST['link'];
    $mainimage = $_POST['image'];
    $maindescription = $_POST['description'];
    
    $doc = new DOMDocument('1.0');
    $doc->formatOutput = true;
    $doc->preserveWhiteSpace = false;
    
    // Condition first xml versus appended xml files
    if (!file_exists("newsfeed/newsfeed.xml")) {
        $root = $doc->appendChild($doc->createElement('feeds'));
    }
    else {
        $doc->load("newsfeed/newsfeed.xml");  
        $root = $doc->documentElement;  
    }
    
    $xml = $root->appendChild($doc->createElement('feed'));
    
    $xml->appendChild($doc->createElement('headline', $mainheadline));
    $xml->appendChild($doc->createElement('author', $mainauthor));
    $xml->appendChild($doc->createElement('link', $mainlink));
    $xml->appendChild($doc->createElement('image', $mainimage));
    $xml->appendChild($doc->createElement('description', $maindescription));
    
    // Save file
    $doc->save("newsfeed/newsfeed.xml");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi