• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

Parsing XML/Formatted TXT into a HTML document.

Okay, this probaly sounds really easy to you intelligent folk, but not to me. ;)

How do I change the XML data I've got into a pretty html page for my visitors?

I want to make a search-engine using Seachfeed's results and incorporate it into my own layout, I would create boxes for it and would most likley call it via http://www.djcl.com/script.php?q=query or however you do it, you get the idea...

Here is what SearchFeed Say:
XML format

This feed format is used for the server side development. For example, if you call similar URL: http://www.searchfeed.com/rd/feed/XMLFeed.jsp?cat=dogs&excID=162&pID=571&nl=5&page=1&ip=1.1.1.1, search results will be returned in the XML formatted file. The breakdown of the URL was shown in the JavaScript example. You should provide the URL parameters that are specific to your needs.



XML will have the following format:

<Listings>

<Page></Page>

<Count></Count>

<Listing>

<Title></Title>

<URL></URL>

<URI></URI>

<Description></Description>

<Bid></Bid>

</Listing>

<Listing>

<Title></Title>

<URL></URL>

<URI></URI>

<Description> </Description>

<Bid></Bid>

</Listing>



</Listings>

So how do I make that go into a HTML template, like normal search results?

Thanks,
 
the easy way is to

  • suck all the xml into a variable $xml
  • do some reg expression search and replace

    eg
    PHP:
    $xml=preg_replace("/<description>([^<]*)<\/description>/","<p><B>$1</B></P>",$xml);
  • print to browser header , $xml , footer
 
Last edited:
a more cunning idea is to use the xml to create a hash of hashes of the info and then you have more flexibilty with doing your template work.

I have a perl example of this I could dig out if you wanted
 
I'd love to see it! Thanks lucifer! :)

Also, the lovley code you gave me goes in PHP right? or perl? or? :confused:

PS. You're new aviator's pretty kewl! Although it don't flash enough... ;)
 
your's is pretty cool too now you've got the chessboard going.

I've been having problems with the animated transparancy on K-meleon browser so I'm having to do some reworking updates soon


what language is your preferrence?


here's the code

Code:
use strict;
my $sampleXML = qq(
<?xml version="1.0"?> 
        <response>
	    <portfolio>
		<portID>Test</portID>
		<portname>Consors Test Portfolio</portname>
		<xloss>-5124.88530628508</xloss>
		<riskgrade>129.39826419201</riskgrade>
		<value>114260.469443242</value>
		<divriskgrade>68.4645462588067</

divriskgrade>
		<divxloss>-2131.56017715564</divxloss>

		<position>
			<posID>Bayer</posID>
			<xloss>-197.254957013363</xloss>
			<riskgrade>148.955450569846</riskgrade>
			<value>6138</value>
			<riskimpact>-0.00822567904632093</riskimpact>
		</position>
		<position>
			<posID>Bond</posID>
			<xloss>-157.580177492695</xloss>
			<riskgrade>29.3498701802706</riskgrade>
			<value>18922.4694432424</value>
		</position>		
                </portfolio>
        </response>                
);

my $portresults = parsePortfolioResponse($sampleXML);

sub parsePortfolioResponse {
        my $response = $_[0];
        
        my %portresults;
        
        my $stats = "riskgrade|riskimpact|xloss|value|divriskgrade|divxloss|posID";

        #assumes portstats come first
        my $ref = "portfolio";
        while ($response =~ m!<($stats)>(.*?)</($stats)>!g) {
                if ($1 eq "posID") {
                        $ref = $2;
                } else {                        
                        $portresults{$ref}->{$1} = $2 if $2;
                }
        }
                              
        return \%portresults;
}

you'd need to do a rewrite for what you want but it should give you an insight

have fun ;)
 
Oh thankyou lucifer!

* Kisses satan's feet, oooh... there hot! ;) *

Thanks! Thanks! Thanks! :)

Originally posted by lucifer
your's is pretty cool too now you've got the chessboard going.

I've been having problems with the animated transparancy on K-meleon browser so I'm having to do some reworking updates soon

