CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
XML File Parsing with Regex -- Need Help

 



vol7ron
New User

Aug 24, 2008, 3:43 PM

Post #1 of 3 (2551 views)
XML File Parsing with Regex -- Need Help Can't Post

Example XML:

Code
<class> 
<student firstName="Joe" lastName="Thomas" age="32" />
<student firstName="Bob" lastName="Villas" age="92" />
<student firstName="Don" lastName="Gaters" age="13" />
</class>


I only want to pull the attributes within. That is, firstName="", lastName="", and age="", whether there is text in the quotes, or not.

If each line of the XML is stored in @body.

Code
   foreach (@body) { 
do {
s/^\s+//; #remove leading spaces
s/\s+$//; #remove trailing spaces
if (/^(.+?)\=\"(.*?)\"/) {
print "${1}:${2} ";
}
$_ = $';
$rem = $';

} until ($rem eq "");
}


The above prints something like the following, which is almost perfect, except for that leading <student.

Code
<student firstName:Joe lastName:Thomas age:32 
<student firstName:Bob lastName:Villas age:92
<student firstName:Don lastName:Gaters age:13



vol7ron
New User

Aug 24, 2008, 4:00 PM

Post #2 of 3 (2548 views)
Re: [vol7ron] XML File Parsing with Regex -- Need Help [In reply to] Can't Post


Code
if (/(\w+?)\=\"(.+?)\"/) {  
...
}


I changed the If-Statement to use the \w (word descriptor) and removed the ^ (beginning stream mark).

I'm wondering if there is still a way using the (.+?) sequence.


KevinR
Veteran


Aug 24, 2008, 6:22 PM

Post #3 of 3 (2545 views)
Re: [vol7ron] XML File Parsing with Regex -- Need Help [In reply to] Can't Post


Code
@body = <DATA>; 
foreach (@body) {
s/^\s+//; #remove leading spaces
s/\s+$//; #remove trailing spaces
print "$1:$2\n" while (/\s([^=]+)="([^"]+)"/g);
}
__DATA__
<class>
<student firstName="Joe" lastName="Thomas" age="32" />
<student firstName="Bob" lastName="Villas" age="92" />
<student firstName="Don" lastName="Gaters" age="13" />
</class>

-------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives