Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do i use XML files in a perl cgi script?

Status
Not open for further replies.

jamsman

Technical User
Jul 22, 2005
38
DE
i have writen a script that queries a database and gets a set of results back in the form of an XML file. This file is loaded into a variable, now i need to display this data in a easy to read formate in a browser.

How can i do this?
How do i use the XML files in this way can i use an XSL file?

any ideas??

cheers
 
If you send the raw XML to a browser with proper headers, most of them make a reasonable fist of displaying it these days.
Code:
my $http_header = "Content-Type: text/xml\n\n";
my $xml_header = '<?xml version="1.0" encoding="ISO-8859-1"?>';
print $http_header, $xml_header, $xml;
I've just tried this for thunderbird, opera and ie with no problems.

f



&quot;As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.&quot;
--Maurice Wilkes
 
thank you i can get this output ok but i now need to make it more userfriendly any suggestions. how can i look inside the XML file and extract parts i need?

once again thank you
 
You need to parse it. If it's dead simple, it'll never chage and you'll never do another one, you could roll your own using regexes but I'd stongly recommend using one of several available modules. There are two distinct styles of XML programming. the first generates a DOM-type tree which you can traverse looking for what you want and the second allows to to register call-backs against specific tags.

Use tree-style parsing if you are familiar with javascript DOM programming and you'll feel right at home.

If you're happy with the concept of callbacks, you can get neater code with less system overhead (you don't need to read the entire file into memory before you can process it) by using this approach.

Have a look at XML::DOM and XML::parser and see which floats your boat. There are many specialised versions of both for extraordinary applications.

Yours,

f


&quot;As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.&quot;
--Maurice Wilkes
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top