Sounds very kewl! I'll be waitinf for the new one to come out!

Originally posted by lucifer
what language is your preferrence?

I'm guessing for this task PHP would be most useful... just a guess, I'll go with most things...

Originally posted by lucifer
here's the code

Code:
use strict;
my $sampleXML = qq(
<?xml version="1.0"?> 
        <response>
	    <portfolio>
		<portID>Test</portID>
		<portname>Consors Test Portfolio</portname>
		<xloss>-5124.88530628508</xloss>
		<riskgrade>129.39826419201</riskgrade>
		<value>114260.469443242</value>
		<divriskgrade>68.4645462588067</

divriskgrade>
		<divxloss>-2131.56017715564</divxloss>

		<position>
			<posID>Bayer</posID>
			<xloss>-197.254957013363</xloss>
			<riskgrade>148.955450569846</riskgrade>
			<value>6138</value>
			<riskimpact>-0.00822567904632093</riskimpact>
		</position>
		<position>
			<posID>Bond</posID>
			<xloss>-157.580177492695</xloss>
			<riskgrade>29.3498701802706</riskgrade>
			<value>18922.4694432424</value>
		</position>		
                </portfolio>
        </response>                
);

my $portresults = parsePortfolioResponse($sampleXML);

sub parsePortfolioResponse {
        my $response = $_[0];
        
        my %portresults;
        
        my $stats = "riskgrade|riskimpact|xloss|value|divriskgrade|divxloss|posID";

        #assumes portstats come first
        my $ref = "portfolio";
        while ($response =~ m!<($stats)>(.*?)</($stats)>!g) {
                if ($1 eq "posID") {
                        $ref = $2;
                } else {                        
                        $portresults{$ref}->{$1} = $2 if $2;
                }
        }
                              
        return \%portresults;
}

you'd need to do a rewrite for what you want but it should give you an insight

have fun ;)

Thanks, I'll post my re-written work here first... because I'll most likley make a few mistakes! Thanks again! :)

Lucifer helps the cow out of it's problems,
 
lol, excitement short lived... hitting self in head...

I'll need variable thingy for search, $earch, don't know how do do that... :eek:

Also I'll just use $IP for their IP, hope that works...

Code:
use strict;
my $sampleXML =  [url]http://www.searchfeed.com/rd/feed/XMLFeed.jsp?cat=/$earch&excID=162&pID=571&nl=5&page=1&ip=$IP[/url]);

my $portresults = parseListingsListing($sampleXML);

How's it going? :confused:

Thanks,
 
Last edited:
In Perl I think you'll have to use LWP module

I'd use php

PHP:
$xml=file("whateverthaturlwas");

$xml=implode("",$xml);

for the IP use their's $REMOTE_ADDR or your sites or just make one up and see if it works
 
Aaahhh.... I'm dying here... 2 hours and I give up! :(

PHP:
<?PHP

$xml=file("http://www.searchfeed.com/rd/feed/XMLFeed.jsp?cat=dogs&excID=162&pID=571&nl=5&page=1&ip=$REMOTE_ADDR");

$xml = str_replace("<title>","<b>",$xml); 
$xml = str_replace("</title>","</b>",$xml); 
$xml = str_replace("<url>","<i>",$xml); 
$xml = str_replace("</url>","</i>",$xml); 

// $xml=implode("",$xml); Don't Work
// echo $xml; Comes up with Array
// implode & join don't work... :(

?>

I didn't get very far, although I get the basic idea, I think... I just need to know how to print the array...

Also how do you make <url>*</url> a string?

Thanks,
 
this worked

PHP:
<?

# get xml
$xml=file("http://www.searchfeed.com/rd/feed/XMLFeed.jsp?cat=dogs&excID=162&pID=571&nl=5&page=1&ip=1.1.1.1");

$xml=implode("",$xml);


# get page/count info
preg_match("/<Page>(.*?)<\/Page>/i",$xml,$match);
$page=$match[1];

