两层xml提要,从xml链接获取数据


Two tier xml feeds, get data from xml link

我使用了新的wordpress插件谷歌xml站点地图。

你好,假设我有100个帖子。我的站点地图xml文件没有100个条目,而是有5个链接,每个链接链接到20个帖子。

我想做的是把帖子的每个名字都放到服务器上的一个文件中

  1. sitemap.xml具有以下架构:<>站点地图…<>类别位置

loc表示5个链接,每个链接都链接到具有以下模式的另一个xml文件:<>url…<>post_name

现在,如果我想访问链接的类别,我会这样做:

$sitemap_feed = 'http://www.mysite.com/sitemap.xml';
$sitemap_xml = simplexml_load_file($sitemap_feed);
foreach( $sitemap_xml->sitemap as $xml){
$cat_location = $xml->category_location;
}

现在,我必须将所有类别位置存储在一个数组中,然后再次运行此代码来实际获取我的帖子名称。

$postname_xml = simplexml_load_file($cat_location);
foreach( $postname_xml->postname as $postname_xml){
$postname = $postname_xml->postname;
}

有没有直接的方法?更优雅的

谢谢!希望你能理解我的问题:(

您可以使用XPath直接从XML中筛选数据。如果你能把XML链接给我,我可能会在编码方面帮你一点忙。

XPath@Wikipedia
Xpath解释

将您的问题视为解决XML文件中提到的XML文件,我在网上找不到简单的解决方案。

我会采取以下步骤

  1. 通过XPath从sitemap.xml中提取URL。(xpath://sitemap/loc
  2. 解析内部XML并将其添加到主文档中
  3. 从主文档中收集所需信息(xpath://postname