There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot bsod computer connection cpu crash css dell desktop dma driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware hijackthis hjt install internet internet explorer itunes keyboard laptop macro malware monitor motherboard network networking outlook outlook 2003 outlook 2007 outlook express pio problem problems router seo server slow sound sp3 spyware trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless
Web Design & Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Internet & Networking > Web Design & Development >
PHP/XML problems


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
Bruxy's Avatar
Junior Member with 19 posts.
 
Join Date: May 2005
Location: Manchester, UK
Experience: Advanced
06-Dec-2005, 01:02 PM #1
PHP/XML problems
I've been teaching myself PHP to do a piece of coursework, which is basically a mini search engine. The database is in MySQL and everything is working fine until I try to do the part which requires me to putput the data from the database in XML format.

I keep getting this error:

Quote:
The XML page cannot be displayed
Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.


--------------------------------------------------------------------------------

Whitespace is not allowed at this location. Error processing resource 'http://localhost:81/xmloutput3.php'. Line 1, Positi...

<?xml version="1.0"?><root><Company><compID>1</compID><name>Sewells Training & Consu...
My code seems fine. Sometimes I'll get an output that gives all the right details just without the XML formatting, then I'll refresh it again and it'll give the above error. Here's the PHP that creates the XML output:

PHP Code:
$xml = '<?xml version="1.0"?>';

$xml .= '<root>';

foreach($CompInfoArray as $CompInfo) {

    $xml .= '<Company>';

    $xml .= '<compID>'.$CompInfo->XcompID.'</compID>';
    $xml .= '<name>'.XmlEntities($CompInfo->Xname).'</name>';
    $xml .= '<add1>'.XmlEntities($CompInfo->Xadd1).'</add1>';
    $xml .= '<add2>'.XmlEntities($CompInfo->Xadd2).'</add2>';
    $xml .= '<add3>'.XmlEntities($CompInfo->Xadd3).'</add3>';
    $xml .= '<postcode>'.XmlEntities($CompInfo->Xpostcode).'</postcode>';
    $xml .= '<Size>'.$CompInfo->XSize.'</Size>';
    $QueryString = "select compt from keywords, relationships where (relationships.comptID = keywords.comptID) and 

(relationships.compID = ".$CompInfo->XcompID.")";
    $KeywordQuery = mysql_query($QueryString);
    $KeywordArray = array();
    while($row2 = mysql_fetch_assoc($KeywordQuery))
        array_push($KeywordArray, $row2['compt']);
    foreach($KeywordArray as $Keyword)
        $xml .= '<compt>'.XmlEntities($Keyword).'</compt>';

    $xml .= '</Company>';

}

$xml .= '</root>';

echo($xml);

The thing is my friend's is pretty much identical in final layotu and works fine, but between us we can't figure out for the life of us why it doesn't like mine.

Experienced advice would be much appreciated!

Cheers
php guy's Avatar
Computer Specs
Senior Member with 514 posts.
 
Join Date: Dec 2004
Location: Bowlaro Lanes, WA
Experience: That's Mr. Clueless to you
06-Dec-2005, 01:20 PM #2
Are the browsers you two are using different? If so, what browser are you using?
Bruxy's Avatar
Junior Member with 19 posts.
 
Join Date: May 2005
Location: Manchester, UK
Experience: Advanced
06-Dec-2005, 02:55 PM #3
I'm on the latest IE. My friend's used IE, Firefox and Oprah to try it.

I've just run some basic XML test files and they work - simple echo statements with 1 data element and the like are all displayed correctly so there's something bad in the code which I can't spot. Looks like you might need it all:

PHP Code:
<?php

include 'config.php';
include 
'opendb.php';
include 
'classes.php';

