I made a code in php. I want a XML output in browser but it shows error. Actually it works fine in locally. But when i hosted it shows error as "XML Parsing Error: junk after document element".
<?php
header("Content-type: text/xml");
echo '<?xml version="1.0" encoding="ISO-8859-1"?>';
echo '<group>';
echo '<family>';
echo '<person>';
echo 'first name';
echo '</person>';
echo '</family>';
echo '</group>';
?>
plese help me.
You have a white-space before your <\?php so before <\?xml
It is causing the error.
Put a die(); after echo '</group>';.
If that clears up your error, then something is being output after the code you posted.
Alternatively you could prepare your XML like:
<?php header("Content-type: text/xml"); ?>
<?xml version="1.0" encoding="ISO-8859-1"?>
<group>
<family>
<person>first name</person>
</family>
</group>
Your script itself works fine in my local environment.
The problem probably comes from somewhere else.
You may have something outputed after your script.
And it is not a white-space before php cause otherwise you would have an error similar to :
XML declaration allowed only at the start of the document
Related
So I've been working on this SIMPLE AS HELL php code and cannot figure out why it will not work. All I'm trying to do is parse some data inside an html file and display it all nice and neat. Here is my xml file
<?xml version="1.0" encoding="UTF-8"?>
<posts>
<post>
<title>Overview</title>
<item>Why are great</item>
<item>Who WonderWidgets</item>
</post>
</posts>
and here is my php inside the html file...
<?php
$posts = simplexml_load_file('/Users/Sam/Desktop/untitled\ html/posts.xml')
echo $posts->post->title;
?>
I have tried everything I can think of...
The echo echoes only
->post->title; ?>
Why is it echoing '?>' ?
Is anyone else having this problem or am I just too stupid to figure out what is wrong here...
Other Information:
It seems as though the variable $posts is empty when I tried this in the command line php.
I cannot occupy it and I have tried to do so with multiple xml files...HELP!
Edit:
I just tried this in my php shell and I get this error:
php > $posts = simplexml_load_file('posts.xml')
php > echo $posts
php > echo $posts->post->title;
Parse error: syntax error, unexpected T_ECHO in php shell code on line 2
My working directory is / and I have the posts.xml stored there.
I tried putting your XML in test.xml and tried your example. It worked fine. I got the output as overview.
Check your path to the file. Check the slashes in the path.
$posts = simplexml_load_file('test.xml');
echo $posts->post->title;
So I have this XML File.
<?xml version="1.0" encoding="iso-8859-1"?>
<user>
<resource>
<reswood>150</reswood>
<resstone>150</resstone>
<ressteel>150</ressteel>
<limit>1000</limit>
</resource>
</user>
And this is the php file.
<?php
session_start();
$userlogin = $_SESSION['username'];
$xml=simplexml_load_file('data/' . $userlogin . '/' . $userlogin . '.xml');
echo $xml->reswood;
echo $xml->resstone;
echo $xml->ressteel;
?>
alright so I have changed to simple xml and now it just displays blank...
I really don't understand the question... "wood tag:" isn't valid PHP and would error out. Unless you wanted the output to actually say "echo" and stuff. But either way, you should really use SimpleXML instead of load to read the XML file, and then you can use XPath to navigate and access the XML data far easier.
Can i use php loops in xml file to list out a list of number incremented file names such as file 1.jpg file2.jpg file3.jpg etc to save me having to manually typing it out.
If so, how do I do this and what doctype etc would the xml file need to have to make it display the loop?
Your question is a little vague, I can't work out if you're trying to read an XML file or write to it.
Assuming you want to read an XML file, and assuming your XML looks a little like this:
<?xml version="1.0" encoding="utf-8" ?>
<rootnode>
<images>
<image>file_1.jpg</image>
<image>file_2.jpg</image>
<image>file_3.jpg</image>
</images>
</rootnode>
you could use SimpleXML to either load the XML file or to load the XML as a string and then return an object to loop through.
http://php.net/manual/en/ref.simplexml.php
Your PHP might look something like this:
if (file_exists('test.xml')) {
$xml = simplexml_load_file('test.xml');
foreach($xml->images AS $image) {
echo $image;
}
} else {
exit('Failed to open test.xml.');
}
What I would do is point to a .php file that outputs XML. All you need to do that is send a header("Content-Type: text/xml"); and make sure you add the at the top of your output.
Here's an example:
<?php
header("Content-Type: text/xml");
print "<?xml version=\"1.0\"?>";
print "<rootNode>";
for(your loop stuff here) {
print "<node>" . $yourDataHere . "</node>";
}
print "</rootNode>";
?>
why wont the following echo out the strings?
<?php
header("Content=type: text/xml");
echo '<?xml version="1.0" ?>';
echo '<strains>';
echo '<strainName>';
echo '</strainName>';
echo '</strains>';
?>
Basically I am experimenting with php and xml and noticed that when i create this script and run it, nothing is outputed to the screen?
The declaration of MIME type is incorrect. It should be:
header('Content-type: text/xml');
Works fine for me.
Open your php.ini and set the option
short_open_tag = Off
After that, restart your server.
I am trying to find out how this would work
For testing purposes, I have made two websites.
One is calling a REST service from the other
I pull the xml data with file_get_contents
if I echo it, I can see a string off data.
But how can I use simpelxml on it, extract data from the nodes themselves?
If I use simplexml_load_file($url), I get some error saying xml declaration only allowed
at the start off the document?
I have this in my testfile
<?php
$url='http://www.woonbel.nl/gps/setgpsloc';
//not working
$xml =simplexml_load_file($url);
print_r($xml);
//just a string
$xml=file_get_contents($url);
echo "<h3>$xml</h3><br>";
?>
this was the xml I send.
I send this from a class file that I included in the top off my php file
if I am sure the webservice is called, maybe that has someting to do with the declaration error?
header('Content-type: text/xml');
echo "<?xml version=\"1.0\"?>\n";
echo "<response>\n";
echo "\t<status>$status_code</status>\n";
echo "\t<fout>Geen</fout>\n";
echo "</response>";
Thanks, Richard
Sounds like there is a blank line at the top of the file, when it should start with the xml declaration. For example:
<?xml version="1.0" encoding="utf-8"?>
Have you got empty lines before the declaration?
Your REST service should be returning XML contents, something like this:
<?xml version="1.0" encoding="utf-8"?>
<results>
<result>
<value awesome="true">LOLCATS</value>
</result>
</results>
You'd then do something along these lines to consume that REST service on the other site:
$xml = simplexml_load_string(file_get_contents('http://example.com/rest.xml'));
foreach($xml->result as $result) {
$value = $result->value;
$awesome = $result->attributes()->awesome;
// do something here with our values and attributes
}
The PHP docs for SimpleXML contain more complicated/real-world examples.
For your specific XML:
$xml = simplexml_load_string(file_get_contents('http://example.com/rest.xml'));
$status = $xml->status;
$fout = $xml->fout;
The error message clearly states that there is a blank, line or character, at the beginning of the xml-data. That could be file-encoding -issue.