Extracting a specific string from an XML file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ahammad
    New Member
    • May 2007
    • 79

    Extracting a specific string from an XML file

    Hello,

    I have an XML file that I need to extract data from. The file has multiple lines, one of which contains the following tag:

    Code:
    <Owner> owner's name goes here </Owner>
    I need to be able to extract the owner's name from the XML file. I have never programmed Perl before, but I have a pretty strong background in C/C++ and Java. You're probably wondering why the hell I'm jumping into this with no experience in Perl...it wasn't my choice, really. I just started working for a company and the person in charge of the Perl scripts is no longer here, and I just happened to be the only person free enough to take over the project.

    I've been doing some research on regular expressions and I found this code:

    Code:
    if($mystring =~ m/start(.*)end/) {
    	print $1;
    }
    Apparently, this would print everything between the word "start" and "end". Based on this, would the following code work?

    Code:
    if($mystring =~ m/<Owner>(.*)</Owner>/) {
    	print $1;
    }
    I'm thinking that the / in </Owner> would cause problems...how would that be fixed?

    Thanks for your help.
  • eWish
    Recognized Expert Contributor
    • Jul 2007
    • 973

    #2
    Really you should probably look at using XML::Simple or XML::Parser. However, you can do it the way you are thinking. To keep </Owners> from causing problems just escape the forward slash.[CODE=Perl]my $xml_file = 'myfile.xml';
    my @owners;

    open(my $XMLFILE, '<', $xml_file) ||die "Can't open file: $!";
    while (my $line =<$XMLFILE>) {

    if($line =~ /<Owners>(.*)< \/Owners>/) {
    push @owners, $1;
    }
    }
    close($XMLFILE) ;

    print join("\n", @owners);[/CODE]

    Not sure how big of a file you are talking about. So, there could be a performance issue.

    Comment

    Working...