Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何访问由XML::Parser创建的解析树?_Xml_Perl - Fatal编程技术网

如何访问由XML::Parser创建的解析树?

如何访问由XML::Parser创建的解析树?,xml,perl,Xml,Perl,我有一个数组引用,里面有一些数组引用。嵌套数组引用还包含数组引用。(这是我的风格。) 这里的$Tree是数组引用,它将是数组引用,内容和嵌套深度都取决于xml文件。我想遍历嵌套数组$Tree并打印内容。这里是一个简单的版本: use strict; use warnings; sub printElement { my ($tag, $content) = @_; if (ref $content) { # This is a XML element: my $att

我有一个数组引用,里面有一些数组引用。嵌套数组引用还包含数组引用。(这是我的风格。)


这里的
$Tree
是数组引用,它将是数组引用,内容和嵌套深度都取决于xml文件。我想遍历嵌套数组
$Tree
并打印内容。

这里是一个简单的版本:

use strict;
use warnings;

sub printElement
{
  my ($tag, $content) = @_;

  if (ref $content) {
    # This is a XML element:
    my $attrHash = $content->[0];

    print "<$tag>";           # I'm ignoring attributes

    for (my $i = 1; $i < $#$content; $i += 2) {
      printElement(@$content[$i, $i+1]);
    }

    print "</$tag>";
  } else {
    # This is a text pseudo-element:
    print $content;             # I'm not encoding entities
  }
} # end printElement

sub printTree
{
  # The root tree is always a 2-element array
  # of the root element and its content:
  printElement(@{ shift @_ });
  print "\n";
}

# Example parse tree from XML::Parser:
my $tree =
    ['foo', [{}, 'head', [{id => "a"}, 0, "Hello ",  'em', [{}, 0, "there"]],
             'bar', [ {}, 0, "Howdy",  'ref', [{}]],
             0, "do"
            ]
    ];

printTree($tree);
使用严格;
使用警告;
子打印元素
{
我的($tag,$content)=@;
如果(参考$content){
#这是一个XML元素:
我的$attrHash=$content->[0];
打印“”;#我忽略属性
对于(我的$i=1;$i<$#$content;$i+=2){
打印元素(@$content[$i,$i+1]);
}
打印“”;
}否则{
#这是一个文本伪元素:
打印$content;#我没有对实体进行编码
}
}#末端打印元件
子打印树
{
#根树始终是一个2元素数组
#根元素及其内容的名称:
printElement(@{shift@});
打印“\n”;
}
#XML::Parser中的解析树示例:
我的美元树=
['foo',[{},'head',[{id=>“a”},0,“Hello”,'em',[{},0,“there”],
'bar'、[{}、0、'Howdy'、'ref'、[{}]],
0,“做”
]
];
printree($tree);

这不会打印属性,尽管您可以通过
$attrHash
访问它们。它也不会对文本中的实体进行编码,因此结果可能不是格式良好的XML。我将这些作为练习留给读者。:-)

可能就是你要找的。您也可以手动使用。

您可以包含示例文件或其中的一些文本。它只是一个xml文件。您可以尝试使用任何xml文件。您应该使用
xml::Parser->new(Style=>'tree')
而不是
new xml::Parser(Style=>'tree')
。间接对象语法可能导致奇怪的错误。
use strict;
use warnings;

sub printElement
{
  my ($tag, $content) = @_;

  if (ref $content) {
    # This is a XML element:
    my $attrHash = $content->[0];

    print "<$tag>";           # I'm ignoring attributes

    for (my $i = 1; $i < $#$content; $i += 2) {
      printElement(@$content[$i, $i+1]);
    }

    print "</$tag>";
  } else {
    # This is a text pseudo-element:
    print $content;             # I'm not encoding entities
  }
} # end printElement

sub printTree
{
  # The root tree is always a 2-element array
  # of the root element and its content:
  printElement(@{ shift @_ });
  print "\n";
}

# Example parse tree from XML::Parser:
my $tree =
    ['foo', [{}, 'head', [{id => "a"}, 0, "Hello ",  'em', [{}, 0, "there"]],
             'bar', [ {}, 0, "Howdy",  'ref', [{}]],
             0, "do"
            ]
    ];

printTree($tree);