简体   繁体   English

如何将内容添加到现有XML文件?

[英]How do I add content to an existing XML file?

I do have an xml generator written in PHP. 我确实有一个用PHP编写的xml生成器。 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. 它将保存到名为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?? 我想要的是如何将新内容添加到现有XML文件并在顶部显示最新内容?

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. 获得数据后,您可以轻松地使用SimpleXML将子节点添加到您的根节点。 :) :)

Use DomDocument assuming it is available to you 假定您可以使用DomDocument

    //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: 我很确定那里有可以简化RSS提要创建的库,但是如果您想使用适当的XML扩展名来做,以下是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: 我们想要格式正确的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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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