简体   繁体   中英

Writing to xml file with PHP domXML

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");

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM