can someone help me print a simple XML document using perl

I

inderpaul_s

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);

===============================================
 
R

Ron Savage

On Wed, 5 Apr 2006 16:19:57 +1000, (e-mail address removed) 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();
 
T

Tad McClellan

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";
 
P

Paul Lalli

Ron said:
On Wed, 5 Apr 2006 16:19:57 +1000, (e-mail address removed) 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
 
I

inderpaul_s

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




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";


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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top