How to modify a XML file using Perl?

Y

yann.pambou

Hi,

I'm trying to update an XML file using Perl, but he does not work:

I'm trying to extract the value of the last tag and store it in the
first tag. See below:
From:
<TroubleTicket>
<TroubleID>12345</TroubleID>
<Descr>No SYNCH</Descr>
<SwitchID>HDLOPER</SwitchID>
</TroubleTicket>

To:
<TroubleTicket>
<TroubleID>HDLOPER</TroubleID>
<Descr>No SYNCH</Descr>
<SwitchID>HDLOPER</SwitchID>
</TroubleTicket>

Here are the syntaxes i'm using:

my(@records) = $xp->findnodes( 'TroubleTicket' );

foreach my $record ( @records ) {
$record-> find('SwitchID')-> string_value = $record->
findvalue('TroubleID');
}

Anyone would have any idea about how to modify a node value? Thanks,

Yann
 
U

usenet

I'm trying to extract the value of the last tag and store it in the
first tag. See below:
<snip>

Well, in the time-honored usenet tradition of not answering more than
was asked:

use strict; use warnings;
use XML::Simple;

my $ticket = XMLin(\*DATA);
$$ticket{'TroubleID'} = $$ticket{'SwitchID'};

use Data::Dumper; #to verify the effects
print Dumper($ticket);

__DATA__
<TroubleTicket>
<TroubleID>12345</TroubleID>
<Descr>No SYNCH</Descr>
<SwitchID>HDLOPER</SwitchID>
</TroubleTicket>


A better question might have yeilded a better answer. I'm unable to
really determine if this answer meets the OP's requirements, but it
answers the question.
 
Y

yann.pambou

Well, in the time-honored usenet tradition of not answering more than
was asked:

use strict; use warnings;
use XML::Simple;

my $ticket = XMLin(\*DATA);
$$ticket{'TroubleID'} = $$ticket{'SwitchID'};

use Data::Dumper; #to verify the effects
print Dumper($ticket);

__DATA__
<TroubleTicket>
<TroubleID>12345</TroubleID>
<Descr>No SYNCH</Descr>
<SwitchID>HDLOPER</SwitchID>
</TroubleTicket>

Thanks David for replying!

I have a big XML file (50MB) that i'm processing with Perl and the
package XML::XPath which looks like this:

<TroubleTicket>
<TroubleID>12345</TroubleID>
<Descr>No SYNCH</Descr>
<SwitchID>HDLOPER</SwitchID>
</TroubleTicket>
<TroubleTicket>
<TroubleID>33849</TroubleID>
<Descr>Interface Down</Descr>
<SwitchID>HDLIC</SwitchID>
</TroubleTicket>
<TroubleTicket>
<TroubleID>393947</TroubleID>
<Descr>Fan Failure</Descr>
<SwitchID>DPRLFO</SwitchID>
</TroubleTicket>

For each trouble ticket, i would like to be able to change modify the
TroubleID with the value of the Switch ID. I tried the following
method:
$record->XML::XPath -> setNodeText('SWITCH', $TT);

Would anyone know why it is not working? And is there a better way of
doing it?

Thanks all for your help

BTW: I'm new at Perl, so sorry if my questions are not precise enough!

Thanks,

Yann
 
U

usenet

I have a big XML file (50MB) that i'm processing with Perl and the
package XML::XPath which looks like this:
<snip>
Is there a reason why you're using this particular module? It seems
like a poor fit. Are you trying to interface with XLST or something?
Is there a better way of doing it?

I would suggest a different parser. The approach that I proposed in an
earlier response is still valid, but needs to be tweaked to consider
that we're processing multiple tickets (and the XML must be wrapped in
some higher-level tagset - I called it "Stuff").

Consider something like this:

#!/usr/bin/perl
use strict; use warnings;
use XML::Simple;

my $tickets = XMLin(\*DATA);

foreach my $ticket_ref ( @{${$tickets}{'TroubleTicket'}} ) {
${$ticket_ref}{'TroubleID'} = ${$ticket_ref}{'SwitchID'};
}

use Data::Dumper; #to validate the effects
print Dumper($tickets);

