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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing XML slows down the web

Status
Not open for further replies.

danzian

Technical User
Apr 7, 2006
11
US
Hello,

I'm new here and I'm totally new to XML. I have some experience in PHP but some other programming language.

I have read a tutorial to parse XML in PHP and successfully displayed the contents in the webpage using PHP, but I'm not really happy with the speed of loading of the page.

If I could get help on loading the page faster, I would very much appreciate your help.

This is the page I'm working on:

The code can be found here:

Or, here is the code itself:
Code:
<?php
$flag = 0;
$ip = "69.180.180.46";
$port = "81";
$portts = "8767";
$timeout = "1";
$nukeurl = "[URL unfurl="true"]http://www.stpaulairlines.com/page.php?height=1750&url2=http://";[/URL]

$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false);
xml_set_element_handler($parser, "start_tag", "end_tag");
xml_set_character_data_handler($parser, "tag_contents");

if ($ip and $port and $timeout)
{
$connection = @fsockopen("$ip", $port, $errno, $errstr, $timeout);
}
echo "<div align='center'>";
echo "<br>";
if(!$connection)
{
echo "<font color='#990000' > <b>Flight Server Offline</b></font>";
echo "<br>";
}
else
{
$document = file("[URL unfurl="true"]http://69.180.180.46:81/xml");[/URL]
echo "<font color='#006600' align='center' ><b>Flight Server Online</b></font>";
echo "<br><br>";
echo "Onine Pilots: ";

foreach ($document as $line)
{
xml_parse($parser, $line);
}

echo "<br>";
echo "<a href=$nukeurl" .$ip. ":" .$port. "/>Click here for <br>Details</a>";


xml_parser_free($parser);

echo "<br>";

echo "<br>";
echo "</div>";
}



function start_tag($parser, $name, $attribs)
{
global $flag;
if ($name=="CurrentPilots") $flag=1;
if ($name=="Name") $flag=2;

}
function end_tag($parser, $name)
{
global $flag;
if ($name=="CurrentPilots") $flag=0;
if ($name=="Name") $flag=0;
}
function tag_contents($parser, $data)
{
global $flag;
if ($flag==1)
echo "<font color='#0033CC' align='center' ><b>" .$data. "</b><br /></font>---------------------------<br>";
if ($flag==2)
echo "<font color='#FF6633' align ='center'><b>" .$data. "</b><br /></font>---------------------------<br>";
}

?>

