Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl & XML...need help 3

Status
Not open for further replies.

newbie1006

Technical User
Jun 10, 2011
9
US
perl script:
use strict;
use warnings;

open FILE, "reg_sample.xml" or die $!;

while (my $line =<FILE>){

if($line= ~ m/<Register> (.*?) <\/Register>/g)
{
print $line;
}

}


I just realized that in the xml file, I see register in following format:
<Register>
XYZ_samp_011 </Register>

What changes should I make to the existing script to make it work? for the format shown above to recognize the tag <Register> and print out the information.
 
Hi

For now I would say, read the entire file in one piece and use the [tt]s[/tt] ( single line ) modifier :
Code:
[navy]$/[/navy][teal]=[/teal][b]undef[/b][teal];[/teal]
[b]open[/b] FILE[teal],[/teal] [green][i]"reg_sample.xml"[/i][/green] or [b]die[/b] $[teal]!;[/teal]
[navy]$xml[/navy] [teal]=[/teal] [green][i]<FILE>[/i][/green][teal];[/teal]
[b]close[/b] FILE[teal];[/teal]

[b]if[/b] [teal]([/teal][navy]$xml[/navy] [teal]=~[/teal] [b]m[/b][fuchsia]/<Register>(.*?)<\/Register>/[/fuchsia][b]s[/b][teal])[/teal] [teal]{[/teal]
  [b]print[/b] [navy]$1[/navy][teal];[/teal]
[teal]}[/teal]
But of course the real solution would be to use an XML parser module.


Feherke.
 
I want to print out the tag <Register> everytime it sees one in the entire xml file.
 
To process XML, use an actual XML Parser like XML::Twig:

Code:
use XML::Twig;

use strict;
use warnings;

my $file = "foo.xml";

my $t = XML::Twig->new();

# Instead of parsing the fake data, process the
# File instead when ready.
#$t->parsefile($file);
$t->parse(do {local $/; <DATA>});

for my $node ($t->findnodes(q{//Register})) {
	print $node->text(), "\n";
	
}

__DATA__
<root>
<Register>Bob</Register>
<blah>foo</blah>
<Register>
XYZ_samp_011 </Register>
</root>

- Miller
 
Hi

Just to show some minor beauties of the language's flexibility.
newbie1006 said:
I want to print out the tag <Register> everytime it sees one in the entire xml file.
Then you only had to change [tt]if[/tt] with [tt]while[/tt]. If you meant you want the tag itself too, not only the value, then also had to change [tt]$1[/tt] with [tt]$&[/tt].

But of course, you should definitely go with Miller's code.


Feherke.
 
so feherke,

$& when used with =~ returns the content between the match and the match itself?

$1 returns the just the content betwen the match?

it that right.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
Hi

After successful [tt]m//[/tt] it contains that portion of the string which would be removed by the [tt]s///[/tt] operator.

In case you not met it when using [tt]s///[/tt], [tt]$&[/tt] can be used there too :
Code:
[blue]  DB<1>[/blue] $s = 'asd5fgh'

[blue]  DB<2>[/blue] $s =~ s/.(\d)./<< got $1 inside $& >>/

[blue]  DB<3>[/blue] print $s
as<< got 5 inside d5f >>gh
By the way, it is available in some other languages too, however with some differences :
JavaScript:
re [teal]=[/teal] [fuchsia]/.(\d)./[/fuchsia]
[b]if[/b] [teal]([/teal]re[teal].[/teal][COLOR=darkgoldenrod]test[/color][teal]([/teal][green][i]'asd5fgh'[/i][/green][teal]))[/teal]
  [COLOR=darkgoldenrod]alert[/color][teal]([/teal]
    [green][i]'inside string : '[/i][/green] [teal]+[/teal] RegExp[teal].[/teal]input [teal]+[/teal] [green][i]'[/i][/green][lime][i]\n[/i][/lime][green][i]'[/i][/green] [teal]+[/teal]
    [green][i]'I captured : '[/i][/green] [teal]+[/teal] RegExp[teal].[/teal]$[purple]1[/purple] [teal]+[/teal] [green][i]'[/i][/green][lime][i]\n[/i][/lime][green][i]'[/i][/green] [teal]+[/teal]
    [green][i]'from matching substring : '[/i][/green] [teal]+[/teal] RegExp[teal][[/teal][green][i]'$&'[/i][/green][teal]][/teal]
  [teal])[/teal]

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top