Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.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
Xml 如何将根标记和根结束标记读取到Perl变量?_Xml_Perl - Fatal编程技术网

Xml 如何将根标记和根结束标记读取到Perl变量?

Xml 如何将根标记和根结束标记读取到Perl变量?,xml,perl,Xml,Perl,我是Perl新手。我想将xml根标记和根结束标记读入perl变量 我尝试了正常的文件读取。成功了。我得到了第一行和最后一行。但有时,如果没有新的路线,你就不能相信第一条路线。阅读第一行的正则表达式就是这样 但我在谷歌上搜索了一些内置的PerlXML函数来完成这项工作。我没有发现任何东西,即对我来说都是新的 请让我知道哪个图书馆最适合这个。如果可能的话,举个例子 例如: 它将拆分为3个文件,其中包含XML::Twig::XML\u split。插件添加自己的头 File1:- <xml_sp

我是Perl新手。我想将xml根标记和根结束标记读入perl变量

我尝试了正常的文件读取。成功了。我得到了第一行和最后一行。但有时,如果没有新的路线,你就不能相信第一条路线。阅读第一行的正则表达式就是这样

但我在谷歌上搜索了一些内置的PerlXML函数来完成这项工作。我没有发现任何东西,即对我来说都是新的

请让我知道哪个图书馆最适合这个。如果可能的话,举个例子

例如:

它将拆分为3个文件,其中包含
XML::Twig::XML\u split
。插件添加自己的头

File1:-
<xml_split:root xmlns:xml_split="http://xmltwig.com/xml_split">
           <a>1</a>
           <a>1</a>
</xml_split:root>

File2:-
<xml_split:root xmlns:xml_split="http://xmltwig.com/xml_split">
           <a>1</a>
           <a>1</a>
</xml_split:root>

File3:-
<xml_split:root xmlns:xml_split="http://xmltwig.com/xml_split">
           <a>1</a>
           <a>1</a>
</xml_split:root>
File1:-
1.
1.
文件2:-
1.
1.
文件3:-
1.
1.
我想要这样的

File1:-
<nst:root arg='1' arg2='2' arg3='3'>
           <a>1</a>
           <a>1</a>
</nst:root>

File2:-
<nst:root arg='1' arg2='2' arg3='3'>
           <a>1</a>
           <a>1</a>
</nst:root>

File3:-
<nst:root arg='1' arg2='2' arg3='3'>
           <a>1</a>
           <a>1</a>
</nst:root>
File1:-
1.
1.
文件2:-
1.
1.
文件3:-
1.
1.

我不知道如何使用
xml\u split
程序,但这里有一种使用
xml::Twig
模块的方法,我在其中创建新元素并将每对孩子从一棵树移动到另一棵树:

#!/usr/bin/env perl

use strict;
use warnings;
use XML::Twig;
use POSIX qw<ceil>;

my ($split_limit, $n) = (2, 0); 

my $twig = XML::Twig->new->parsefile( shift );
my $root = $twig->root;

for (  1 .. ceil( $root->children_count / $split_limit ) ) { 
    my $t = XML::Twig::Elt->new( $root->tag, $root->atts );
    for ( 1 .. $split_limit ) { 
        my $children = $root->first_child;
        last unless $children;
        $children->move( last_child => $t );
    }   
    $t->print_to_file( 'xmlfile-' . $n++ . '.xml' );
}
这将为根的每对子级及其头生成一个文件:

==> xmlfile-0.xml <==
<nst:root arg="1" arg2="2" arg3="3"><a>1</a><a>1</a></nst:root>
==> xmlfile-1.xml <==
<nst:root arg="1" arg2="2" arg3="3"><a>1</a><a>1</a></nst:root>
==> xmlfile-2.xml <==
<nst:root arg="1" arg2="2" arg3="3"><a>1</a><a>1</a></nst:root>

==>xmlfile-0.xml xmlfile-1.xml xmlfile-2.xml我不知道如何使用
xml\u split
程序,但这里有一种使用
xml::Twig
模块的方法,我在其中创建新元素,并将每一对child从一棵树移动到另一棵树:

#!/usr/bin/env perl

use strict;
use warnings;
use XML::Twig;
use POSIX qw<ceil>;

my ($split_limit, $n) = (2, 0); 

my $twig = XML::Twig->new->parsefile( shift );
my $root = $twig->root;

for (  1 .. ceil( $root->children_count / $split_limit ) ) { 
    my $t = XML::Twig::Elt->new( $root->tag, $root->atts );
    for ( 1 .. $split_limit ) { 
        my $children = $root->first_child;
        last unless $children;
        $children->move( last_child => $t );
    }   
    $t->print_to_file( 'xmlfile-' . $n++ . '.xml' );
}
这将为根的每对子级及其头生成一个文件:

==> xmlfile-0.xml <==
<nst:root arg="1" arg2="2" arg3="3"><a>1</a><a>1</a></nst:root>
==> xmlfile-1.xml <==
<nst:root arg="1" arg2="2" arg3="3"><a>1</a><a>1</a></nst:root>
==> xmlfile-2.xml <==
<nst:root arg="1" arg2="2" arg3="3"><a>1</a><a>1</a></nst:root>

==>xmlfile-0.xml xmlfile-1.xml xmlfile-2.xml@mirod您能帮我一下吗?为什么要在一个变量中使用开始标记,在另一个变量中使用结束标记?名称空间在哪里声明?看,这就是我想要做的。我有一个xml文件,它有实际的根标记。我使用
XML::Twig::XML\u split将该文件拆分为多个文件。我有很多文件,但标题是不同的。我想用主文件的实际标题更新子文件。@mirod你能帮我吗?为什么要在一个变量中使用开始标记,在另一个变量中使用结束标记?名称空间在哪里声明?看,这就是我想要做的。我有一个xml文件,它有实际的根标记。我使用
XML::Twig::XML\u split将该文件拆分为多个文件。我有很多文件,但标题是不同的。我想用主文件的实际头更新子文件。我想用原始文件替换子xml文件头。非子元素header@Sahal例如我不明白你的意思。根据您的输入提供示例输出数据。请检查示例部分谢谢,非常有用。最后一个帮助。我在原始档案中有9条记录。它创建了4个文件(split_limit=2),并忽略了最后一条记录。它应该创建5个文件,最后一个应该有一个记录。理解代码需要时间。对你来说这很容易。。工作得很有魅力。。非常感谢你。我希望子xml文件头应该替换为原始文件。非子元素header@Sahal例如我不明白你的意思。根据您的输入提供示例输出数据。请检查示例部分谢谢,非常有用。最后一个帮助。我在原始档案中有9条记录。它创建了4个文件(split_limit=2),并忽略了最后一条记录。它应该创建5个文件,最后一个应该有一个记录。理解代码需要时间。对你来说这很容易。。工作得很有魅力。。非常感谢你。你让我开心。
perl script.pl xmlfile
==> xmlfile-0.xml <==
<nst:root arg="1" arg2="2" arg3="3"><a>1</a><a>1</a></nst:root>
==> xmlfile-1.xml <==
<nst:root arg="1" arg2="2" arg3="3"><a>1</a><a>1</a></nst:root>
==> xmlfile-2.xml <==
<nst:root arg="1" arg2="2" arg3="3"><a>1</a><a>1</a></nst:root>