can someone help me print a simple XML document using perl

Discussion in 'Perl Misc' started by inderpaul_s@yahoo.com, Apr 5, 2006.

  1. Guest

    I'm having trouble printing out a XML document from inside a while loop
    how can I do this ? This code is not entirely accurate as I'm new to
    Perl and XML. But the point is inside the loop contains scalars which
    are rettrieved from the loop. Many thanks for just reading.


    ================================================
    #!/usr/local/bin/perl -w
    use strict;
    use CGI::pretty;
    use HTML::TokeParser;
    use LWP::Simple;

    etc.....
    etc.....
    etc.....

    my $xmlDocument = <<END_OF_XML;
    <?xml version=\"1.0\"?>
    <freeDB>
    while (my $tagReference = $parser->get_tag('a'))
    {
    <search>$criteria</search>
    <text>$text</text>
    }
    </freeDB>
    END_OF_XML

    print $q->h1("The XML document is");
    print $q->header();
    print $q->pre($xmlForHTML);

    ===============================================
     
    , Apr 5, 2006
    #1
    1. Advertising

  2. Ron Savage Guest

    On Wed, 5 Apr 2006 16:19:57 +1000, wrote:

    Hi

    #!/usr/bin/perl
    #
    # Name:
    # display-xml.cgi.
    #
    # Author:
    # Ron Savage.
    # http://savage.net.au/index.html

    use strict;
    use warnings;

    use CGI;
    use HTML::Entities::Interpolate;

    # --------------

    my $xml = <<'END_OF_XML';
    <?xml version=\"1.0\"?>
    <freeDB>
    while (my $tagReference = $parser->get_tag('a'))
    {
    <search>$criteria</search>
    <text>$text</text>
    }
    </freeDB>
    END_OF_XML

    $xml = $Entitize{$xml};
    $xml =~ s|\n|<br />|g;
    my($q) = CGI -> new();

    print $q -> header(),
    $q -> start_html(),
    'Here is the XML document:<br /><br />',
    $xml,
    $q -> end_html();
     
    Ron Savage, Apr 5, 2006
    #2
    1. Advertising

  3. <> wrote:

    > I'm having trouble printing out a XML document from inside a while loop
    > how can I do this ?



    You are not trying to print an XML document from inside a while loop,
    you are trying to include code within a string.


    > my $xmlDocument = <<END_OF_XML;
    ><?xml version=\"1.0\"?>
    ><freeDB>
    > while (my $tagReference = $parser->get_tag('a'))
    > {
    > <search>$criteria</search>
    > <text>$text</text>
    > }
    ></freeDB>
    > END_OF_XML



    A "here document" (that's what the << thingy is called) is nothing
    more than another way of quoting a string.

    See the "Quote and Quote-like Operators" section in perlop.pod for
    a description of the ways a string can be quoted.

    You _could_ refactor it to bring the code outside of the quoted string:

    (double quotes are not special in a here-doc, so they don't need
    to be backslashed.
    )

    -------------------------------
    my $xmlDocument = <<END_OF_XML;
    <?xml version="1.0"?>
    <freeDB>
    END_OF_XML

    while (my $tagReference = $parser->get_tag('a'))
    {
    $xmlDocument .= "<search>$criteria</search>\n<text>$text</text>\n";
    }

    $xmlDocument .= <<END_OF_XML;
    </freeDB>
    END_OF_XML
    -------------------------------


    But it is probably better to give up on here-docs and use some
    other form of quoting:

    my $xmlDocument = qq(<?xml version="1.0"?>\n<freeDB>\n);
    while (my $tagReference = $parser->get_tag('a'))
    {
    $xmlDocument .= "<search>$criteria</search>\n<text>$text</text>\n";
    }
    $xmlDocument .= "</freeDB>\n";


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 5, 2006
    #3
  4. Paul Lalli Guest

    Ron Savage wrote:
    > On Wed, 5 Apr 2006 16:19:57 +1000, wrote:
    >
    > #!/usr/bin/perl
    > #
    > # Name:
    > # display-xml.cgi.
    > #
    > # Author:
    > # Ron Savage.
    > # http://savage.net.au/index.html
    >
    > use strict;
    > use warnings;
    >
    > use CGI;
    > use HTML::Entities::Interpolate;


    Could you please explain how the use of your module in any way solves
    the OP's problem?

    Paul Lalli
     
    Paul Lalli, Apr 5, 2006
    #4
  5. Guest

    Tad McClellan wrote:
    > <> wrote:
    >
    > > I'm having trouble printing out a XML document from inside a while loop
    > > how can I do this ?

    >
    >
    > You are not trying to print an XML document from inside a while loop,
    > you are trying to include code within a string.
    >
    >
    > > my $xmlDocument = <<END_OF_XML;
    > ><?xml version=\"1.0\"?>
    > ><freeDB>
    > > while (my $tagReference = $parser->get_tag('a'))
    > > {
    > > <search>$criteria</search>
    > > <text>$text</text>
    > > }
    > ></freeDB>
    > > END_OF_XML

    >
    >
    > A "here document" (that's what the << thingy is called) is nothing
    > more than another way of quoting a string.
    >
    > See the "Quote and Quote-like Operators" section in perlop.pod for
    > a description of the ways a string can be quoted.
    >
    > You _could_ refactor it to bring the code outside of the quoted string:
    >
    > (double quotes are not special in a here-doc, so they don't need
    > to be backslashed.
    > )
    >
    > -------------------------------
    > my $xmlDocument = <<END_OF_XML;
    > <?xml version="1.0"?>
    > <freeDB>
    > END_OF_XML
    >
    > while (my $tagReference = $parser->get_tag('a'))
    > {
    > $xmlDocument .= "<search>$criteria</search>\n<text>$text</text>\n";
    > }
    >
    > $xmlDocument .= <<END_OF_XML;
    > </freeDB>
    > END_OF_XML
    > -------------------------------
    >
    >
    > But it is probably better to give up on here-docs and use some
    > other form of quoting:
    >
    > my $xmlDocument = qq(<?xml version="1.0"?>\n<freeDB>\n);
    > while (my $tagReference = $parser->get_tag('a'))
    > {
    > $xmlDocument .= "<search>$criteria</search>\n<text>$text</text>\n";
    > }
    > $xmlDocument .= "</freeDB>\n";
    >
    >
    > --
    > Tad McClellan SGML consulting
    > Perl programming
    > Fort Worth, Texas



    Many many thanks...it worked...I was familiar with the << when printing
    HTML code from a CGI script but XML I was not sure of. Thanks to all.
     
    , Apr 5, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. George
    Replies:
    1
    Views:
    319
  2. Alan Silver
    Replies:
    0
    Views:
    346
    Alan Silver
    Oct 6, 2005
  3. Tony Prichard
    Replies:
    0
    Views:
    791
    Tony Prichard
    Dec 12, 2003
  4. MAX
    Replies:
    7
    Views:
    242
    A. Sinan Unur
    Dec 21, 2005
  5. Replies:
    18
    Views:
    229
    Tad McClellan
    Feb 6, 2006
Loading...

Share This Page