On the line 27: $document = file("I am suspecting this is taking a long time to load. As soon as it loads the file, the display of the contents is instant.

Am I doing it wrong? Or is there a better way to load XML into a variable?

Again, I really appreciate your time even to look at my post. I'm very grateful.

I'm planning on using this code in the left panel of the front page of my website:
Thank you very much. Have a great day.

Sincerely,
Danzian
 
Danzian,

It might be a bit faster if you were to use XSLT, since it appears you are pretty much deriving an HTML document from the XML document. By doing so, you would probably avoid loading the document into the PHP version of the DOM, which might be what is taking so long, and I don't see any added value in the PHP code you have shown (i.e. XSLT can do everything you are doing in PHP).

Tom Morrison
 
Thank you Tom for the quick reply.

I'm not very familiar with either XML or XSLT. I have read some articles about XSLT though. My understanding is that you need to have an XSLT file first in order to fetch the data. Am I right?

The server I'm getting data from does not have XSLT file, only XML file:

Can I quickly convert that into xslt?

Also, you mentioned DOM, I have not used any DOM functions in my codes. I think I should try that as well.

Thanks.

Danzian
 
Apparently,

DOM objects do not run on my webhost. We have 4.4.2 php version, and still I do not know why.

In my personal apache server at home, the codes work flawlessly. I have PHP5.1.1 at home.

Well well.

Danzian
 
Go through the w3schools tutorial here.

You can also download Stylus Studio and try it free for a while. If you have an XHTML page as your target, and an example XML document as input, Stylus Studio should be able to crank out an XSLT quite easily using its XSLT Mapper function.

Tom Morrison
 
A-ha,

that's what I'm reading now. Coincidence, I say.

Thanks.

Danzian
 
XSLT is not really going to work for me at least with that tutorial.

The output data I get is in the raw format ie. ^^player_name^^, instead of the real name of the pilot. As you can tell I was working on the master xml.html file not the generated xml file.

First I need to fetch the data from to a file/variable. And that is what's causing the delay in my original codes.

I'm not really making any sense, am I? :) I will look around more later.

Thanks.
Danzian
 
When comparing these two links:


I moved the $document = file(" command below two display commands.

As you can see, the loading of the xml file takes a lot of time. There has to be a faster way of doing that.

I gotta run for now. Will look at it again later in the evening.

I appreciate taking your time to read my post and I will report back when I find out something about it.

Regards,
Danzian





Do you know how to make that fast?



Gotta run.



Naresh
 
Naresh,

Of course, I can GET the XML document with my browser almost instantaneously. I would seriously consider the concept that parsing the document in the manner you are using is what is taking the time. It may be that the PHP bindings are inefficient.

Here is an XSLT stylesheet that fetches the document and creates an HTML page similar to yours.
Code:
<?xml version='1.0' ?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
	<xsl:template match="/">
		<xsl:apply-templates select="document('[URL unfurl="true"]http://69.180.180.46:81/xml')"[/URL] mode="makedoc"/>
	</xsl:template>
	<xsl:template match="/" mode="makedoc">
		<html>
			<body>
				<div align="center">
				<br />
				<font color="#006600" align="center"><b>Flight Server Online</b></font>
				<br/><br/>Online Pilots: 
				<font color="#0033CC" align="center">
						<b>
							<xsl:value-of select="count(FSHost/Players/Player)"/>
						</b><br/>---------------------------<br/>
						<xsl:for-each select="FSHost/Players/Player">
							<b><xsl:value-of select="Name"/></b>
							<br/>
						</xsl:for-each>
					</font>---------------------------<br/>
					<a><xsl:attribute name="href">
					<xsl:text disable-output-escaping="yes"><![CDATA[[URL unfurl="true"]http://www.stpaulairlines.com/page.php?height=1750&url2=http://69.180.180.46:81/[/URL]]]></xsl:text>
					</xsl:attribute>Click here for <br/>Details</a>
				</div>
			</body>
		</html>
	</xsl:template>
</xsl:stylesheet>

Tom Morrison
 
Hi Tom,

Thank you very much for all your effort to help me.

I have to tell you one thing that the test.php is only a test page. It will be displayed on block like below:
Clip_2.jpg


I am not sure how I am going to call xslt file from that block. Perhaps I can use the RSS block.

I did try your code and it works great. But I can not call that xml file from the url. It is something different and I think it is not worth explaining here.

The server generates xml file from remoteaccessxml.html. Thus the link instead of a filename for example
I did add the <?xml-stylesheet type="text/xsl" href="block1.xsl"?> line in that remoteaccessxml.htm but it generates error with the link
However, if I rename the file remoteaccessxml.htm to remoteaccessxml.xml, I can open it(from my computer) in IE with no problem. I can not get that to work in Firefox though. But again, it can't be accessed from internet.

It was a good learning experience for me though. Thanks. I will look at it again this weekend.

Have a great weekend.

Danzian
 
Hi,

I removed all the parse functions and I can confirm that the line $document = file(" is the culprit.

I created a static xml file and called it using $document = file("onlineserver.xml"); and the web page loads instantly.

This is the page with static xml file that loads instantly:

Code:
<?php
$flag = 0;
$ip = "69.180.180.46";
$port   = "81";
$portts = "8767";
$timeout = "1";
$nukeurl = "[URL unfurl="true"]http://www.stpaulairlines.com/page.php?height=1750&url2=http://";[/URL]


 if ($ip and $port and $timeout) 
 {
   $connection =  @fsockopen("$ip", $port, $errno, $errstr, $timeout);
 }
echo "<div align='center'>";
echo "<br>";
 	if(!$connection)
 	{
   		echo "<font color='#990000' > <b>Flight Server Offline</b></font>";
  		 echo "<br>";
    }
	else
	{





//	    $document = file("[URL unfurl="true"]http://69.180.180.46:81/xml");[/URL]
		$document = file("onlineserver.xml");
   		echo "<font color='#006600' align='center' ><b>Flight Server Online</b></font>";
		echo "<br><br>";
		echo $document[10];
		echo "<br><br>";
		echo $document[11];
		echo "<br><br>";
	   	echo "Onine Pilots: ";
		echo $document[14];
        echo "<br>";
   		
   

	    echo "<br>";

		echo "<br>";
		echo "</div>";
	} 
 
 
?>

This is the page with URL file call that takes a while to load:

Code:
<?php
$flag = 0;
$ip = "69.180.180.46";
$port   = "81";
$portts = "8767";
$timeout = "1";
$nukeurl = "[URL unfurl="true"]http://www.stpaulairlines.com/page.php?height=1750&url2=http://";[/URL]


 if ($ip and $port and $timeout) 
 {
   $connection =  @fsockopen("$ip", $port, $errno, $errstr, $timeout);
 }
echo "<div align='center'>";
echo "<br>";
 	if(!$connection)
 	{
   		echo "<font color='#990000' > <b>Flight Server Offline</b></font>";
  		 echo "<br>";
    }
	else
	{





	    $document = file("[URL unfurl="true"]http://69.180.180.46:81/xml");[/URL]
//		$document = file("onlineserver.xml");
   		echo "<font color='#006600' align='center' ><b>Flight Server Online</b></font>";
		echo "<br><br>";
		echo $document[10];
		echo "<br><br>";
		echo $document[11];
		echo "<br><br>";
	   	echo "Onine Pilots: ";
		echo $document[14];
        echo "<br>";
   		
   

	    echo "<br>";

		echo "<br>";
		echo "</div>";
	} 
 
 
?>

Notice the difference in $document line:

$document = file(" slow
$document = file("onlineserver.xml"); instant

I will now need to find a better way to store xml file to an array or I need to somehow try to get the dynamic xml file for my flight server.

Thanks for reading my post. Have a great day.

Danzian
 
Hi Tom,

Thank you very much for the suggestion. I just did a quick look at it and it looks promising.

I will give this a try later tonight. Oh, the weather is so nice here in MN, finally spring is here.

Have a great weekend.

Regards,
Danzian
 
Hello Tom,

Well, what can I say. I get this error.

Sablotron error on line 1: XML parser error 4: not well-formed (invalid token)


I have searched an answer for this error code in Google, and it seems like many people have this problem. I could not find a good answer to this.

Me tired. will look at it more later.

Thanks for all your help.

Danzian
 
A follow up,

I actually got my original code working now.

It so happened, it was the port issue. Somehow, using the same port could give you delay in queries.

$port = "81";
$connection = @fsockopen("$ip", $port, $errno, $errstr, $timeout)
$document = file(" were having some kind of problem.

I changed that TCP $port value to a different open TCP and it worked.

Thanks Tom for all your help. I appreciate that.

Best wishes,
Danzian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top