将 RSS-Feed 转变为另一种“标准”使用 PHP 的 XML 格式
简单的问题:我需要将默认的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
另一种方法是使用 XSLT:
上面的样式表只有一个规则,即将所有
元素从源 XML 深度复制到 XML 文件,并忽略源文件中的其他所有内容。这些节点将被复制到根节点的
元素中。要处理这个问题,您可以将返回值写入文件而不是输出。
进一步阅读:
A different approach would be to use an XSLT:
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 doInstead of outputting, you could just write the return value to file.
Further reading:
你所要求的几乎不是一个转变。您基本上只是按原样提取
元素。此外,您给出的结果不是有效的 XML,因为它缺少根节点。除此之外,您可以简单地这样做:
上面将回显
节点。您可以简单地缓冲输出,将其连接到字符串,向其写入数组并内爆等,然后将其写入文件。如果您想使用 DOM(和根节点)正确执行此操作,完整代码将是:
您可以使用
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:
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:
Instead of
saveXML()
, you'd usesave('filename.xml')
to write it to a file.尝试:
未经测试,但数组
$title[]
$描述[]
$链接[]
$pubDate[]
$guid[]
应该填充您需要的所有数据!
编辑:
好的,另一种方法是:
在这个例子中,每个变量都将填充正确的值。
Try:
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:
In this example each variable will be filled with the correct values.