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!

How do I parse XML tag contents ?? 1

Status
Not open for further replies.

DR4296

Technical User
Jan 4, 2001
3
0
0
US
Greetings All !

I've always found pattern matching a bit hard to do / remember.

I'm trying to grab an error message out of an XML page and I'm not sure how to tell perl to... &quot;grab everything between the <ERROR> and </ERROR> tags&quot;.

From everything I've located using lookups at Google, I realize that this is not a good way to do it. I should probably use that XML::parser module. But I'm only dealing with one line of code here. I've already got lines in place to pull the &quot;success&quot; data I want from the XML pages (pricing data, actually). But I admit that I cut-and-pasted this from elsewhere and I don't really grasp the syntax.

Can anybody tell me how to &quot;grab everything&quot; between a set of XML tags ?

Thanks !

-= Dave Raasch =-
 
like this:[tt]

$data =~ m~<ERROR>(.*?)</ERROR>~m ;

[/tt]

it then assigns the part in parenthesis to the variable $1, which you can then play with as you see fit. note, the 'm' at the end means multi-line data, and isn't the same as the 'm' at the front which is just for match. if the error text only ever occurs on one single line with the error tags, you can remove the trailing 'm'.
also, this presupposes that the xml file has been read into a single string. for other data structures, the code would be alot different. &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
This is a great example and Im able to use it for the 1st error found in xml, but Im having trouble finding 1 or more.. Any suggestions????
 
A slight modification of stillflame's code
Code:
my @errors = $data =~ m~<ERROR>(.*?)</ERROR>~sg;
@errors = map { tr/\n/ /; $_ } @errors; # git rid of pesky newlines.
the /s forces '.*' to match newlines and the /g means to find all matches in the string. m//g used in list context returns a list of captured matches.

jaa
 
Does anyone know how I could parse this:
<SECT ID=&quot;31128&quot;></SECT>

My script is returning blanks when I do this:
I want to get the ID value.
thanks

foreach $snapshot (@XML_In_TASK_Phase1_messages)

{

@snapshot=$snapshot =~ m~<SECT ID=>(.*?)</SECT>>~sg;
print Debug &quot; @snapshot \n&quot;;

@CES_snap=(@CES_snap,@snapshot);

}
 
I tried this but still did not work

@snapshot=$snapshot =~ m~<SECT ID=&quot;(.*?)&quot;/>~sg;
 
try this:

m~<SECT ID=&quot;(.*?)&quot;></SECT>~sg

 
Hello, Jaa and others!

Based on the above thread, I worked out the following:

sub replacepattern {
foreach $source (@filesfound) {
undef @newfile;
open (FILE,&quot;<$source&quot;) || die &quot;Can't open $source: $!.&quot;;
@file = <FILE>;
close FILE;
foreach $line (@file) {
foreach $string (@tags) {
($start,$end) = split(' ',$string);
if ($line =~ m~$start(.*?)$end~sg) {
print &quot;innerpart: &quot;, $1, &quot;\n&quot;;
$line =~s/$1/NEW_STRING/g;
print &quot;whole line: &quot;, $line, &quot;\n&quot;;
}
}
push @newfile,$line;
}
open (FILE,&quot;>$source&quot;) || die &quot;Can't open $source: $!.&quot;;
print FILE @newfile;
close FILE;
}
}

Although I tried to work out the suggestions from Jaa and the others, the above works only for ONE match withing the SAME line. That is:
<starttag> string to be replaced<endtag>
...works ok.

However, this one:
<starttag> string1 replaced<endtag><starttag> string2 replaced<endtag>
...and this one:
<starttag> st
ring to
be repla
ced
<endtag>
...do not work. Only one match per line is replaced.

Any ideas how to fix this? Many thanks in advance for your ideas!

-brt



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top