如何将根标记和根结束标记读取到 Perl 变量? [英] How to read root tag and root closing tag to a Perl variable?

查看:27
本文介绍了如何将根标记和根结束标记读取到 Perl 变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new to Perl. I want to read the xml root tag and root end tag to a perl variable.

我试过这种正常的文件读取.有效.我得到了第一行和最后一行.但有时如果没有新线,你就不能相信第一线.这样就完成了读取第一行的正则表达式.

I tried this normal file reading. It worked. I am getting fist line and last line. But some times you can't trust the fist line if there is no new line. So done with a regular expression for reading the first line.

但我在谷歌上搜索了一些内置的 Perl xml 函数来完成这项工作.我没有找到任何,即对我来说都是新的.

But I googled it for some built in Perl xml function to get this done. I did not find any, i.e all are new to me.

请告诉我哪个库最适合这个.如果可能的话,举一些例子.

Please let me know which library is best for this. Some example if possible.

例如:-

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

我想要 2 个变量,例如,

I want 2 variable like,

$root = '<nst:root arg='1' arg2='2' arg3='3'>';
$rootClose = '</nst:root>';

我想用其他 xml 替换这个根标签.请帮忙.

I want to replace this root tag with other xml. Please help.

这就是我想做的.我有一个 xml 文件,它有实际的根标签.我使用 XML::Twig::xml_split 将该文件拆分为多个文件.我收到很多文件,但标题不同.我想用主文件中的实际标题更新子文件

This is what I wanted to do. I have a xml file and that has actual root tag. I split that file in to multiple using XML::Twig::xml_split. I get many files but header is different. I want to update the child file with actual header from main file

EG:-

分割限制为 2

实际文件是,

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

它将使用 XML::Twig::xml_split 拆分为 3 个文件.和插件添加自己的标题.

It will split to, 3 files with XML::Twig::xml_split. And plugin add its own header.

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:-
<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>

推荐答案

我不知道如何使用 xml_split 程序,但这里有一个使用 XML::Twig<的方法/code> 模块,我在其中创建新元素并将每对子节点从一棵树移动到另一棵树:

I don't know how to use xml_split program, but here you have an approach using XML::Twig module, where I create new elements and move each pair of childs from one tree to another one:

#!/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' );
}

像这样运行:

perl script.pl xmlfile

这会为 root 的每对子节点及其标题生成一个文件:

That yields one file for each pair of childs of root with its header:

==> 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>

这篇关于如何将根标记和根结束标记读取到 Perl 变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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