preg_match("/<Count>(.*?)<\/Count>/i",$xml,$match);
$count=$match[1];


# parse xml
$tags="(Title|URL|URI|Description|Bid)";

preg_match_all("/<$tags>(.*?)<\/$tags>/i",$xml,$match,PREG_SET_ORDER);

# now all data in $match[x][y]
# bit of funny order
# $match[x][1] = tag name
# $match[x][2] = value
#
# where x=1 - 6 first record, 7-11 second etc.
# could be sorted better


# print data

for( $i=0 ; $i<($count * 5) ; $i++ ){

# clean up title/descript fields due to nasty xml

$match[$i][2]=preg_replace ("/<!\[CDATA\[/","",$match[$i][2]);
$match[$i][2]=preg_replace ("/\]\]>/","",$match[$i][2]);

echo $match[$i][1] .": ". $match[$i][2]. "<br>";

}
?>

hope that helps ;)
 
Here we go again :) this is a much better version and sorts things into an array of hashes so that it's simpler to use.

I don't know why you had an implode problem - it's a core part of the language.

the output page is poor no <HEAD><HTML> etc but it shows how to do it. formating and link etc.


PHP:
<?
# get xml
$xml=file("http://www.searchfeed.com/rd/feed/XMLfeed.jsp?cat=dog&execID=162&pID=571&nl=5&page=1&ip=1.1.1.1");
$xml=implode("",$xml);

# pull out page/count tag values
preg_match("/<Page>(.*?)<\/Page>/i",$xml,$match); 
$page=$match[1];
preg_match("/<Count>(.*?)<\/Count>/i",$xml,$match); 
$count=$match[1]; 

# get rid of nasty bits in xml (title/description)
$xml=preg_replace("/<!\[CDATA\[/","",$xml);
$xml=preg_replace("/\]\]>/","",$xml); 

# pull out the fields/values
$tags="(Title|URL|URI|Description|Bid)"; 
preg_match_all("/<$tags>(.*?)<\/$tags>/i",$xml,$match,PREG_SET_ORDER); 

# arrange into array of hashes
#  $info[ result number ][field]
#  eg $info[0][Title]  - title of first result
#     $info[3][URL]  - URL of 4th record
for ($i=0;$i<$count;$i++){ 
	for ($j=0;$j<5;$j++){ 
		$key=$match[($i*5+$j)][1]; 
		$info[$i][$key]=$match[($i*5+$j)][2]; 
	} 
} 


# output page

echo "<p>Page $page of $count</p>";

for ($i=0;$i<$count;$i++){
?>
<p><a href="<?=$info[$i][URL]?>" target="_new"><b><?=$info[$i][Title]?></b></a><br><?=$info[$i][Description]?></p><hr>
  
<? } ?>
 
Thankyou soooo much lucifer!!! But before I kiss your hot feet again I think I'll be checking it... ;)

{EDIT}

Trying the last one...
Warning: Unknown option 'P' in /home/djcl/public_html/beta/php/31.php on line 11

Warning: Unknown option 'C' in /home/djcl/public_html/beta/php/31.php on line 13

Warning: Compilation failed: missing terminating ] for character class at offset 9 in /home/djcl/public_html/beta/php/31.php on line 17

Warning: Unknown option '(' in /home/djcl/public_html/beta/php/31.php on line 22

Page of


Parse error: parse error in /home/djcl/public_html/beta/php/31.php on line 42

Hmmm... I'll go check my server config for PHP etc...

Apparently I need a header with the code #!/usr/bin/php4 included to access all the php4 stuff... I added it under the <? thingy... and used the first script... no luck, second...

Warning: Unknown option 'P' in /home/djcl/public_html/beta/php/34.php on line 11

Warning: Unknown option 'C' in /home/djcl/public_html/beta/php/34.php on line 14

Warning: Unknown option '(' in /home/djcl/public_html/beta/php/34.php on line 21

