将 RSS-Feed 转变为另一种“标准”使用 PHP 的 XML 格式

发布于 2024-09-06 02:43:07 字数 2393 浏览 3 评论 0原文

简单的问题:我需要将默认的 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>

...我只想提取项目元素(带有子项和属性)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 等不同的方法,但没有成功。

谢谢 克里斯

quick question: I need to transform a default RSS Structure into another XML-format.

The RSS File is like....

<?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>

...and I want to extract only the item-elements (with childs and attributes) XML like:

<?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>
...

It hasn't to be stored into a file. I need just the output.

edit: Furthermore you need to know: The RSS File could have dynamic numbers of items. This is just a sample. So it has to be looped with while, for, for-each, ...

I tried different approaches with DOMNode, SimpleXML, XPath, ... but without success.

Thanks
chris

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

话少心凉 2024-09-13 02:43:07

另一种方法是使用 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;

上面的样式表只有一个规则,即将所有 元素从源 XML 深度复制到 XML 文件,并忽略源文件中的其他所有内容。这些节点将被复制到根节点的 元素中。要处理这个问题,

$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

您可以将返回值写入文件而不是输出。

进一步阅读:

A different approach would be to use an 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;

The above stylesheet has just one rule, namely deep copying all <item> elements from the source XML to an XML file and ignore everything else from the source file. The nodes will be copied into an <items> element for root node. To process this, you'd do

$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

Instead of outputting, you could just write the return value to file.

Further reading:

孤芳又自赏 2024-09-13 02:43:07

你所要求的几乎不是一个转变。您基本上只是按原样提取 元素。此外,您给出的结果不是有效的 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
}

上面将回显 节点。您可以简单地缓冲输出,将其连接到字符串,向其写入数组并内爆等,然后将其写入文件。

如果您想使用 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

您可以使用 save('filename.xml') 而不是 saveXML() 将其写入文件。

What you ask for is hardly a transformation. You are basically just extracting the <item> elements as they are. Also, the result you give is not valid XML, as it lacks a root node.

Apart from that, you can simple do it like this:

$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
}

The above would echo the <item> nodes. You could simply buffer the output, concatenate it to a string, write to it an array and implode, etc - and write it to file.

If you want to do it properly with DOM (and a root node), the full code would be:

$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

Instead of saveXML(), you'd use save('filename.xml') to write it to a file.

青春有你 2024-09-13 02:43:07

尝试:

<?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[]
$描述[]
$链接[]
$pubDate[]
$guid[]

应该填充您需要的所有数据!

编辑:
好的,另一种方法是:

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

在这个例子中,每个变量都将填充正确的值。

Try:

<?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;
}
?>

Untested but the arrays

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

should be populated with all of the data that you need!

EDIT:
OK so another approach:

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

In this example each variable will be filled with the correct values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文