Gregadeath Posted December 18, 2008 Share Posted December 18, 2008 New to SimpleXML, but I'm finding it very straightforward to use. There's one problem, though - I can't send the XML to a browser. If I do: echo $xml->asXML the XML does indeed print, but this line: <?xml version="1.0" ?> is appearing on the second line. The first line is blank. There are no other echo lines, there's no whitespace outside of the php code, and there's nothing in the document but PHP. So I'm stumped... what are some reasons this might happen? Link to comment https://forums.phpfreaks.com/topic/137605-solved-simplexml-output-is-putting-dtd-on-the-wrong-line/ Share on other sites More sharing options...
tivrfoa Posted December 19, 2008 Share Posted December 19, 2008 New to SimpleXML, but I'm finding it very straightforward to use. There's one problem, though - I can't send the XML to a browser. If I do: echo $xml->asXML the XML does indeed print, but this line: <?xml version="1.0" ?> is appearing on the second line. The first line is blank. There are no other echo lines, there's no whitespace outside of the php code, and there's nothing in the document but PHP. So I'm stumped... what are some reasons this might happen? Sometimes times there are little "dirties" in the code that you can't see depending in what text editor you are using. Post your code and your XML here. Link to comment https://forums.phpfreaks.com/topic/137605-solved-simplexml-output-is-putting-dtd-on-the-wrong-line/#findComment-719521 Share on other sites More sharing options...
Gregadeath Posted December 22, 2008 Author Share Posted December 22, 2008 Away from a computer all weekend. Usually that helps me figure out what the heck's going on, but not this time. I don't doubt your explanation that there is some weird hidden character in there, but even copying and pasting into Notepad doesn't help. FWIW, I'm using Scite, which has never given me any trouble whatsoever. Here's the PHP: <?php header('Content-Type: text/xml'); include("../../zzz/blogConnect.php"); $xmlStr=""; //determine if there is an entry to show in full other than the latest entry if(isset($_GET['viewing'])) { $showMe=$_GET['viewing']; } else { $showMe=0; } $getPosts="SELECT DISTINCT posts.id AS postId, posts.responseTo AS responseTo, posts.approved AS approved, posts.title AS title, posts.content AS content, posts.postdate AS postdate, posts.level AS level, posts.hasChildren AS hasChildren, users.id AS userId, users.handle AS handle FROM posts JOIN users on posts.postedby=users.id WHERE posts.level=0 ORDER BY posts.postdate DESC LIMIT 0, 100;"; $foundPosts=mysql_query($getPosts); ?> <?php //write out XML containing full story for most recent post or selected post $its=0; while ($row=mysql_fetch_assoc($foundPosts)) { $postId=$row['postId']; $title=$row['title']; $approved=$row['approved']; $title=$row['title']; $content=$row['content']; $postdate=$row['postdate']; $userId=$row['userId']; $handle=$row['handle']; $responseTo=$row['responseTo']; $level=$row['level']; $hasChildren=$row['hasChildren']; $xmlStr.="<post>"; //show or hide content if($showMe==$postId) { $xmlStr.="<show>show</show>"; $xmlStr.="<content>$content</content>"; } else if($its==0 && $showMe==0) { $xmlStr.="<show>show</show>"; $xmlStr.="<content>$content</content>"; } else { $xmlStr.="<show>hide</show>"; } //fill out the rest of the content for each entry $xmlStr.="<postId>$postId</postId>"; $xmlStr.="<hasChildren>$hasChildren</hasChildren>"; $xmlStr.="<title>$title</title>"; $xmlStr.="<approved>$approved</approved>"; $xmlStr.="<postdate>$postdate</postdate>"; $xmlStr.="<userId>$userId</userId>"; $xmlStr.="<handle>$handle</handle>"; $xmlStr.="<level>$level</level>"; $xmlStr.="<replyTo>$responseTo</replyTo>"; $xmlStr.="</post>"; $its++; } $toXML = <<<XML <?xml version='1.0' standalone='yes'?><posts>$xmlStr</posts> XML; $xml=new SimpleXMLElement($toXML); echo $xml->asXML(); ?> ...and the output XML, complete with that white space on top that I can't... get... rid... of.... <?xml version="1.0" standalone="yes"?> <posts><post><show>show</show><content>Just checking...</content><postId>11</postId><hasChildren>1</hasChildren><title>Why is my user ID going to respondTo?</title><approved>1</approved><postdate>2008-12-18 11:45:27</postdate><userId>1</userId><handle>ABCStagePass</handle><level>0</level><replyTo>0</replyTo></post><post><show>hide</show><postId>10</postId><hasChildren>0</hasChildren><title>Comic defense fund</title><approved>1</approved><postdate>2008-12-17 14:36:50</postdate><userId>1</userId><handle>ABCStagePass</handle><level>0</level><replyTo>0</replyTo></post><post><show>hide</show><postId>9</postId><hasChildren>0</hasChildren><title>Another longer post</title><approved>1</approved><postdate>2008-12-17 14:27:18</postdate><userId>1</userId><handle>ABCStagePass</handle><level>0</level><replyTo>0</replyTo></post><post><show>hide</show><postId>8</postId><hasChildren>0</hasChildren><title>I am writing a longer post</title><approved>1</approved><postdate>2008-12-17 14:26:39</postdate><userId>1</userId><handle>ABCStagePass</handle><level>0</level><replyTo>0</replyTo></post></posts> Most vexing. I'm about this close to iterating through the XML and cranking it as a string, but I really don't want to do that. Any help would be most appreciated... Link to comment https://forums.phpfreaks.com/topic/137605-solved-simplexml-output-is-putting-dtd-on-the-wrong-line/#findComment-721510 Share on other sites More sharing options...
Gregadeath Posted December 22, 2008 Author Share Posted December 22, 2008 Never mind... while copying and pasting bits and pieces into a test doc, I tracked down the problem. Overlooked something really stupid this time. Closing. Link to comment https://forums.phpfreaks.com/topic/137605-solved-simplexml-output-is-putting-dtd-on-the-wrong-line/#findComment-721521 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.