简体   繁体   中英

How do I add content to an existing XML file?

I do have an xml generator written in PHP. sample is given below but few lines only due to space issues.

$output =  '<?xml version="1.0" encoding="UTF-8"?>'."\n";
    $output .= '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:syn="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">'."\n";
    $output .= '<channel rdf:about="'.$urlfr.'">'."\n";
    $output .= '<title>'.$title.'</title>'."\n";
    $output .= '<link>'.$urlorg.'</link>'."\n";
    $output .= '<description></description>'."\n";
    $output .= '<dc:language>'.$lang.'</dc:language>'."\n";
    $output .= '<dc:rights>'.$copyright.'</dc:rights>'."\n";

this is saved into a file called content-xml.xml. every day I do have a new content add to this file. what I want is how do I add new content to an existing XML file and show the latest content on top??

The data has to be coming from somewhere, right? How about automating the data retrieval process. Once you have the data you could easily use SimpleXML to add a child node to your root node. :)

Use DomDocument assuming it is available to you

    //Create an Instance of DomDocument and load existing XML
        $xmlDoc=new DomDocument();
        $xmlDoc->loadXML($xmlString); 
        $xmlDoc->saveXML();
   //Create an Instance of DomDocument with xml to be appended
         $xmlSnippet=new DomDocument();
         $xmlSnippet->loadXML($xmlSnippet);

   // get node to insertbefore let say item so first item in rss feed
   $item = $xmlSnippet->getElementsByTagName("item")->item(0);
   $item = $xmlDoc->importNode($item, true);
  //append to channel node 

   $item = $xmlDoc->documentGetElementByTagName('channel')->item(0)->appendChild($item)

   save doc 

   $xmlDoc->saveXML();

I am pretty sure there is libraries out there that ease the creation of RSS feeds, but if you want to do it with a proper XML extension, here is an example with DOM:

First we define the namespace. This is for laziness only.

$namespaces = array(
    'xmlns'            => 'http://purl.org/rss/1.0/',
    'xmlns:rdf'        => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
    'xmlns:slash'      => 'http://purl.org/rss/1.0/modules/slash/',
    'xmlns:taxo'       => 'http://purl.org/rss/1.0/modules/taxonomy/',
    'xmlns:dc'         => 'http://purl.org/dc/elements/1.1/',
    'xmlns:syn'        => 'http://purl.org/rss/1.0/modules/syndication/',
    'xmlns:admin'      => 'http://webns.net/mvcb/',
    'xmlns:feedburner' => 'http://rssnamespace.org/feedburner/ext/1.0'
);

Next you need to create and setup a new Document. We want nicely formatted UTF8 XML:

// prepare DOMDocument
$dom = new DOMDocument('1.0', 'utf-8');
$dom->formatOutput = TRUE;
$dom->preserveWhitespace = FALSE;

Next you need to create a root element and add all the namespaces to it. Because we have the namespaces in an array, we can simply iterate over the array and add them:

// create root node
$root = $dom->createElement('rdf:RDF');
foreach($namespaces as $ns => $uri) {
    $root->setAttributeNS('http://www.w3.org/2000/xmlns/', $ns, $uri);
}
$dom->appendChild($root);

The remainder is creating and adding nodes. This is always the same. Create Node, configure it, append it to the parent element. The code below is equivalent to your concatenated strings:

// create and append Channel
$channel = $dom->createElement('channel');
$channel->setAttribute('rdf:about', 'foo');
$root->appendChild($channel);

// create and append Title and Description
$channel->appendChild($dom->createElement('title', 'Example Feed'));
$channel->appendChild($dom->createElement('description'));

// special chars like & are only automatically encoded when added as DOMText
$link = $dom->createElement('link');
$link->appendChild($dom->createTextNode('http://example.com?foo=1&bar=2'));
$channel->appendChild($link);

// we added namespaces to root, so we can simply add ns'ed elements with
$channel->appendChild($dom->createElement('dc:language', 'en'));
$channel->appendChild($dom->createElement('dc:rights', 'public domain'));

And that's it. Now to output, you do:

// output cleanly formatted XML
echo $dom->saveXML();

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