如何使用Perl从目录中的多个XML文件创建一个XML文件 [英] How to create one XML file from multiple XML files in a directory, using Perl

查看:82
本文介绍了如何使用Perl从目录中的多个XML文件创建一个XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
如果有任何语法错误,请原谅.
我在一个目录中有多个xml文件,我需要在一个XML文件中创建多个XML文件.像这样的示例文件

Hi every one,
Please excuse me if any grammatical mistakes is there.
I have multiple xml files in one directory, I need to create multiple XML files into one XML file.example files like this

  file1:bvr.xml

<?xml version="1.0" encoding="UTF-8"?>
<specification xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <details>
 <name>johan</name>
 <address>Langgt 23</address>
  ---more info---
 </details>
</specification>

 file2:kvr.xml

 <?xml version="1.0" encoding="UTF-8"?>
 <specification xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <details>
 <name>venu</name>
 ---more info---
 </details>
 </specification>

   file3:svr.xml

 <?xml version="1.0" encoding="UTF-8"?>
 <specification xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <details>
 <name>kent</name>
 ---more info----
  </details>
  </specification>

  file4:tvr.xml

 <?xml version="1.0" encoding="UTF-8"?>
 <specification xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <definition>
 <name>kent</name>
  ----more info---
  </definition>
  </specification>

我需要这样创建一个xml文件.

I need to create one xml file like this.

       new.xml 

<?xml version="1.0" encoding="UTF-8"?>
<specification xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <details>
 <name>johan</name>
 <address>Langgt 23</address>
  --more info--
 </details>

 <details>
 <name>venu</name>
 ---more info----
 </details>

 <details>
 <name>kent</name>
 <address>vadrss 25</address>
 ---more info--
  </details>

   <definition>
 <name>kent</name>
  ----more info----
  </definition>
</specification>

我曾经这样尝试过,但是这个脚本有一些问题.

I tried like this But I have some problems with this script.

#!/usr/bin/perl
  use warnings;
  use strict;
 use File::Find;
   use XML::LibXML::Reader;
 use Data::Dumper;
  my $Number;

  my $dir="C:/file/sav";
  find(\&wanted, $dir);
  sub wanted() {
       if ( -f and /(\.xml)$/) {# find all the files with a suffix of .xml

  my $reader = XML::LibXML::Reader->new( location =>$_ )
 or die "cannot read file.xml\n";

    while ($reader->nextElement( 'details' ) ){

          $Number = $reader->readOuterXml();

          print "$Number\n";    
             } 

         }
   return;                   
  }

但是这个脚本有两个问题

but I have two problem in this script

1)我正在从所有具有细节"节点元素的XML文件中提取信息,但是在一个XML文件中,我具有与其他节点元素定义"相同的数据,我没有提取该信息,如果我想怎么办提取该信息并将其存储在同一变量中.

1) I am extracting information from all XML files Having "details" Node element, But in one XML file I have data with some other Node element "definition" I am not extracting that information, What should I do if I want to extract that information and store in the same variable.

2)提取存储在$Number变量中的所有信息之后,我想将该$Number变量信息存储在一个XML文件中,我该怎么做.请帮助我.我对perl还是很陌生.

2) After extracting all information That is stored in a $Number variable, I want to store that $Number variable information in one XML file how can I do that one. Please help me.I am very new to perl.

                    (or)

还有其他方法可以将所有XML文件数据组合到一个XML文件中.

Is there any other ways to combined all XML files data into one XML file.

尝试了您所说的代码后,它给出了更多错误

After trying your code as you said it gives some more errors

#!/usr/bin/perl
use warnings;
use strict;
  use XML::LibXML;
my @xml_files = glob '*.xml';
my $bigXML = XML::LibXML::Document->new( '1.0', 'UTF-8');
my $aggregated; for my $xml_file ( @xml_files ) {

my $doc = XML::LibXML->new->parse_file( $xml_file );

my ( $specNode ) = $doc->findnodes( '//Specification' );

  if ( $aggregated ) {
 $aggregated = $specNode;
 }
 else {
 my @details = $specNode->findnodes( './details' );
 $aggregated->addChild( $_ ) foreach @details;
 }
  }
  $bigXML->adoptNode( $aggregated ); 
 $bigXML->toFile( 'aggregated_data.xml' );

出现与我在can't call method "findnodes" on an undefined value之前的要求相同的错误,我尝试在详细信息行中用斜杠替换点,也给出了相同的错误.

it giving same error as I asked before can't call method "findnodes" on an undefined value, I tried to replce dot with slash in details line also it giving same error.

如果尝试使用像这样的golb函数

and if tried to use golb function like this

my @xml_files = glob '*dtc.xml';

这次在上面的代码中,它给出了类似

in the above code this time it gives different error like

XML::LibXML::Document::adoptNode()-- Node is not a blessed SV reference at line 21

我正在为这些错误而苦苦挣扎,因为这对我来说是一个非常新的模块,并且我查看了该文档,所有内容的格式都正确,但是我无法理解这些错误.我是这个perl的初学者,可以帮助我.

I am struggling with these errors because this is very new module for me and also I looked into the that document , every thing is in right format but I cant understand this errors. I am very beginner to this perl , can help me.

推荐答案

首先,感谢您在这里发表您的努力并说明最终要做什么,即将所有XML合并到一个文件中.

First of all, thank you for posting your effort here and for stating what you ultimately want to do, which is combine all XMLs into a single file.

要解决问题的需求:

  1. use strict; use warnings;

捕捉愚蠢的错误并实施良好的错误处理

To catch silly mistakes and enforce good error handling

use XML::LibXML;

使用解析器

my @xml_files = glob '*.xml';

找到目录中的所有XML文件

Find all XML files in your directory

my $bigXML = XML::LibXML::Document->new( '1.0', 'UTF-8');

实例化将存储所有节点的大型XML.

Instantiate big XML where all nodes will be stored.

遍历每个文件,获取节点并将其推送到聚合节点

Loop over each file, get the node and push the node into an aggregated node

我的$总计; 为我的$ xml_file(@xml_files){

my $aggregated; for my $xml_file ( @xml_files ) {

my $doc = XML::LibXML->new->parse_file( $xml_file );

my ( $specNode ) = $doc->findnodes( '//specification' );

if ( ! $aggregated ) {        # Initialize if doesn't exist

    $aggregated = $specNode;
}

else {                        # Add more <details>

    my @details = $specNode->findnodes( './details' );
    $aggregated->addChild( $_ ) foreach @details;
}

}

$aggregated节点添加到$bigXML文档中并打印:

Add the $aggregated node to the $bigXML document and print:

$bigXML->addChild( $aggregated ); $bigXML->toFile( 'aggregated_data.xml' );

$bigXML->addChild( $aggregated ); $bigXML->toFile( 'aggregated_data.xml' );

这篇关于如何使用Perl从目录中的多个XML文件创建一个XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