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.
Related
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>";
?>
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
I have created a XML file using PHP's simple XML, saved the file. When opening the file in php using fopen and printing the contents. my XML looks like this: (see below)
<?xml version="1.0" encoding="UTF-8"?>
<home><orderList><delivery_cost>0.00</delivery_cost><delivery_surname>TEST</delivery_surname><delivery_postcode>1234</delivery_postcode><status>1</status></orderList></home>
I want the xml file looking all indented and on new lines for each element. Does anybody know how to do this?
Thanks
You can do this using the formatOutput property of DOMDocument.
Save your XML like this instead, presuming your XML is in a variable called $yourXML, and you want to save it to a file at $xmlFilePath:
$dom = new DOMDocument();
$dom->loadXML($yourXML);
$dom->formatOutput = true;
$formattedXML = $dom->saveXML();
$fp = fopen($xmlFilePath,'w+');
fwrite($fp, $formattedXML);
fclose($fp);
Code adapted from here.
This is called "pretty printing" and SimpleXML does not do that. If you search on Stack Overflow and elsewhere on the web you'll find custom solutions that do that.
Pretty printing is good for visulation but I don't recommend saving documents in that format.
If you're still looking for a pretty-printer, you can try SimpleDOM's asPrettyXML()
include 'SimpleDOM.php';
$home = simpledom_load_string('<?xml version="1.0" encoding="UTF-8"?>
<home><orderList><delivery_cost>0.00</delivery_cost><delivery_surname>TEST</delivery_surname><delivery_postcode>1234</delivery_postcode><status>1</status></orderList></home>');
echo $home->asPrettyXML();
echo "\n"; for new line in xml
ob_start(); echo '
' . "\n";?>
I'm using PHP5 to create XML files. I have code like this:
$doc = new DOMDocument();
...
$xml_content = $doc->saveXML();
The problem is that created XML code starts with a root node like this one:
<?xml version="1.0"?>
But I want it to be like this:
<?xml version="1.0" standalone="yes" ?>
I guess I need to call some function on $doc, but I can't figure out which one?
You want to set
$doc->xmlStandalone = true;
It's not a function of the class, it's a property so it's a little harder to find in the docs. You can read about it here.