__DATA__
<Stuff>
<TroubleTicket>
<TroubleID>12345</TroubleID>
<Descr>No SYNCH</Descr>
<SwitchID>HDLOPER</SwitchID>
</TroubleTicket>
<TroubleTicket>
<TroubleID>33849</TroubleID>
<Descr>Interface Down</Descr>
<SwitchID>HDLIC</SwitchID>
</TroubleTicket>
<TroubleTicket>
<TroubleID>393947</TroubleID>
<Descr>Fan Failure</Descr>
<SwitchID>DPRLFO</SwitchID>
</TroubleTicket>
</Stuff>
 
R

robic0

Hi,

I'm trying to update an XML file using Perl, but he does not work:

I'm trying to extract the value of the last tag and store it in the
first tag. See below:
From:
<TroubleTicket>
<TroubleID>12345</TroubleID>
<Descr>No SYNCH</Descr>
<SwitchID>HDLOPER</SwitchID>
</TroubleTicket>

To:
<TroubleTicket>
<TroubleID>HDLOPER</TroubleID>
<Descr>No SYNCH</Descr>
<SwitchID>HDLOPER</SwitchID>
</TroubleTicket>

Here are the syntaxes i'm using:

my(@records) = $xp->findnodes( 'TroubleTicket' );

foreach my $record ( @records ) {
$record-> find('SwitchID')-> string_value = $record->
findvalue('TroubleID');
}

Anyone would have any idea about how to modify a node value? Thanks,

Yann

<TroubleTicket><TroubleID>HDLOPER</TroubleID><Descr>No SYNCH</Descr>
<SwitchID>HDLOPER</SwitchID></TroubleTicket>

I messed up my originam elegant worded reply (thanks google) so now its
just
down and dirty.

In lieu of Dom or Sax and Xerces, if this is just a fairly constant
named tags you
could always just rename the tags (without too much fear):

while (<XMLIN>) {
s/TroubleID/TIDold/ig;
s/SwitchID/TroubleID /ig;
print (XMLOUT, $_);
}
 
U

usenet

you could always just rename the tags (without too much fear):

while (<XMLIN>) {
s/TroubleID/TIDold/ig;
s/SwitchID/TroubleID /ig;
print (XMLOUT, $_);
}

That's not what the OP wanted to do. The idea was to duplicate the
value of the SwtichID tag (although I can see how the name of the tag
may suggest the solution that robic0 proposed, although the solution
doesn't close the circle on the switch).
 
R

robic0

That's not what the OP wanted to do. The idea was to duplicate the
value of the SwtichID tag (although I can see how the name of the tag
may suggest the solution that robic0 proposed, although the solution
doesn't close the circle on the switch).

However the OP left out alot of things. He seemed more interrested
in tagging certain data in the block than anything else.
Otherwise, wouldn't the idea of overwriting a tags data be a little
wrong?
 
R

robic0

Purl Gurl wrote:
[-snip-]
odd (plus five) even (plus five) odd (plus five) even ...

TroubleID lines will have a line count number ending in 1 or 6 always.
SwitchID lines will have a line count number ending in 3 or 8 always.
Difference between key lines is 5 always.

A psuedo matrix appears like so,

TroubleID -> 1 6 11 16 21 26 ...
SwitchID -> 3 8 13 18 23 28 ...
Difference -> 5 5 5 5 5 ...
What if the xml is not so nice:
<TroubleTicket>
<TroubleID>HDLOPER</TroubleID><Descr>No
SYNCH</Descr><SwitchID>HDLOPER</SwitchID></TroubleTicket>
 
R

robic0

However the OP left out alot of things. He seemed more interrested
in tagging certain data in the block than anything else.
Otherwise, wouldn't the idea of overwriting a tags data be a little
wrong?
Although its a little taxing in time and energy, and its all just
mechanics, I could "close the switch", but not unless the OP discloses
the valid reasons he wants to do this and/or the weight of the other
tags and their data before and after the transformation.

The complete xml data file itself could be on a single line, won't
matter. hehe......
 
R

robic0

<snip>

Well, in the time-honored usenet tradition of not answering more than
was asked:

I thought that the Usenet tradition was to answer an entirely
different question, make a grammar/spelling flame, and watch the
thread turn to ash.

[snip]
hahahahaaaaaaaaaaaaaa
Funny as hell!!!!!!!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top