0

我在这里有一个相当愚蠢的时刻。问题是这是两天愚蠢的时刻,而且不认识任何了解 perl 的人,我在这里变得相当混乱。考虑以下代码:

  use strict;
  use warnings;
  use XML::LibXML;
  my $filename = 'f:\proxml\1561\15610024.xml';
    my $parser = XML::LibXML->new();
  my $doc    = $parser->parse_file($filename);

  for my $line ($doc->findnodes('Document/Page/Lines')) {
          for my $property ($line->findnodes('/Document/Page/Lines/OCRCharacters')) {
    print $property->textContent();
  }
  }

我正在尝试打印 OCRCharacters 内容。这是 xml 文件的基本内容:

<Document>
  <Page>
    <Lines>
      <NumberOfLines>21</NumberOfLines>
      <LineNumber>1</LineNumber>
      <OCRCharacters>Test ocr in XML</OCRCharacters>
      <LineNumber>2</LineNumber>
      <OCRCharacters>This is the 2nd line</OCRCharacters>
    </Lines>
  </Page>
</Document>

我在这里搜索了我的问题的答案,但我显然错过了一些非常基本的东西。如果您有解决方案,以及时间,请回来。非常感谢

4

1 回答 1

0
$line->findnodes('/Document/Page/Lines/OCRCharacters')

应该

$line->findnodes('OCRCharacters')

use strict;
use warnings;
use XML::LibXML qw( );

my $parser = XML::LibXML->new();
my $doc    = $parser->parse_fh(\*DATA);

for my $line ($doc->findnodes('/Document/Page/Lines')) {
   for my $property ($line->findnodes('OCRCharacters')) {
      print $property->textContent(), "\n";
   }
}

__DATA__
<Document>
  <Page>
    <Lines>
      <NumberOfLines>21</NumberOfLines>
      <LineNumber>1</LineNumber>
      <OCRCharacters>Test ocr in XML</OCRCharacters>
      <LineNumber>2</LineNumber>
      <OCRCharacters>This is the 2nd line</OCRCharacters>
    </Lines>
  </Page>
</Document>

输出:

Test ocr in XML
This is the 2nd line
于 2013-04-22T03:37:17.300 回答