perlnewb123 has asked for the wisdom of the Perl Monks concerning the following question:

Hey Monks :) could someone help with me this? I have a srting that's in the form of xml:
<?xml version="1.0" encoding="utf-8"?> <string xmlns="test.test.com/site/TST/">someresult</string> <?xml version="1.0" encoding="utf-8"?>
Everything in this string will stay the same except the result. In this example I need to pull someresult out of the xml sting.

Replies are listed 'Best First'.
Re: Help extracting text from XML data
by GrandFather (Saint) on Oct 20, 2008 at 20:21 UTC

    The module that will do you for today, tomorrow and many days after that for processing XML is XML::Twig. Consider:

    use warnings; use strict; use XML::Twig; my $xml = <<XML; <?xml version="1.0" encoding="utf-8"?> <string xmlns="test.test.com/site/TST/">someresult</string> XML my $twig = XML::Twig->new (twig_roots => {string => \&pullString}); $twig->parse ($xml); sub pullString { my ($t, $elt) = @_; print $elt->text (), "\n" }

    Prints:

    someresult

    Perl reduces RSI - it saves typing
      Thank you everyone! I had a regex that was parsing it down to >someresult<, but that was as far as I could get. This is much better!
Re: Help extracting text from XML data
by BrowserUk (Patriarch) on Oct 20, 2008 at 20:35 UTC

        Yes.

        $xml = '<string>Everyone knows that 1 &lt; 2</string>';; print $xml =~ m[>([^<]+)</string>]sm;; Everyone knows that 1 &lt; 2

        From the spec:

        The ampersand character (&) and the left angle bracket (<) MUST NOT appear in their literal form, except when used as markup delimiters, or within a comment, a processing instruction, or a CDATA section. If they are needed elsewhere, they MUST be escaped using either numeric character references or the strings "&" and "<" respectively.

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Help extracting text from XML data
by tmaly (Monk) on Oct 20, 2008 at 20:21 UTC
    use the module XML::Twig. specify a handler then for the element use $el->text to access the someresult value
Re: Help extracting text from XML data
by perrin (Chancellor) on Oct 20, 2008 at 20:19 UTC