Damn! :(
 
Last edited:
funny things :eek:

seems all my \ got eaten when I Ctrl-V

PHP:
# pull out page/count tag values

preg_match("/<Page>(.*?)</Page>/i",$xml,$match); 

$page=$match[1];

preg_match("/<Count>(.*?)</Count>/i",$xml,$match); 

$count=$match[1]; 


# get rid of nasty bits in xml (title/description)

$xml=preg_replace("/<![CDATA[/","",$xml);

$xml=preg_replace("/]]>/","",$xml); 




# pull out the fields/values

$tags="(Title|URL|URI|Description|Bid)"; 

preg_match_all("/<$tags>(.*?)</$tags>/i",$xml,$match,PREG_SET_ORDER);

should be

Code:
# pull out page/count tag values

preg_match("/<Page>(.*?)<[B][COLOR=red]\[/COLOR][/B]/Page>/i",$xml,$match); 

$page=$match[1];

preg_match("/<Count>(.*?)<[B][COLOR=red]\[/COLOR][/B]/Count>/i",$xml,$match); 

$count=$match[1]; 


# get rid of nasty bits in xml (title/description)

$xml=preg_replace("/<![B][COLOR=red]\[/COLOR][/B][CDATA[B][COLOR=red]\[/COLOR][/B][/","",$xml);

$xml=preg_replace("/[B][COLOR=red]\[/COLOR][/B]][B][COLOR=red]\[/COLOR][/B]]>/","",$xml);




# pull out the fields/values

$tags="(Title|URL|URI|Description|Bid)"; 

preg_match_all("/<$tags>(.*?)<[B][COLOR=red]\[/COLOR][/B]/$tags>/i",$xml,$match,PREG_SET_ORDER);

sorry about that
 
#! thing should be on first line of script before anything

I think this board must use stripslashes() somewhere in it's routine

the stuff above is for the last script the other needs similar slashes putting in

I know they work cos I ran them on my server before I posted them

sorry you've been f****d about


let me know how you do and let's see it when it's working It's got me curious about this bidding stuff
 
Thanks again! We're getin' closer, well when I say we I really mean you... but anyway...

Okay, that's sort of there, now I've only got...
Parse error: parse error in /home/djcl/public_html/beta/php/37.php on line 41

So I modified the line to:
Code:
<p><a href="<?=$info[$i][URL]?>" target="_new"><b><?=$info[$i][Title]?><[COLOR=red]\[/color]/b><[COLOR=red]\[/color]/a><br><?=$info[$i][Description]?><[COLOR=red]\[/color]/p><hr>

Although still no luck... :(
 
Originally posted by }:8) Supermoo
Thanks again! We're getin' closer, well when I say we I really mean you... but anyway...

Okay, that's sort of there, now I've only got...


So I modified the line to:
Code:
<p><a href="<?=$info[$i][URL]?>" target="_new"><b><?=$info[$i][Title]?><[COLOR=red]\[/color]/b><[COLOR=red]\[/color]/a><br><?=$info[$i][Description]?><[COLOR=red]\[/color]/p><hr>

Although still no luck... :(
good try

Code:
echo "<p><a href=[COLOR=red]\[/color]"<?=$info[$i][URL]?>[COLOR=red]\[/color]" target=[COLOR=red]\[/color]"_new[COLOR=red]\[/color]"><b><?=$info[$i][Title]?></b></a><br><?=$info[$i][Description]?></p><hr>
";

this is a "quoted" string so we need to backslash " and \ only to keep them

the others were regular expressions! so different rules in reg exp's backslash everything that is not alphanumeric if you want it to be it's self

confussing :)
 
lol :confused:

Anyway... I changed it and suprise... got another error!

It returns the following...

#!/usr/bin/php4
Page of


Parse error: parse error in /home/djcl/public_html/beta/php/40.php on line 42

I'm heading off now, so c'ya later... :)
 
ignore that last thing it was html not php anyway - me being silly

I've incuded the file more sensible it worked for me!!

big problem

that link is down

my script does not error check (yet)

let me know how you get on
 

Attachments

  • moo.txt
    1.2 KB · Views: 31
Back
Top