Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Parsing an XML file and adding another tag, if the tag is not available / the value is null

Reply
Thread Tools

Parsing an XML file and adding another tag, if the tag is not available / the value is null

 
 
P
Guest
Posts: n/a
 
      01-10-2007
Hi

I have a scenario in which we are adding a new tag to the existing
structure of the XML files, some of the new files created might have
the newly added tag with value / just an empty tag, all I need is to
add the new tag if it is not present with the value available in
another tag, or if it is empty have to give the value from the other
tag to this.
For Example:
Old XML file:
<employee>
<personal_details>
<fname>Annbu</fname>
<lname>P</lname>
<empid>7655</empid>
</personal_details>
......
</employee>

New XML file:
<employee>
<personal_details>
<fname>Annbu</fname>
<lname>P</lname>
<nickname>Annbu</nickname>
<empid>7655</empid>
</personal_details>
......
</employee>

in the new file we have added a new field/tag called <nickname> - for
the existing XML files it has to take the value present in the <fname>
tag, (if the tag is empty we need to add the value, else have to add
the new tag with the value).

Kindly let me know, how to do it with perl, I am new to perl.

thank you.

Regards,
Annbu P

 
Reply With Quote
 
 
 
 
John Bokma
Guest
Posts: n/a
 
      01-10-2007
"P" <(E-Mail Removed)> wrote:

> Kindly let me know, how to do it with perl, I am new to perl.


Personally I would say use XSLT for stuff like this. But maybe XML::Simple
can solve your problem the Perl way.

--
John Experienced Perl programmer: http://castleamber.com/

Perl help, tutorials, and examples: http://johnbokma.com/perl/
 
Reply With Quote
 
 
 
 
P
Guest
Posts: n/a
 
      01-10-2007
Thanks John,

I can't use the XSLT because, we just need to insert the text / the new
tag with the value in the same file, sine there are so many XPATH
expressions in other files that refer to these files.

can you let me know if there is any sample code, that could help me.

Thanks again.

Regards,
Annbu P

John Bokma wrote:
> "P" <(E-Mail Removed)> wrote:
>
> > Kindly let me know, how to do it with perl, I am new to perl.

>
> Personally I would say use XSLT for stuff like this. But maybe XML::Simple
> can solve your problem the Perl way.
>
> --
> John Experienced Perl programmer: http://castleamber.com/
>
> Perl help, tutorials, and examples: http://johnbokma.com/perl/


 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      01-10-2007
"P" <(E-Mail Removed)> wrote:

> Thanks John,
>
> I can't use the XSLT because, we just need to insert the text / the
> new tag with the value in the same file,


Uhm, you can transform the current one to a new file, and then copy it
over the old one. Doesn't sound impossible to me.

> can you let me know if there is any sample code, that could help me.


http://search.cpan.org/~grantm/XML-S.../XML/Simple.pm

has examples.

Other option might be:
XML:OM::Lite
http://search.cpan.org/~rhundt/XML-D...ML/DOM/Lite.pm


BTW: please don't top post (if you don't know what that means, google for
it).

--
John Experienced Perl programmer: http://castleamber.com/

Perl help, tutorials, and examples: http://johnbokma.com/perl/
 
Reply With Quote
 
mirod
Guest
Posts: n/a
 
      01-10-2007
On Jan 10, 10:18 am, "P" <(E-Mail Removed)> wrote:

> I have a scenario in which we are adding a new tag to the existing
> structure of the XML files, some of the new files created might have
> the newly added tag with value / just an empty tag, all I need is to
> add the new tag if it is not present with the value available in
> another tag, or if it is empty have to give the value from the other
> tag to this.


Using XML::Twig here is how you would do it:

#!/usr/bin/perl

use strict;
use warnings;

use XML::Twig;

XML::Twig->new( twig_roots => { personal_details => \&add_nick },
twig_print_outside_roots => 1,
keep_spaces => 1,
)
->parsefile( "employee.xml");

