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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