function 
XmlEntities($data
{
    
$position 0;
    
$length strlen($data);
    
$escapeddata "";
    for(;
$position<$length;) {
        
$character substr($data$position1);
        
$code Ord($character);
        switch(
$code) {
            case 
34:
            
$character '"';
            break;

            case 
38:
            
$character "&";
            break;

            case 
39:
            
$character "&apos;";
            break;

            case 
60:
            
$character "<";
            break;

            case 
62:
            
$character ">";
            break;

            default:
            if (
$code<32)
                
$character = ("&#".strval($code).";");
            break;
        }
        
$escapeddata .= $character;
        
$position++;
    }
    return 
$escapeddata;
}

$Query mysql_query('select * from compinfo');
$CompInfoArray = array();
while(
$row mysql_fetch_assoc($Query)) {

    
$CompInfo = new CompInfoSet();
    
$CompInfo->XcompID $row['compID'];
    
$CompInfo->Xname $row['name'];
    
$CompInfo->Xadd1 $row['add1'];
    
$CompInfo->Xadd2 $row['add2'];
    
$CompInfo->Xadd3 $row['add3'];
    
$CompInfo->Xpostcode $row['postcode'];
    
$CompInfo->XSize $row['Size'];
    
array_push($CompInfoArray$CompInfo);

}

$xml '<?xml version="1.0"?>';

$xml .= '<root>';

foreach(
$CompInfoArray as $CompInfo) {

    
$xml .= '<Company>';

    
$xml .= '<compID>'.$CompInfo->XcompID.'</compID>';
    
$xml .= '<name>'.XmlEntities($CompInfo->Xname).'</name>';
    
$xml .= '<add1>'.XmlEntities($CompInfo->Xadd1).'</add1>';
    
$xml .= '<add2>'.XmlEntities($CompInfo->Xadd2).'</add2>';
    
$xml .= '<add3>'.XmlEntities($CompInfo->Xadd3).'</add3>';
    
$xml .= '<postcode>'.XmlEntities($CompInfo->Xpostcode).'</postcode>';
    
$xml .= '<Size>'.$CompInfo->XSize.'</Size>';
    
$QueryString "select compt from keywords, relationships where (relationships.comptID = keywords.comptID) and 

(relationships.compID = "
.$CompInfo->XcompID.")";
    
$KeywordQuery mysql_query($QueryString);
    
$KeywordArray = array();
    while(
$row2 mysql_fetch_assoc($KeywordQuery))
        
array_push($KeywordArray$row2['compt']);
    foreach(
$KeywordArray as $Keyword)
        
$xml .= '<compt>'.XmlEntities($Keyword).'</compt>';

    
$xml .= '</Company>';

}

$xml .= '</root>';

echo(
$xml);

include 
'closedb.php';

?>
php guy's Avatar
Computer Specs
Senior Member with 514 posts.
 
Join Date: Dec 2004
Location: Bowlaro Lanes, WA
Experience: That's Mr. Clueless to you
06-Dec-2005, 03:26 PM #4
This doesn't look like a server side error. I'm not an XML expert, but maybe IE's newest build is picky about the XML structure, and requires each field to be on a new line, but this script echos it all on one line. Try adding a \n at the end of each XML statement. If that doesn't work, the error report will be a little more useful in finding the problem.
__________________
if($post) echo $signature;
Bruxy's Avatar
Junior Member with 19 posts.
 
Join Date: May 2005
Location: Manchester, UK
Experience: Advanced
06-Dec-2005, 04:02 PM #5
Quote:
Whitespace is not allowed at this location. Error processing resource 'http://localhost:81/xmloutput3.php'. Line 4, Positi...

<name>Sewells Training & Consultancy</name>
------------------------^

So it doesn't mind the gap before the '&', but it's moans at the one after.

Is it because it's a special character? I though that's what the XmlEntities function was supposed to sort...

Anyway, I'll start looking into it but if you have any ideas let me know. Multi-lining it was suprisingly effective, because the vague error message was really doing my head in, cheers
php guy's Avatar
Computer Specs
Senior Member with 514 posts.
 
Join Date: Dec 2004
Location: Bowlaro Lanes, WA
Experience: That's Mr. Clueless to you
06-Dec-2005, 04:14 PM #6
Ok, the problem is the & sign. Try replacing any "&" character with "&amp;".

A simple str_replace("&", "&amp;", $data); before the "return $escapeddata;" in function XmlEntities should work.
Bruxy's Avatar
Junior Member with 19 posts.
 
Join Date: May 2005
Location: Manchester, UK
Experience: Advanced
06-Dec-2005, 04:21 PM #7
Nope, it doesn't like that. Same error comes up.
php guy's Avatar
Computer Specs
Senior Member with 514 posts.
 
Join Date: Dec 2004
Location: Bowlaro Lanes, WA
Experience: That's Mr. Clueless to you
06-Dec-2005, 04:30 PM #8
Exact same? With an & insteed of an &amp;?

If so, then it isn't replacing it right. Take out the str_replace("&", "&amp;", $data); from the function, then replace this line,
$xml .= '<name>'.XmlEntities($CompInfo->Xname).'</name>';
with this,
$xml .= '<name>'.str_replace("&", "&amp;",XmlEntities($CompInfo->Xname)).'</name>';

If it still doesn't like the &amp;, you might have to replace any & character with text "and".
__________________
if($post) echo $signature;
Bruxy's Avatar
Junior Member with 19 posts.
 
Join Date: May 2005
Location: Manchester, UK
Experience: Advanced
06-Dec-2005, 04:38 PM #9
Huzzah!

You, sir, are a genius

I'd just better hope no more dodgy characters get put in the database because I don't know enough PHP/XML to put in sufficient validation.

Thanks a lot!
php guy's Avatar
Computer Specs
Senior Member with 514 posts.
 
Join Date: Dec 2004
Location: Bowlaro Lanes, WA
Experience: That's Mr. Clueless to you
06-Dec-2005, 04:43 PM #10
No problem.

You might have to add the replace function on the other entries if the "&" shows up in any other parts, but if any other errors show up, we're here.
php guy's Avatar
Computer Specs
Senior Member with 514 posts.
 
Join Date: Dec 2004
Location: Bowlaro Lanes, WA
Experience: That's Mr. Clueless to you
06-Dec-2005, 05:10 PM #11
Oh, and you can mark this thread as solved if you go up near the top and click where it says "Thread Tools" then mark it as solved.
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 12:35 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.