sub add_nick
{ my( $t, $details)= @_;
my $fname = $details->first_child( 'fname');
my $nick = $details->first_child( 'nickname');

if( !$nick)
{ $nick = $fname->insert_new_elt( after => nickname =>
$fname->text); }
elsif( $nick->text=~ m{^\s*$})
{ $nick->set_text( $fname->text); }

$t->flush; # output the details and frees the memory
}

A few words of explanation:
twig_roots triggers the associated sub every time a
personal_details element is found,
twig_print_outside_roots prints everything outside of the
personal_details elements
keep_spaces preserves the non-significant spaces in the
input
flush outputs the fragment of the tree
built so far, and frees the memory

OTH

--
mirod

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      01-10-2007
mirod <(E-Mail Removed)> wrote:
> On Jan 10, 10:18 am, "P" <(E-Mail Removed)> wrote:
>
>> I have a scenario in which we are adding a new tag to the existing
>> structure of the XML files, some of the new files created might have
>> the newly added tag with value / just an empty tag, all I need is to
>> add the new tag if it is not present with the value available in
>> another tag, or if it is empty have to give the value from the other
>> tag to this.

>
> Using XML::Twig here is how you would do it:



[ snip robust code that any sensible person would prefer. ]


Or if the OP is willing to live with a fragile dirty hack, then:

$xml =~ s{(.*<fname>.*?</fname>\n)(.*\n)}
{ $a = $1 . $2;
($b=$1) =~ s/fname/nickname/g;
$a . $b;
}gie;


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
P
Guest
Posts: n/a
 
      01-11-2007
Thanks Guys, for taking your time out to help me out.

I still have an issue, I can't use XML::Twig, it is not provided with
the version of the UNIX we use here, can you give me some other way
similar to this, with other older packages.

Also this needs to be done for all the XML files in a folder & sub
folders.

Just let me know your thoughts.

Thank you,

Regards,
Annbu P

Tad McClellan wrote:
> mirod <(E-Mail Removed)> wrote:
> > On Jan 10, 10:18 am, "P" <(E-Mail Removed)> wrote:
> >
> >> I have a scenario in which we are adding a new tag to the existing
> >> structure of the XML files, some of the new files created might have
> >> the newly added tag with value / just an empty tag, all I need is to
> >> add the new tag if it is not present with the value available in
> >> another tag, or if it is empty have to give the value from the other
> >> tag to this.

> >
> > Using XML::Twig here is how you would do it:

>
>
> [ snip robust code that any sensible person would prefer. ]
>
>
> Or if the OP is willing to live with a fragile dirty hack, then:
>
> $xml =~ s{(.*<fname>.*?</fname>\n)(.*\n)}
> { $a = $1 . $2;
> ($b=$1) =~ s/fname/nickname/g;
> $a . $b;
> }gie;
>
>
> --
> Tad McClellan SGML consulting
> (E-Mail Removed) Perl programming
> Fort Worth, Texas


 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      01-12-2007
P <(E-Mail Removed)> wrote:

> I still have an issue, I can't use XML::Twig, it is not provided with

^^^^^^^^^^^^
> the version of the UNIX we use here,



Then install it!


[ snip TOFU, please do not top-post ]

--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
file reader returning null when file is not null content jason Java 13 05-14-2010 01:50 AM
"stringObj == null" vs "stringObj.equals(null)", for null check?? qazmlp1209@rediffmail.com Java 5 03-29-2006 10:37 PM
Different results parsing a XML file with XML::Simple (XML::Sax vs. XML::Parser) Erik Wasser Perl Misc 5 03-05-2006 10:09 PM
only a simple xml reader <tag:id>value</tag:id> martijn@gamecreators.nl Python 4 02-11-2006 06:44 PM
how do u invoke Tag b's Tag Handler from within Tag a's tag Handler? shruds Java 1 01-27-2006 03:00 AM



Advertisments