Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.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
用perl解析xml文件_Xml_Perl - Fatal编程技术网

用perl解析xml文件

用perl解析xml文件,xml,perl,Xml,Perl,我需要存储xml数据 <pathway name="path:ko00010" org="ko" number="00010" title="Glycolysis / Gluconeogenesis" image="http://www.kegg.jp/kegg/pathway/ko/ko00010.png" link="http://www.kegg.jp/kegg-bin/show_pathway?ko000

我需要存储xml数据

<pathway name="path:ko00010" org="ko" number="00010"
             title="Glycolysis / Gluconeogenesis"
             image="http://www.kegg.jp/kegg/pathway/ko/ko00010.png"
             link="http://www.kegg.jp/kegg-bin/show_pathway?ko00010">
        <entry id="13" name="ko:K01623 ko:K01624 ko:K01622 ko:K11645 ko:K16305 ko:K16306" type="ortholog" reaction="rn:R01070"
            link="http://www.kegg.jp/dbget-bin/www_bget?K01623+K01624+K01622+K11645+K16305+K16306">
            <graphics name="K01623..." fgcolor="#000000" bgcolor="#BFBFFF"
                 type="rectangle" x="483" y="404" width="46" height="17"/>
        </entry>
 </pathway>
到目前为止,我可以访问除图形节点及其子节点之外的所有元素

my @grand_kids = $child -> childNodes();
在第二个foreach中,通过属性执行另一个步骤

我已经为你树立了榜样

#!/usr/bin/perl     
use XML::LibXML;
use strict;
use warnings;
my $parser = new XML::LibXML;

my $xmlp= $parser->parse_file("ko00010.xml");
my $rootel = $xmlp->getDocumentElement();

my $elname = $rootel->getName();
my @rootelements=$rootel->getAttributes();

foreach my $rootatt(@rootelements){
    printf "R {%s}[%s]\t", $rootatt->getName(), $rootatt->getValue();
}

