【问题标题】:Perl XML::DOM::ParserPerl XML::DOM::解析器
【发布时间】:2013-07-24 11:42:15
【问题描述】:

我正在尝试使用 perl、XML::DOM 和 XML::Parser 从 RSS 提要中获取 som 信息。 我很难获得有关 XML::DOM 和 XML::Parser 的 som 文档 :(

这是 rss 提要支出。

<rss version="2.0">
<channel>
    <item>
        <title>The title numer 1</title>
        <link>
        http://www.example.com/link1.php?getfile=1&sha=1234567890
        </link>
        <description>
        File 1
        </description>
    </item>
    <item>
        <title>The title numer 2</title>
        <link>
        http://www.example.com/link1.php?getfile=2&sha=0192837465
        </link>
        <description>
        File 2
        </description>
    </item>
        <item>
        <title>The title numer 3</title>
        <link>
        http://www.example.com/link1.php?getfile=1&sha=0987654321
        </link>
        <description>
        File 3
        </description>
    </item>
</channel>

所以我试图从这个 rss 提要中获取“标题”和“链接”。

我不能使用 XML::LibXML 或 XML::simple 或 XML::RSS

【问题讨论】:

    标签: perl xml-parsing xmldom


    【解决方案1】:

    您的 XML 数据有问题(未加引号的 '&' 字符):

    行如

    ...getfile=1&sha...
    

    必须写成

    ...getfile=1&amp;sha...
    

    一旦这个问题得到解决,您就可以使用 XML::Reader:PP 来解析 XML:

    use strict;
    use warnings;
    
    use XML::Reader::PP;
    
    my $rdr = XML::Reader::PP->new(\*DATA, { mode => 'branches' },
      { root => '/rss/channel/item', branch => [ '/title', '/link' ] });
    
    while ($rdr->iterate) {
        my ($title, $link) = $rdr->value;
    
        for ($title, $link) {
            $_ = '' unless defined $_;
        }
    
        print "title = '$title'\n";
        print "link  = '$link'\n";
    }
    
    __DATA__
    <rss version="2.0">
      <channel>
        <item>
            <title>The title numer 1</title>
            <link>
            http://www.example.com/link1.php?getfile=1&amp;sha=1234567890
            </link>
            <description>
            File 1
            </description>
        </item>
        <item>
            <title>The title numer 2</title>
            <link>
            http://www.example.com/link1.php?getfile=2&amp;sha=0192837465
            </link>
            <description>
            File 2
            </description>
        </item>
            <item>
            <title>The title numer 3</title>
            <link>
            http://www.example.com/link1.php?getfile=1&amp;sha=0987654321
            </link>
            <description>
            File 3
            </description>
        </item>
      </channel>
    </rss>
    

    【讨论】:

      【解决方案2】:

      解析您的 RSS XML 文件时出现问题。对于文件

      <xml>
      <channel>
          <item>
              <title>The title numer 1</title>
              </item>
      
          <item>
              <title>The title numer 2</title>
              </item>
      </channel>
      </xml>
      

      你可以的

      use strict;
      use warnings;
      use XML::Parser;
      use Data::Dumper;
      use XML::DOM::Lite qw(Parser XPath);
      
      my $parser = Parser->new();
      my $doc = $parser->parseFile('2.xml', whitespace => 'strip');
      
      
      #XML::DOM::Lite::NodeList - blessed array ref for containing Node objects
      my $nlist = $doc->selectNodes('/xml/channel/item/title');
      
      
      foreach my $node (@{$nlist})
      {
          print $node->firstChild()->nodeValue() . "\n";
      }
      

      【讨论】:

        【解决方案3】:

        我在尝试安装它时遇到错误,但它看起来像这样:

        use XML::DOM::Parser qw( );
        use XML::XQL         qw( );
        use XML::XQL::DOM    qw( );
        
        my $parser = XML::DOM::Parser->new();
        my $doc = $parser->parsefile("file.xml");
        
        for my $item_node ($doc->xql('/channel/item')) {
           my $title = join '', $item_node->xql('title/textNode()');
           my $link  = join '', $item_node->xql('link/textNode()');
           ...
        }
        

        【讨论】:

          猜你喜欢
          • 2011-05-09
          • 1970-01-01
          • 2013-11-15
          • 2022-11-04
          • 1970-01-01
          • 2010-11-24
          • 1970-01-01
          • 2010-12-25
          • 2012-09-10
          相关资源
          最近更新 更多