在Perl中,使用XML :: Twig

时间:2015-07-22 14:35:40

标签: xml xml-parsing perl-module

以下是我要解析的xml文件:

<?xml version="1.0" encoding="UTF-8"?>

<topic id="yerus5" xmlns:ditaarch="http://dita.oasis-open.org/architecture/2005/">



<title/>
  <shortdesc/>
  <body>
<p><b>CCU_CNT_ADDR: (Address=0x004 Reset=32'h1)</b><table id="table_r5b_1xj_ts">
    <tgroup cols="4">
      <colspec colnum="1" colname="col1"/>
      <colspec colnum="2" colname="col2"/>
      <colspec colnum="3" colname="col3"/>
      <colspec colnum="4" colname="col4"/>
      <tbody>
        <row>
          <entry>Field</entry>
          <entry>OFFSET</entry>
          <entry>R/W Access</entry>
          <entry>Description</entry>
        </row>
        <row>
          <entry>reg2sm_cnt</entry>
          <entry>15:0</entry>
          <entry>R/W</entry>
          <entry>Count Value to increment in the extenral memory at the specified location.
            Default Value of 1. A Count value of 0 will clear the counter value</entry>
        </row>
        <row>
          <entry>ccu2bus_endianess</entry>
          <entry>24</entry>
          <entry>R/W</entry>
          <entry>Endianess of the data structure bit</entry>
        </row></tbody>
    </tgroup>
  </table><b>CCU_STAT_ADDR: (Address=0x008 Reset=32'h0)</b><table id="table_mcc_1xj_ts">
    <tgroup cols="4">
      <colspec colnum="1" colname="col1"/>
      <colspec colnum="2" colname="col2"/>
      <colspec colnum="3" colname="col3"/>
      <colspec colnum="4" colname="col4"/>
      <tbody>
        <row>
          <entry>Field</entry>
          <entry>OFFSET</entry>
          <entry>R/W Access</entry>
          <entry>Description</entry>
        </row>
        <row>
          <entry>fifo_cnt</entry>
          <entry>1:0</entry>
          <entry>R</entry>
          <entry>Status. 0x0 indicates that the engine is free. Will be 0x1 on a write to
            address</entry>
        </row>
        <row>
          <entry>rfifo_cnt</entry>
          <entry>3:2</entry>
          <entry>R</entry>
          <entry>Status. 0x0 indicates there are no pending read values from CCU engine.</entry>
        </row> </tbody>
    </tgroup>
  </table></p>


</body>
</topic>

运行以下代码后(In Perl, XML::Simple is not able to dereference multi dimensional associative array parsed by Data::Dumper处可用):

        use strict;
    use warnings;
    use XML::Twig;

    use Data::Dumper;

    my @headers;

    my $column_to_show = 'Field';

    sub process_row {
        my %entries;

        my ( $twig, $row ) = @_;
        my @row_entries = map { $_->text } $row->children;
        if (@headers) {
            @entries{@headers} = @row_entries;
            print $column_to_show, " => ", $entries{$column_to_show}, "\n";
        }
        else {
            @headers = @row_entries;
        }
    }

    my $twig = XML::Twig->new(
    'pretty_print' => 'indented_a',
    twig_handlers  => { 'row' => \&process_row }
)->parsefile ( 'your_file.xml' ); 

我可以访问<entry></entry>的每个数据。

我无法详细提取每个<b></b>文字的详细信息。是的,我能够提取所有<b></b>文本。但无法分别为每个<row></row>提取<b></b>。以下是样本输出:

Name: CCU_CNT_ADDR: (Address=0x004 Reset=32'h1)
Field: reg2sm_cnt 
OFFSET: 15:0 
Access: R/W 
Description: Count Value to increment in the extenral memory at the specified location. Default Value of 1. A Count value of 0 will clear the counter value 

Filed: ccu2bus_endianess 
OFFSET: 24 
Access: R/W 
Description: Endianess of the data structure bit 
 .
 .
 .
 .
 .
 .
 .
Name: CCU_STAT_ADDR: (Address=0x008 Reset=32'h0) 
Field: fifo_cnt 
.
 .
 .
 .
 .
 .
 .

我试过以下但不行:

foreach my $b ( $twig -> get_xpath ("//b") ) # Extract text of <b></b>
{

print $b ->text, "\n";
    foreach my $row ( $twig -> get_xpath ("//row") )
    {
        print $row ->text, "\n";
    }
}

1 个答案:

答案 0 :(得分:0)

好的,给出你的例子 - 它实际上有点刺激,因为XML没有明确地关联&#39;标题&#39;用表&#39;表&#39; (例如,将它们封装在XML节点中)。

但是,您可以使用prev_sibling方法将前一个元素放在同一级别。

#!/usr/bin/env perl
use strict;
use warnings;
use XML::Twig;

my $twig = XML::Twig->new->parsefile ( 'your_file.xml' );

foreach my $table ( $twig->get_xpath('//table') ) {
    my $header = $table->prev_sibling->text;
    print "Name: $header\n";
    my @headers;
    foreach my $row ( $table->get_xpath("tgroup/tbody/row") ) {
        my %entries;
        my @row_entries = map { $_->text =~ s/\n\s+//rg; } $row->children;
        if (@headers) {
            @entries{@headers} = @row_entries;
            foreach my $field (@headers) {
                print "$field: $entries{$field}\n";
            }
        }
        else {
            @headers = @row_entries;
        }
    }
    print "----\n";
}

注意 - 此假定 table&#39;之前的&#39;元素是标题。它适用于您的特定情况,但只有在始终您想要显示的<table>之前的元素时才能正常工作。

  • 我们经营一个&#39; foreach&#39;循环,挑选出名为table的元素(样本中有两个元素。
  • 每个表格,我们假设前一个兄弟元素是标题。在这种情况下,这是您的<b>元素。但要小心,因为<b>在HTML中表示粗体并且是格式化标记。
  • 然后我们基本上做同样的事情 - 对于每个表,分解行,使得我们有一个标题和一堆列,然后每行打印一个。
  • 作为这样做的一部分,我使用正则表达式来删除换行符和空格&#39; (s/\n\s+//gr)因为说明上的格式看起来有些“关闭”。显然你可以删除它,如果它不受欢迎。 (注意 - 这仅适用于较新的perl版本 - 5.14 + IIRC)

这会产生:

Name: CCU_CNT_ADDR: (Address=0x004 Reset=32'h1)
Field: reg2sm_cnt
OFFSET: 15:0
R/W Access: R/W
Description: Count Value to increment in the extenral memory at the specified location.Default Value of 1. A Count value of 0 will clear the counter value
Field: ccu2bus_endianess
OFFSET: 24
R/W Access: R/W
Description: Endianess of the data structure bit
----
Name: CCU_STAT_ADDR: (Address=0x008 Reset=32'h0)
Field: fifo_cnt
OFFSET: 1:0
R/W Access: R
Description: Status. 0x0 indicates that the engine is free. Will be 0x1 on a write toaddress
Field: rfifo_cnt
OFFSET: 3:2
R/W Access: R
Description: Status. 0x0 indicates there are no pending read values from CCU engine.
----