my @kids = $rootel -> childNodes();
foreach my $child(@kids) {
    printf "\nCH = %s\n",  $child->getName();
    my @atts = $child->getAttributes();
    foreach my $at (@atts) {
        printf "C {%s}[%s]\t", $at->getName(), $at->getValue();
    }
    my @grand_kids=$child->childNodes();
    foreach my $grand_child(@grand_kids) {
        printf "\nGR CH = %s\n",  $grand_child->getName();
        my @atts2 = $grand_child->getAttributes();
        foreach my $at2 (@atts2) {
            printf "GC {%s}[%s]\t", $at2->getName(), $at2->getValue();
        }
    }
}
给出此输出-(我不确定#文本节点来自何处)

你需要做什么

my @grand_kids = $child -> childNodes();
在第二个foreach中,通过属性执行另一个步骤

我已经为你树立了榜样

#!/usr/bin/perl     
use XML::LibXML;
use strict;
use warnings;
my $parser = new XML::LibXML;

my $xmlp= $parser->parse_file("ko00010.xml");
my $rootel = $xmlp->getDocumentElement();

my $elname = $rootel->getName();
my @rootelements=$rootel->getAttributes();

foreach my $rootatt(@rootelements){
    printf "R {%s}[%s]\t", $rootatt->getName(), $rootatt->getValue();
}

my @kids = $rootel -> childNodes();
foreach my $child(@kids) {
    printf "\nCH = %s\n",  $child->getName();
    my @atts = $child->getAttributes();
    foreach my $at (@atts) {
        printf "C {%s}[%s]\t", $at->getName(), $at->getValue();
    }
    my @grand_kids=$child->childNodes();
    foreach my $grand_child(@grand_kids) {
        printf "\nGR CH = %s\n",  $grand_child->getName();
        my @atts2 = $grand_child->getAttributes();
        foreach my $at2 (@atts2) {
            printf "GC {%s}[%s]\t", $at2->getName(), $at2->getValue();
        }
    }
}
给出此输出-(我不确定#文本节点来自何处)


另一种完全不同的方法:使用XML模式,并使用CPAN模块
XML::Compile
自动转换XML数据。与其他xml-to-data工具(如
xml::Simple
)相比,
xml::Compile
无需猜测或使用“ForceArray”等选项进行调整,而且如果子元素有时变成数组,有时变成标量,也不会有什么意外

如果您的数据没有XML模式,则可以使用
trang
自动创建一个:

trang testdata.xml schema.xsd
XML::Compile
随命令行工具
xml2yaml
提供,用于快速转换:

xml2yaml testdata.xml schema.xsd > testdata.yaml

另一种完全不同的方法:使用XML模式,并使用CPAN模块
XML::Compile
自动转换XML数据。与其他xml-to-data工具(如
xml::Simple
)相比,
xml::Compile
无需猜测或使用“ForceArray”等选项进行调整,而且如果子元素有时变成数组,有时变成标量,也不会有什么意外

如果您的数据没有XML模式,则可以使用
trang
自动创建一个:

trang testdata.xml schema.xsd
XML::Compile
随命令行工具
xml2yaml
提供,用于快速转换:

xml2yaml testdata.xml schema.xsd > testdata.yaml

我不清楚您到底要创建什么样的数据结构。或者,当您可以使用XPath获取所需的数据而无需将XML映射到其他内容时,为什么要创建数据结构


在我看来,你有点像是在模仿你所做的。在这种情况下,是否不直接使用XML::Simple?我知道一般不建议对任何复杂的XML使用它,但是如果您的XML很简单,并且由XML::simple创建的数据对您有效,那么使用广泛使用的模块可能比尝试重写它更安全(我应该知道,我用XML::Twig重写了它,这并不特别困难,但也不一定是完全琐碎的).

我不清楚您到底要创建什么样的数据结构。或者,当您可以使用XPath获取所需的数据而无需将XML映射到其他内容时,为什么要创建数据结构


在我看来,你有点像是在模仿你所做的。在这种情况下,是否不直接使用XML::Simple?我知道一般不建议对任何复杂的XML使用它,但是如果您的XML很简单,并且由XML::simple创建的数据对您有效,那么使用广泛使用的模块可能比尝试重写它更安全(我应该知道,我用XML::Twig重写了它,这并不特别困难,但也不一定是完全琐碎的).

XML::Simple将起作用,但也建议使用LibXML。下面是关于一些显著差异以及从XML::Simple到LibXML的转换的示例

使用LibXML并使用XPathContextfindnodes执行此操作的一种方法:

use strict;
use warnings;
use XML::LibXML;
use Data::Dumper;

my $parser    = XML::LibXML->new();
my $doc       = $parser->parse_file("ko00010.xml");
my $root      = $doc->getDocumentElement();
my %nodeHash  = ();

# get list of nodes and stores each nodeName(key) and textContent(value) in %nodeHash
my $perlmatch = sub {
    die "Not a nodelist"
      unless $_[0]->isa('XML::LibXML::NodeList');
    die "Missing a regular expression"
      unless defined $_[1];
    my $i = 0;
    while ( my $node = $_[0]->get_node($i++) ) {
        push @{ $nodeHash{$node->nodeName} }, $node->textContent; 
    }
};

# Create XPathContext and find all nodes
my $xc = XML::LibXML::XPathContext->new($root);
$xc->registerFunction( 'perlmatch', $perlmatch ); # register 'perlmatch' function   
$xc->findnodes('perlmatch(//*, ".")') or die "Error retrieving nodes."; # //* is to go through all parent and child nodes, "." to match any nodeName

print Dumper(%nodeHash); # print the contents of nodeHash (you can see the final hash structure here)

取自上的示例(对于所有节点,替换为哈希而不是数组和“.”)。

XML::Simple将起作用,但也建议使用LibXML。下面是关于一些显著差异以及从XML::Simple到LibXML的转换的示例

使用LibXML并使用XPathContextfindnodes执行此操作的一种方法:

use strict;
use warnings;
use XML::LibXML;
use Data::Dumper;

my $parser    = XML::LibXML->new();
my $doc       = $parser->parse_file("ko00010.xml");
my $root      = $doc->getDocumentElement();
my %nodeHash  = ();

# get list of nodes and stores each nodeName(key) and textContent(value) in %nodeHash
my $perlmatch = sub {
    die "Not a nodelist"
      unless $_[0]->isa('XML::LibXML::NodeList');
    die "Missing a regular expression"
      unless defined $_[1];
    my $i = 0;
    while ( my $node = $_[0]->get_node($i++) ) {
        push @{ $nodeHash{$node->nodeName} }, $node->textContent; 
    }
};

# Create XPathContext and find all nodes
my $xc = XML::LibXML::XPathContext->new($root);
$xc->registerFunction( 'perlmatch', $perlmatch ); # register 'perlmatch' function   
$xc->findnodes('perlmatch(//*, ".")') or die "Error retrieving nodes."; # //* is to go through all parent and child nodes, "." to match any nodeName

print Dumper(%nodeHash); # print the contents of nodeHash (you can see the final hash structure here)

取自上的示例(替换为哈希而不是数组和“.”,适用于所有节点)。

我尝试了这一点,但在
中,我的$child(@kids)
图形不是
@kids
中的
$child
图形。我的意思是
childnodes()
不返回图形作为
my@childs=$rootel->childnodes()中的节点谢谢你的回答,尝试使用xml simple,它确实很简单,我可以设法分析所有元素。我尝试了这一点,但在
中,我的$child(@kids)
图形不是
@kids
中的
$child
图形。我的意思是
childnodes()
不返回图形作为
my@childs=$rootel->childnodes()中的节点感谢您的回答,尝试使用xml simple,它确实很简单,我可以设法解析所有元素。我真的不知道如何继续。在发布此问题之前,我尝试了许多不同的模块,我更喜欢使用此xml::lib@shaq-好吧,为此,你坚持错误(即更难)模块。libxml在某些方面很好,但对于返回所需的数据结构,有更多合适的模块。我真的不知道如何继续。在发布此问题之前,我尝试了很多不同的模块,我更喜欢使用以下XML:lib@shaq-好的,为此,您粘贴了错误(即更难)的模块。libxml在某些方面很好,但是对于返回所需的数据结构,有更多合适的模块。Cross-posted from Cross-posted from