使用 XML::LibXML 解析 XML 的 Perl 脚本;

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10404152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:28:12  来源:igfitidea点击:

Perl script to parse XML using XML::LibXML;

xmlperllibxml2

提问by lozwell

I think this is a very simple issue, but I cannot figure it out despite many searches.

我认为这是一个非常简单的问题,但尽管进行了多次搜索,我还是无法弄清楚。

I am trying to parse the following XML to print something similar to TAG=VALUE, so that I can write this to a CSV file. The problem is the tags are not always the same for each sample. I cannot seem to figure out how to get the actual tag names. Any help appreciated!!!

我正在尝试解析以下 XML 以打印类似于 TAG=VALUE 的内容,以便我可以将其写入 CSV 文件。问题是每个样本的标签并不总是相同的。我似乎无法弄清楚如何获得实际的标签名称。任何帮助表示赞赏!!!

XML File -

XML 文件 -

<Statistics>
  <Stats>
    <Sample>
        <Name>System1</Name>
        <Type>IBM</Type>
        <Memory>2GB</Memory>
        <StartTime>2012-04-26T14:30:01Z</StartTime>
        <EndTime>2012-04-26T14:45:01Z</EndTime>
    </Sample>

    <Sample>
        <Name>System2</Name>
        <Type>Intel</Type>
        <Disks>2</Disks>
        <StartTime>2012-04-26T15:30:01Z</StartTime>
        <EndTime>2012-04-26T15:45:01Z</EndTime>
        <Video>1</Video>
    </Sample>
  </Stats>
</Statistics>

Script -

脚本 -

#!/usr/bin/perl
use XML::LibXML;

$filename = "data.xml";

my $parser = XML::LibXML->new();
my $xmldoc = $parser->parse_file($filename);

for my $sample ($xmldoc->findnodes('/Statistics/Stats/Sample')) {

print $sample->nodeName(), ": ", $sample->textContent(), "\n";

}

回答by Grant McLean

You have the right method for getting the tag names, you just need an extra loop to run through the tags inside each <sample>:

您有获取标签名称的正确方法,您只需要一个额外的循环来运行每个内部的标签<sample>

#!/usr/bin/perl

use strict;
use warnings;

use XML::LibXML;

my $filename = "data.xml";

my $parser = XML::LibXML->new();
my $xmldoc = $parser->parse_file($filename);

for my $sample ($xmldoc->findnodes('/Statistics/Stats/Sample')) {
    for my $property ($sample->findnodes('./*')) {
        print $property->nodeName(), ": ", $property->textContent(), "\n";
    }
    print "\n";
}

Edit: I have now created a tutorial site called Perl XML::LibXML by Examplewhich answers exactly this type of question.

编辑:我现在创建了一个名为Perl XML::LibXML by Example的教程站点它正好回答了这类问题。

回答by dpp

You need to iterate over the children of sample node,

您需要遍历示例节点的子节点,

for my $sample ( $xmldoc->findnodes('/Statistics/Stats/Sample') ) {
    print $sample->nodeName(), "\n";
    foreach my $child ( $sample->getChildnodes ) {
        if ( $child->nodeType() == XML_ELEMENT_NODE ) {
            print "\t", $child->nodeName(), ":", $child->textContent(), "\n";
        }
    }
}

will show,

将会呈现,

Sample
        Name:System1
        Type:IBM
        Memory:2GB
        StartTime:2012-04-26T14:30:01Z
        EndTime:2012-04-26T14:45:01Z
Sample
        Name:System2
        Type:Intel
        Disks:2
        StartTime:2012-04-26T15:30:01Z
        EndTime:2012-04-26T15:45:01Z
        Video:1