1

簡単な質問:デフォルトのRSS構造を別のXML形式に変換する必要があります。

RSSファイルは次のようなものです。

<?xml version="1.0" encoding="UTF-8"?>
    <rss version="2.0">
        <channel>
            <title>Name des RSS Feed</title>
            <description>Feed Beschreibung</description>
            <language>de</language>
            <link>http://xml-rss.de</link>
            <lastBuildDate>Sat, 1 Jan 2000 00:00:00 GMT</lastBuildDate>
            <item>
                <title>Titel der Nachricht</title>
                <description>Die Nachricht an sich</description>
                <link>http://xml-rss.de/link-zur-nachricht.htm</link>
                <pubDate>Sat, 1. Jan 2000 00:00:00 GMT</pubDate>
                <guid>01012000-000000</guid>
            </item>
            <item>
                <title>Titel der Nachricht</title>
                <description>Die Nachricht an sich</description>
                <link>http://xml-rss.de/link-zur-nachricht.htm</link>
                <pubDate>Sat, 1. Jan 2000 00:00:00 GMT</pubDate>
                <guid>01012000-000000</guid>
            </item>
            <item>
                <title>Titel der Nachricht</title>
                <description>Die Nachricht an sich</description>
                <link>http://xml-rss.de/link-zur-nachricht.htm</link>
                <pubDate>Sat, 1. Jan 2000 00:00:00 GMT</pubDate>
                <guid>01012000-000000</guid>
            </item>
        </channel>
    </rss>

...そして、次のようなitem-elements(子と属性を含む)XMLのみを抽出したいと思います。

<?xml version="1.0" encoding="ISO-8859-1"?>
<item>
    <title>Titel der Nachricht</title>
    <description>Die Nachricht an sich</description>
   <link>http://xml-rss.de/link-zur-nachricht.htm</link>
   <pubDate>Sat, 1. Jan 2000 00:00:00 GMT</pubDate>
   <guid>01012000-000000</guid>
</item>
...

ファイルに保存する必要はありません。出力だけが必要です。

編集:さらに知っておく必要があります:RSSファイルには動的な数のアイテムが含まれている可能性があります。これは単なるサンプルです。したがって、while、for、for-each、..でループする必要があります。

DOMNode、SimpleXML、XPathなどでさまざまなアプローチを試しましたが、成功しませんでした。

ありがとうクリス

4

3 に答える 3

1

あなたが求めるのは、ほとんど変革ではありません。基本的には、<item>要素をそのまま抽出するだけです。また、ルートノードがないため、結果は有効なXMLではありません。

それとは別に、次のように簡単に行うことができます。

$dom = new DOMDocument;           // init new DOMDocument
$dom->loadXML($xml);              // load some XML into it

$xpath = new DOMXPath($dom);      // create a new XPath
$nodes = $xpath->query('//item'); // Find all item elements
foreach($nodes as $node) {        // Iterate over found item elements
    echo $dom->saveXml($node);    // output the item node outerHTML
}

上記は<item>ノードをエコーし​​ます。出力をバッファリングし、文字列に連結し、配列に書き込んで内破するなど、ファイルに書き込むことができます。

DOM(およびルートノード)で適切に実行する場合、完全なコードは次のようになります。

$dom = new DOMDocument;                          // init DOMDocument for RSS
$dom->loadXML($xml);                             // load some XML into it

$items = new DOMDocument;                        // init DOMDocument for new file
$items->preserveWhiteSpace = FALSE;              // dump whitespace
$items->formatOutput = TRUE;                     // make output pretty
$items->loadXML('<items/>');                     // create root node

$xpath = new DOMXPath($dom);                     // create a new XPath
$nodes = $xpath->query('//item');                // Find all item elements
foreach($nodes as $node) {                       // iterate over found item nodes
    $copy = $items->importNode($node, TRUE);     // deep copy of item node
    $items->documentElement->appendChild($copy); // append item nodes
}
echo $items->saveXML();                          // outputs the new document

の代わりに、ファイルに書き込むためにsaveXML()使用します。save('filename.xml')

于 2010-06-16T21:46:47.790 に答える
1

別のアプローチは、XSLTを使用することです。

$xsl = <<< XSL
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<items>
  <xsl:copy-of select="//item">
    <xsl:apply-templates/>
  </xsl:copy-of>
</items>
</xsl:template>
</xsl:stylesheet>
XSL;

<item>上記のスタイルシートには、ソースXMLからXMLファイルにすべての要素をディープコピーし、ソースファイルから他のすべてを無視するという1つのルールしかありません。<items>ノードはルートノードの要素にコピーされます。これを処理するには、

$xslDoc = new DOMDocument();           // create Doc for XSLT
$xslDoc->loadXML($xsl);                // load stylesheet into it
$xmlDoc = new DOMDocument();           // create Doc for RSS
$xmlDoc->loadXML($xml);                // load your XML/RSS into it
$proc = new XSLTProcessor();           // init XSLT engine
$proc->importStylesheet($xslDoc);      // load stylesheet into engine
echo $proc->transformToXML($xmlDoc);   // output transformed XML

出力する代わりに、戻り値をファイルに書き込むことができます。

参考文献:

于 2010-06-16T22:21:15.863 に答える
0

試す:

<?php
$xmlFile = new DOMDocument(); //Instantiate new DOMDocument
$xmlFile->load("URL TO RSS/XML FILE"); //Load in XML/RSS file
$xmlString = file_get_contents("URL TO RSS/XML FILE"); 

$title[] = "";
$description[] = "";
$link[] = "";
$pubDate[] = "";
$guid[] = "";

for($i = 0; $i < substr_count($xmlString, "<item>"); $i++)
{
$title[] = $xmlFile->getElementsByTagName("title")->item(0)->nodeValue; //Get the value of the node <title>
$description[] = $xmlFile->getElementsByTagName("description")->item(0)->nodeValue;
$link[] = $xmlFile->getElementsByTagName("link")->item(0)->nodeValue;
$pubDate[] = $xmlFile->getElementsByTagName("pubDate")->item(0)->nodeValue;
$guid[] = $xmlFile->getElementsByTagName("guid")->item(0)->nodeValue;
}
?>

テストされていませんが、アレイ

$ title [] $ description [] $ link [] $ pubDate [] $ guid []

必要なすべてのデータを入力する必要があります。

編集: OK、別のアプローチ:

<?php
$xmlString = file_get_contents("URL TO RSS/XML FILE"); 
$titles = preg_filter("/<title>([.]*)</title>/","\\1", mixed $xmlString);
$descriptions = preg_filter("/<description>([.]*)</description>/","\\1", mixed $xmlString);
$links = preg_filter("/<link>([.]*)</link>/","\\1", mixed $xmlString);
$pubDates = preg_filter("/<pubDate>([.]*)</pubDate>/","\\1", mixed $xmlString);
$guids = preg_filter("/<guid>([.]*)</guid>/","\\1", mixed $xmlString);
?>

この例では、各変数に正しい値が入力されます。

于 2010-06-16T20:45:40.457 に答える