如何在Perl中解析XML并创建树结构 [英] How to parse XML and create a tree structure in Perl
问题描述
我正在使用 XML :: Simple $
I am parsing a XML file with XML::Simple
. Is there any way to get a tree form from the XML? If so please explain with example or suggest a CPAN package.
我想知道在列
之后必须处理哪个标签,依此类推。
I would like to know which tag I have to process after column
and so on.
标签没有序列。 列
标记可以多次出现在 Table
或 display_name
之后。
There is no sequence for the tags. The column
tag can appear after Table
or display_name
many times.
Tab
column
Table
column
display_name
column
display_name
XML:
<Tab>
<column>
<display_name>xyz</display_name>
<display_name>pqr</display_name>
</column>
<Table>
<column><display_name>Department</display_name></column>
</Table>
<display_name>abc</display_name>
<column>pwd</column>
<display_name>jack</display_name>
</Tab>
使用XML :: Simple:
output with XML::Simple:
$VAR1 = {
'Table' => {
'column' => {
'display_name' => 'Department'
}
},
'display_name' => [
'abc',
'jack'
],
'column' => [
{
'display_name' => [
'xyz',
'pqr'
]
},
'pwd'
]
};
预期o / p:
$VAR1 = {
'column' => {
'display_name' => [
'xyz',
'pqr'
]
}
'Table' => {
'column' => {
'display_name' => 'Department'
}
},
'display_name' => 'abc',
'column' => 'pwd',
'display_name' =>'jack'
};
我知道使用相同键的哈希是不可能的。请提出一种我可以维护标签序列并能够打印它们的方法。
I know a hash with same keys isn't possible. Please suggest a way that I can maintain the sequence of tags and will be able to print them.
推荐答案
XML :: LibXML创建
XML::LibXML creates a tree with no loss of information.
use XML::LibXML qw( );
my $parser = XML::LibXML->new();
my $tree = $parser->parse_file($qfn);
您可以从此处生成指定的输出。 (我不知道为什么要这么做,因为要运行的Perl代码在运行时会丢失数据。)
You can generate the output you specified from there. (I don't know why you'd want to, since the Perl code you want for output would lose data if run.)
这篇关于如何在Perl中解析XML并创建树结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!