简体   繁体   English

Perl简单的XML问题

[英]Perl simple xml question

I am trying to parse a simple XML file that I created... it looks like this: 我正在尝试解析我创建的简单XML文件...它看起来像这样:

<project name="AU" date="2/2/2011" location="proj1">
  <GenomeList name="Processed">
    <genome name="AU4" />
  </GenomeList>
  <GenomeList name="Unprocessed">
      <genome name="AU1" />
      <genome name="AU2" />
      <genome name="AU3" />
  </GenomeList>
</project>

The problem I am having is the parsing of the genome list: It seems that when there is only one element in the list it adds the entry in a different way, as can be seen here: 我遇到的问题是基因组列表的解析:似乎当列表中只有一个元素时,它会以不同的方式添加条目,如下所示:

'Unprocessed' => {
                  'genome' => {
                              'AU2' => {},
                              'AU3' => {},
                              'AU1' => {}
                              }
                 },
'Processed' => {
                'genome' => {
                            'name' => 'AU4'
                            }
               }

Is there any way I can get it to parse in a consistent way? 有什么办法可以使我以一致的方式进行解析?

I tried using valueAttr but it didnt seem to be working 我尝试使用valueAttr,但似乎没有用

Thanks 谢谢

This is a pretty common issue with using XML::Simple so I'm guessing this is what you're using. 使用XML :: Simple是一个非常普遍的问题,所以我猜这就是您正在使用的。 ForceArray will probably do what you want. ForceArray可能会做您想要的。 See the examples and output below; 参见下面的示例和输出; the third print statement should do what you want. 第三个打印语句应执行您想要的操作。 Note that XML::Simple is decidedly not simple once your usage expands beyond trivial and predictable in/out stuff. 请注意,一旦您的用法超出了琐碎且可预测的输入/输出内容,XML :: Simple绝对不是简单的。 If you want to use it, don't stop reading its documentation at the word “Simple.” 如果要使用它,请不要停止阅读“简单”一词的文档。

XML::LibXML or perhaps XML::Twig are also well worth learning. XML :: LibXMLXML :: Twig也是值得学习的。

use warnings;
use strict;
use XML::Simple;
use Data::Dumper;
$Data::Dumper::Terse = 1;
$Data::Dumper::Indent = 1;

my $multi = <<"";
<project name="AU" date="2/2/2011" location="proj1">
  <GenomeList name="Processed">
    <genome name="AU4" />
  </GenomeList>
  <GenomeList name="Unprocessed">
      <genome name="AU1" />
      <genome name="AU2" />
      <genome name="AU3" />
  </GenomeList>
</project>

my $single = <<"";
<project name="AU" date="2/2/2011" location="proj1">
  <GenomeList name="Processed">
    <genome name="AU4" />
  </GenomeList>
  <GenomeList name="Unprocessed">
      <genome name="AU1" />
  </GenomeList>
</project>

print Dumper( XMLin($multi) );
print Dumper( XMLin($single) );
print Dumper( XMLin( $single, ForceArray => 1 ) );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM