How can I use Perl regular expressions to parse XML data?
Posted
by Luke
on Stack Overflow
See other posts from Stack Overflow
or by Luke
Published on 2010-06-01T14:36:43Z
Indexed on
2010/06/01
14:53 UTC
Read the original article
Hit count: 364
I have a pretty long piece of XML that I want to parse. I want to remove everything except for the subclass-code and city. So that I am left with something like the example below.
EXAMPLE
TEST SUBCLASS|MIAMI
CODE
<?xml version="1.0" standalone="no"?>
<web-export>
<run-date>06/01/2010
<pub-code>TEST
<ad-type>TEST
<cat-code>Real Estate</cat-code>
<class-code>TEST</class-code>
<subclass-code>TEST SUBCLASS</subclass-code>
<placement-description></placement-description>
<position-description>Town House</position-description>
<subclass3-code></subclass3-code>
<subclass4-code></subclass4-code>
<ad-number>0000284708-01</ad-number>
<start-date>05/28/2010</start-date>
<end-date>06/09/2010</end-date>
<line-count>6</line-count>
<run-count>13</run-count>
<customer-type>Private Party</customer-type>
<account-number>100099237</account-number>
<account-name>DOE, JOHN</account-name>
<addr-1>207 CLARENCE STREET</addr-1>
<addr-2> </addr-2>
<city>MIAMI</city>
<state>FL</state>
<postal-code>02910</postal-code>
<country>USA</country>
<phone-number>4014612880</phone-number>
<fax-number></fax-number>
<url-addr> </url-addr>
<email-addr>[email protected]</email-addr>
<pay-flag>N</pay-flag>
<ad-description>DEANESTATES2BEDS2BATHSAPPLIANCED</ad-description>
<order-source>Import</order-source>
<order-status>Live</order-status>
<payor-acct>100099237</payor-acct>
<agency-flag>N</agency-flag>
<rate-note></rate-note>
<ad-content> MIAMI/Dean Estates: 2
beds, 2 baths. Applianced. Central air. Carpets. Laundry. 2 decks. Pool. Parking. Close to everything.No smoking. No utilities. $1275 mo. 401-578-1501. </ad-content>
</ad-type>
</pub-code>
</run-date>
</web-export>
PERL
So what I want to do is open an existing file read the contents then use regular expressions to eliminate the unnecessary XML tags.
open(READFILE, "FILENAME");
while(<READFILE>)
{
$_ =~ s/<\?xml version="(.*)" standalone="(.*)"\?>\n.*//g;
$_ =~ s/<subclass-code>//g;
$_ =~ s/<\/subclass-code>\n.*/|/g;
$_ =~ s/(.*)PJ RER Houses /PJ RER Houses/g;
$_ =~ s/\G //g;
$_ =~ s/<city>//g;
$_ =~ s/<\/city>\n.*//g;
$_ =~ s/<(\/?)web-export>(.*)\n.*//g;
$_ =~ s/<(\/?)run-date>(.*)\n.*//g;
$_ =~ s/<(\/?)pub-code>(.*)\n.*//g;
$_ =~ s/<(\/?)ad-type>(.*)\n.*//g;
$_ =~ s/<(\/?)cat-code>(.*)<(\/?)cat-code>\n.*//g;
$_ =~ s/<(\/?)class-code>(.*)<(\/?)class-code>\n.*//g;
$_ =~ s/<(\/?)placement-description>(.*)<(\/?)placement-description>\n.*//g;
$_ =~ s/<(\/?)position-description>(.*)<(\/?)position-description>\n.*//g;
$_ =~ s/<(\/?)subclass3-code>(.*)<(\/?)subclass3-code>\n.*//g;
$_ =~ s/<(\/?)subclass4-code>(.*)<(\/?)subclass4-code>\n.*//g;
$_ =~ s/<(\/?)ad-number>(.*)<(\/?)ad-number>\n.*//g;
$_ =~ s/<(\/?)start-date>(.*)<(\/?)start-date>\n.*//g;
$_ =~ s/<(\/?)end-date>(.*)<(\/?)end-date>\n.*//g;
$_ =~ s/<(\/?)line-count>(.*)<(\/?)line-count>\n.*//g;
$_ =~ s/<(\/?)run-count>(.*)<(\/?)run-count>\n.*//g;
$_ =~ s/<(\/?)customer-type>(.*)<(\/?)customer-type>\n.*//g;
$_ =~ s/<(\/?)account-number>(.*)<(\/?)account-number>\n.*//g;
$_ =~ s/<(\/?)account-name>(.*)<(\/?)account-name>\n.*//g;
$_ =~ s/<(\/?)addr-1>(.*)<(\/?)addr-1>\n.*//g;
$_ =~ s/<(\/?)addr-2>(.*)<(\/?)addr-2>\n.*//g;
$_ =~ s/<(\/?)state>(.*)<(\/?)state>\n.*//g;
$_ =~ s/<(\/?)postal-code>(.*)<(\/?)postal-code>\n.*//g;
$_ =~ s/<(\/?)country>(.*)<(\/?)country>\n.*//g;
$_ =~ s/<(\/?)phone-number>(.*)<(\/?)phone-number>\n.*//g;
$_ =~ s/<(\/?)fax-number>(.*)<(\/?)fax-number>\n.*//g;
$_ =~ s/<(\/?)url-addr>(.*)<(\/?)url-addr>\n.*//g;
$_ =~ s/<(\/?)email-addr>(.*)<(\/?)email-addr>\n.*//g;
$_ =~ s/<(\/?)pay-flag>(.*)<(\/?)pay-flag>\n.*//g;
$_ =~ s/<(\/?)ad-description>(.*)<(\/?)ad-description>\n.*//g;
$_ =~ s/<(\/?)order-source>(.*)<(\/?)order-source>\n.*//g;
$_ =~ s/<(\/?)order-status>(.*)<(\/?)order-status>\n.*//g;
$_ =~ s/<(\/?)payor-acct>(.*)<(\/?)payor-acct>\n.*//g;
$_ =~ s/<(\/?)agency-flag>(.*)<(\/?)agency-flag>\n.*//g;
$_ =~ s/<(\/?)rate-note>(.*)<(\/?)rate-note>\n.*//g;
$_ =~ s/<ad-content>(.*)\n.*//g;
$_ =~ s/\t(.*)\n.*//g;
$_ =~ s/<\/ad-content>(.*)\n.*//g;
}
close( READFILE1 );
Is there an easier way of doing this? I don't want to use any modules. I know that it might make this easier but the file I am reading has a lot of data in it.
© Stack Overflow or respective owner