0

I am working on a file upload system that also parses the files that are uploaded and generates another file based on info inside the file uploaded. The files being uploaded as XML files. I only need to parse the first XML tag in each file and only need to get the value of the single attribute in the tag.

Sample XML:

<LAB title="lab title goes here">...</LAB> 

I am looking for a good way of extracting the value of the title attribute using the Perl split function or using Regex. I would use a Perl XML parser if I had the ability to install Perl modules on the server I am hosting my code on, however I do not have that ability.

This XML is located in an XML file, that I am opening and then attempting to parse out the attribute value. I have tried using both Split and Regex to no luck. However, I am not very familiar with Perl or regular expressions.

This is he basic outline my code so far:

open(LAB, "<", "path-to-file-goes-here") or die "Unable to open lab.\n"; foreach my $line (<LAB>) { my @pieces = split(/"(.*)"/, $line); foreach my $piece (@pieces) { print "$piece\n"; } } 

I have tried using split to match against title alone using

/title/ 

Or match against the = character or the " character using

/\=/ or /\"/ 

I have also tried doing similar things using regex and have had no luck as well. I am not sure if I am just not using the proper expression or if this is not possible using split/regex. Any help on the matter would be much appreciated, as I am admittedly a novice at Perl still. If this type of question has been answered elsewhere, I apologize. I did some searching and could not find a solution. Most threads suggest using an XML parsing Perl module, which I would if I had the privileges to install them.

2
  • 3
    It's 2013. Use an XML parser. Commented Apr 9, 2013 at 18:28
  • 2
    Yes, even you can use CPAN. Commented Apr 9, 2013 at 18:30

1 Answer 1

5

"But I can't use CPAN" is a quick way to get yourself downvoted on the Perl tag (though it wasn't I who did so). There are many ways that you can use CPAN, even if you don't have root. In fact you can have your own Perl even if you don't have root. While I highly recommend some of those options, for now, the easiest way to do this is just to download some Pure Perl modules, and included them in your codebase. Mojolicious has a very small, but very useful XML/DOM parser called Mojo::DOM which is a likely candidate for this kind of process.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the response and the suggestions. I'll definitely look into Perlbrew. I admit I didn't think of ways to install modules without root access and that was pretty dumb of me.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.