L
Leif Wessman
Hi!
I'm trying to parse some xml with a regular expression (yes, i know
that there is several XML modules that I can use).
My problem is that I'm not that good in creating regular expressions.
The following code does not work as expected. I have a list of items in
xml. Each item has an id and an optional name (no <name>-tag or
<name/>). Each item can also have other tags that I'm not interested
in.
I'm trying to parse this simle xml document so that I extract the id
for each item and the name (if it's there).
However, the output of my program only displays the id:s, not any name.
That's my first problem. My second problem is that I would like to know
if it's possible to make my code more efficient (faster and using less
memory). In reality my xml-file can be quite large.
My code:
--------
#!/usr/bin/perl
use strict;
use warnings;
open (XML, "<items.xml") or die "open: $!";
my $xml;
while(my $line = <XML>) {
$xml = $xml . $line;
}
while ($xml =~
/<item>.*?<id>(.*?)<\/id>.*?(<name>(.*?)<\/name>)?.*?<\/item>/gs) {
print "id : $1\n";
if ($3) {
print "name: $3\n";
}
}
My xml-document:
----------------
<xml>
<item>
<id>mf3</id>
<color>blue</color>
<name>moto F3</name>
</item>
<item>
<id>nk1</id>
</item>
<item>
<id>jk8</id>
<name/>
</item>
<item>
<id>la2</id>
<name>labo 2</name>
</item>
<xml>
My output:
----------
id : mf3
id : nk1
id : jk8
id : la2
Leif
I'm trying to parse some xml with a regular expression (yes, i know
that there is several XML modules that I can use).
My problem is that I'm not that good in creating regular expressions.
The following code does not work as expected. I have a list of items in
xml. Each item has an id and an optional name (no <name>-tag or
<name/>). Each item can also have other tags that I'm not interested
in.
I'm trying to parse this simle xml document so that I extract the id
for each item and the name (if it's there).
However, the output of my program only displays the id:s, not any name.
That's my first problem. My second problem is that I would like to know
if it's possible to make my code more efficient (faster and using less
memory). In reality my xml-file can be quite large.
My code:
--------
#!/usr/bin/perl
use strict;
use warnings;
open (XML, "<items.xml") or die "open: $!";
my $xml;
while(my $line = <XML>) {
$xml = $xml . $line;
}
while ($xml =~
/<item>.*?<id>(.*?)<\/id>.*?(<name>(.*?)<\/name>)?.*?<\/item>/gs) {
print "id : $1\n";
if ($3) {
print "name: $3\n";
}
}
My xml-document:
----------------
<xml>
<item>
<id>mf3</id>
<color>blue</color>
<name>moto F3</name>
</item>
<item>
<id>nk1</id>
</item>
<item>
<id>jk8</id>
<name/>
</item>
<item>
<id>la2</id>
<name>labo 2</name>
</item>
<xml>
My output:
----------
id : mf3
id : nk1
id : jk8
id : la2
Leif