将RSS源转换为另一个“RSS源”;标准;XML格式与PHP
快速提问:我需要将默认RSS结构转换为另一种XML格式 RSS文件类似于将RSS源转换为另一个“RSS源”;标准;XML格式与PHP,php,xml,dom,xpath,transformation,Php,Xml,Dom,Xpath,Transformation,快速提问:我需要将默认RSS结构转换为另一种XML格式 RSS文件类似于 <?xml version="1.0" encoding="UTF-8"?> <rss version="2.0"> <channel> <title>Name des RSS Feed</title> <description>Feed Beschreibung</desc
<?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>
名称des RSS源
饲料白蜡
判定元件
http://xml-rss.de
2000年1月1日星期六00:00:00 GMT
纳克里赫特滴度
这是我的梦想
http://xml-rss.de/link-zur-nachricht.htm
星期六,1。2000年1月00:00:00 GMT
01012000-000000
纳克里赫特滴度
这是我的梦想
http://xml-rss.de/link-zur-nachricht.htm
星期六,1。2000年1月00:00:00 GMT
01012000-000000
纳克里赫特滴度
这是我的梦想
http://xml-rss.de/link-zur-nachricht.htm
星期六,1。2000年1月00:00:00 GMT
01012000-000000
…我只想提取item元素(带有child和属性)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>
...
纳克里赫特滴度
这是我的梦想
http://xml-rss.de/link-zur-nachricht.htm
星期六,1。2000年1月00:00:00 GMT
01012000-000000
...
它不必存储到文件中。我只需要输出
编辑:此外,您需要知道:RSS文件可能包含动态数量的项目。这只是一个样本。因此,它必须循环使用while,for,for,each
我用DOMNode、SimpleXML、XPath等尝试了不同的方法。。。但是没有成功
谢谢
克里斯试试:
<?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;
}
?>
load(“RSS/XML文件的URL”)//加载到XML/RSS文件中
$xmlString=file_get_contents(“RSS/XML文件的URL”);
$title[]=“”;
$description[]=“”;
$link[]=“”;
$pubDate[]=“”;
$guid[]=“”;
对于($i=0;$igetElementsByTagName(“title”)->item(0)->nodeValue;//获取节点的值
$description[]=$xmlFile->getElementsByTagName(“description”)->项(0)->节点值;
$link[]=$xmlFile->getElementsByTagName(“link”)->item(0)->nodeValue;
$pubDate[]=$xmlFile->getElementsByTagName(“pubDate”)->项(0)->节点值;
$guid[]=$xmlFile->getElementsByTagName(“guid”)->项(0)->节点值;
}
?>
未经测试,但阵列
$title[]
$description[]
$link[]
$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);
?>
在本例中,每个变量都将填充正确的值。您要求的几乎不是转换。您基本上只是按原样提取
元素。此外,您给出的结果是无效的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
$dom=newdomdocument;//用于RSS的初始化DOMDocument
$dom->loadXML($xml);//加载一些XML到其中
$items=新文档;//初始化新文件的DOMDocument
$items->preserveWhiteSpace=FALSE;//转储空白
$items->formatOutput=TRUE;//使输出美观
$items->loadXML(“”);//创建根节点
$xpath=new-DOMXPath($dom);//创建新的XPath
$nodes=$xpath->query('//item');//查找所有项目元素
foreach($nodes as$node){//迭代找到的项节点
$copy=$items->importNode($node,TRUE);//项目节点的深度复制
$items->documentElement->appendChild($copy);//追加项目节点
}
echo$items->saveXML();//输出新文档
您可以使用
save('filename.xml')
将其写入文件,而不是saveXML()
$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
var,方法与使用herdoc语法指定$xsl
相同。或者使用->load('filename.xml')
.Hm,您确定没有忘记什么吗?因为我没有得到任何输出。是的,它是有效的。我的错..我的代码有一个愚蠢的错误。再次感谢
$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