Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

XML parsing

by Anonymous Monk
on Mar 19, 2002 at 20:50 UTC ( #152839=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I need to get the data from between the file_size tags. How do I do that? I went to a couple of sites, but they where lacking in clarity.
<?xml verison="1.0"?> <Document_Confirmation> <FILE_NAME>ack_mr00000001.xml</FILE_NAME> <DATE_RECEIVED>11/27/2001</DATE_RECEIVED> <FILE_SIZE>2022</FILE_SIZE> </Document_Confirmation>

Replies are listed 'Best First'.
Re: XML parsing
by gellyfish (Monsignor) on Mar 19, 2002 at 21:02 UTC

    Ignoring your typo in the XML declaration :) - If all you want to do is get that value and this is the entirety of your document then you probably want to go with XML::Simple :

    #!/usr/bin/perl -w use XML::Simple; use strict; local $/; my $data = XMLin(<DATA>); print $data->{FILE_SIZE}; __DATA__ <?xml version="1.0"?> <Document_Confirmation> <FILE_NAME>ack_mr00000001.xml</FILE_NAME> <DATE_RECEIVED>11/27/2001</DATE_RECEIVED> <FILE_SIZE>2022</FILE_SIZE> </Document_Confirmation>
    This is of course is reading from DATA - you will replace the <DATA> with a variable containing your XML document.


      Thank you. bad news, our box does not have XML/ on it, and no, they won't add it because its their machine damn it, and I'll need to find another way to do it. There is nothing like cooperation, and our systems people our nothing like cooperative.

        Does your box have XML::Parser on it? Install XML::Simple in your user space, then. Yes, it's a pain. Yes, it's worth it. XML parsing is a complex, often subtle problem that's difficult to get right -- so let other people get it right, and use the wheels they invent.

        If you don't have the requisite modules for XML::Simple (and that includes a compiled Expat library, IIRC), you're in deeper trouble. You might have some luck with XML::Parser::Lite, which I haven't tried but seems to be entirely Perl-based. If you insist on building your own parser, consider using XML::RegExp.

        It might be easier to seek a political solution to the problem (i.e. have your manager lean on the systems people until XML/ appears in your @INC).

        I don't mean to be condescending, but parsing XML really is a lot harder than you think. Writing a half-assed collection of regexes (you'd have more luck using Text::Balanced, of course) that breaks on 90% of the XML out there is easy. Getting it to work properly is insanely hard. (I've been there. I gave up.) Telling your boss that your parser reads XML, but not XML with newfangled features foo, bar, and baz, is probably not acceptable.

        Good luck.


        Just get the tar ball from CPAN yourself, and install it in your own area (along with your programs). Is that not possible?

        Of course, you need to consider the dependencies too (like XML::Parser which XML::Simple depends on), but you get the idea.


        quick helpful addition:

        there are a couple ways to include whatever modules you install in your own space. the one i tend to use for situations like this is:

        use qw(/path/to/your/custom/install);

        use lib qw(/path/to/your/custom/install); use XML::Parser;
        ... etc.



        FoxtrotUniform caught that typo. Thanks, FU ;-)
Re: XML parsing
by cjf (Parson) on Mar 19, 2002 at 22:36 UTC

    I recently posted Working with XML and received some excellent responses and module suggestions there. I'm sure the thread will be of use to you as well. :)

Re: XML parsing
by Matts (Deacon) on Mar 20, 2002 at 07:32 UTC
    If your system doesn't have expat on, you can use the pure perl parser built into XML::SAX. Installed it locally in your user space and then use XML::SAX::Simple (also on CPAN, it's a XML::Simple look-alike module that uses SAX) to access the XML.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://152839]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2018-07-21 13:59 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (449 votes). Check out past polls.