
vol7ron
New User
Aug 24, 2008, 3:43 PM
Post #1 of 3
(1544 views)
|
|
XML File Parsing with Regex -- Need Help
|
Can't Post
|
|
Example XML:<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.
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.
<student firstName:Joe lastName:Thomas age:32 <student firstName:Bob lastName:Villas age:92 <student firstName:Don lastName:Gaters age:13
|