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");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?