两层xml订阅源,从xml链接获取数据

时间:2011-06-02 20:29:24

标签: php xml

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

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

我要做的是将帖子的每个名字都放到我服务器上的文件中。

  1. sitemap.xml具有以下架构: <>网站地图    ...<> category_location
  2. 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;
    
    }
    

    有没有办法直接这样做?更优雅的东西?

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

1 个答案:

答案 0 :(得分:0)

您可以使用XPath直接从XML过滤数据。如果你可以链接我的XML,我可能会帮助你一点点编码。

XPath @ Wikipedia
Xpath explained

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

我会按照以下步骤进行操作

  1. 通过XPath将URL从sitemap.xml中煮出来。 (xpath://sitemap/loc
  2. 解析内部XML并将其添加到主文档
  3. 从主文档(xpath://postname
  4. 中收集所需信息
相关问题