Perl:XML Twig插入数据

时间:2018-05-29 17:50:41

标签: xml perl xml-twig

我有一个settings.xml文件

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
                      https://maven.apache.org/xsd/settings-1.0.0.xsd">

  <pluginGroups>
    <pluginGroup>org.mortbay.jetty</pluginGroup>
  </pluginGroups>
  <servers>
    <server>
      <id>server001</id>
      <username>my_login</username>
      <password>my_password</password>
      <privateKey>${user.home}/.ssh/id_dsa</privateKey>
      <passphrase>some_passphrase</passphrase>
      <filePermissions>664</filePermissions>
      <directoryPermissions>775</directoryPermissions>
      <configuration></configuration>
    </server>
  </servers>
</settings>

如图所示,没有代理数据。我想使用Perl:Twig添加数据。 我无法使用其他CPAN模块,因为我没有维护perl发行版。

我已阅读perl doc on twig,但我对插入过程感到困惑。我还阅读了this perl linkthis SO link,其中使用了XML的比较/复制(这里不相关,因为我不是在比较)。我也读了SO twig child adding,但我想我想创建一个标签。这个So link explains如何使用XML :: DOM,但我仅限于Perl:Twig,最后是this SO on XML:Simple,但我再次限于Perl:Twig,加上perldoc状态不使用XML:简单

我目前的代码与

类似
my $t = XML::Twig->new(
    twig_roots   => { proxy => 1 },
    pretty_print => 'indented',
)->parsefile( $maven_Dir . "/" . $settings_File );

if ( !$t->root->children('proxy') ) {
    print(
        "In your settings.xml located in your .m2 folder, 
         there appears to be no proxy settings \n"
    );
    print("Adding Jlab proxy settings \n");
    my $cp = $t->root->children('settings');
    $cp->insert("proxies");

#       $cp->first_child('proxy');
#       $cp->paste( 'last_child', $t->root );

}
else {

    foreach my $proxy ( $t->root->children('proxy') ) {
        my $port     = $proxy->first_child_text('port');
        my $host     = $proxy->first_child_text('host');
        my $protocol = $proxy->first_child_text('protocol');
        my $active   = $proxy->first_child_text('active');

        if (   $port == 1492
            && $host eq "jpoop"
            && $protocol eq "http"
            && $active eq "true" )
        {
            print("Correct proxy settings are set properly for Jlab \n");
            print( "$port \n", "$host \n", "$protocol \n",
                "$active'\n \n" );

        }

    }
}

有人会指出我添加类似于

的数据的方向
<proxies>
    <proxy>
        <active>true</active>
        <protocol>http</protocol>
        <host>jpoop</host>
        <port>1492</port>
    </proxy>
</proxies>

使用Perl:Twig?

进入XML文件

1 个答案:

答案 0 :(得分:2)

XML :: Twig不是编辑XML的最佳选择,但它可以完成:

#!/usr/bin/perl
use warnings;
use strict;
use feature qw{ say };

use XML::Twig;

my ($PORT, $HOST, $PROTOCOL, $ACTIVE) = (1492, 'jpoop', 'http', 'true');

my $twig = 'XML::Twig'->new(
    pretty_print => 'indented',
)->parsefile(shift);

my $root = $twig->root;
my ($proxies) = $root->children('proxies');

if (! $proxies) {
    print STDERR
        "In your settings.xml located in your .m2 folder,
         there appears to be no proxy settings.
         Adding Jlab proxy settings \n";

    my $proxies = $root->insert_new_elt('proxies');
    $proxies->set_inner_xml(
        "<proxy><active>$ACTIVE</active><protocol>$PROTOCOL</protocol>"
        . "<host>$HOST</host><port>$PORT</port></proxy>");
    $root->print;

} else {
    my $found;
    for my $proxy ( $proxies->children('proxy') ) {
        my $port     = $proxy->first_child_text('port');
        my $host     = $proxy->first_child_text('host');
        my $protocol = $proxy->first_child_text('protocol');
        my $active   = $proxy->first_child_text('active');

        $found = 1 if $port == $PORT
                   && $host eq $HOST
                   && $protocol eq $PROTOCOL
                   && $active eq $ACTIVE;
    }
    if ($found) {
        print STDERR "Correct proxy settings are set properly for Jlab \n";
    } else {
        print STDERR "Correct proxy settings missing:\n",
            "port $PORT\nhost $HOST\nprotocol $PROTOCOL\nactive $ACTIVE\n";

    }
}