asXML() gives error if remove echo, how to solve it? - php

I am using following code to create xml file:
$xml = new SimpleXMLElement('<xml/>');
$track = $xml->addChild('Account');
/* Add Attribute */
$track->addAttribute("name","aa");
$track->addChild('name', 'abc');
$track->addChild('mobile', '989875');
Header('Content-type: text/xml; charset=utf-8');
$filename = 'file/abc.xml';
echo $xml->asXML();
$file = fopen($filename,"w");
fwrite($file,$xml->asXML());
fclose($file);
but if i remove echo from this line echo $xml->asXML(); then it gives me following error:
XML Parsing Error: no element found Line Number 1, Column 1:
and also i am not able to save xml using asXML() function, so i use fwrite, it create the file.
so what is the problem? why it gives the error? why asXML($filename) not save the file?

This error is not from PHP! It's from Firefox. You are sending Header('Content-type: text/xml; charset=utf-8'); so the browser is expecting an XML file to be sent.
When you do echo $xml->asXML();, you are doing just that. The browser then parses the XML file and shows it to you.
Without the echo, you are sending a blank page and telling the browser to parse that as XML. That's why it's saying "no element found".

Related

How to display raw xml file in php to web browser [duplicate]

This question already has answers here:
How to return a file in PHP
(4 answers)
Closed 3 years ago.
Hi i got an xml file and i want to display it on a website.
I tried everything what i found and tried to do it my self but im a newbie to any code language.
My code is this right now:
<?php
header('Content-type: text/xml');
$xml = simplexml_load_file(xmlfile.xml) ;
echo $xml ;
?>
And the output what i see when i go to my website is nothing just a warning about: This XML file does not appear to have any style information associated with it. The document tree is shown below.
But i cant see anything. So please can someone help me write a code that outputs the whole xml file including xml declaration , tags and node values?
You do not have to use the simplexml_load_file: there is another function to read a file, this function is file_get_contents($filename).
Here is a simple code to use:
<?php
// Set the encoding to XML
header('Content-type: text/xml');
// Get contents of the file
$xml = file_get_contents("xmlfile.xml") ;
// Print contents
echo $xml;
?>
I hope it helped you! And sorry for the language mistakes ;)
Try this code. It works for me.
<?php
header("Content-type: text/xml");
$yourFile = "xmlfile.xml";
$file = file_get_contents($yourFile);
echo $file;
If you insist on simple xml you can write like this.
$xml = simplexml_load_file("xmlfile.xml");
echo $xml->asXML();

SimpleXML addChild issue when header sent

I have this code :
$xml = new SimpleXMLElement('<myxml></myxml>');
$xml->addChild('testNode attr="test Attribute"');
$node = $xml->addChild('erroNode attr="My Child node causes error -> expect >"');
//$node->addChild('nodeChild attr="node Child"');
header('Content-type: text/xml');
echo $xml->asXML();
exit();
I can create a childnode with attributes via $xml, but not with $node(child's child), Why? i get the error error on line 2 at column 66: expected '>'
From the docs it say that the addChild function returns a SimpleXmlElement of the child.
Check by uncommenting the commented line $node->addChild('nodeChild attr="node Child"');
Also it only happens when header is sent, if i comment header and do like below i can see the correct xml in page source :
$xml = new SimpleXMLElement('<myxml></myxml>');
$xml->addChild('testNode attr="test Attribute"');
$node = $xml->addChild('erroNode attr="My Child node causes error -> expect >"');
$node->addChild('nodeChild attr="node Child"');
//header('Content-type: text/xml');
echo $xml->asXML();
exit();
My PHP version is 5.4.9
The error you are seeing is not coming from SimpleXML, but from your browser - that's why changing the HTTP header works. With this line, the browser knows the page is XML, and checks that it's valid; without it, it assumes it's HTML, and is more lenient:
header('Content-type: text/xml');
If you use "View Source" in your browser, you'll find that the actual output from PHP is the same in both cases. Another nice test is to set the content-type to text/plain instead, which means the browser won't interpret the output at all, just show it as-is.
So, for some reason, SimpleXML is generating invalid XML. This is because the ->addChild() method takes as its first argument just the name of the element to add, in your case 'erroNode'; you are passing in an invalid name that also includes attributes, which should be added later with ->addAttribute().
If we simplify the example a bit further, and look at the XML generated, we can see what's going on (here's an online demo):
// Make browser show plain output
header('Content-type: text/plain');
// Working example
$xml = new SimpleXMLElement('<myxml></myxml>');
$xml->addChild('testNode attr="test Attribute"');
echo $xml->asXML();
echo "\n";
// Broken example
$xml = new SimpleXMLElement('<myxml></myxml>');
$node = $xml->addChild('testNode attr="test Attribute"');
$node->addChild('test');
echo $xml->asXML();Child('testNode attr="test Attribute"');
$node->addChild('test');
echo $xml->asXML();
This outputs the below:
<?xml version="1.0"?>
<myxml><testNode attr="test Attribute"/></myxml>
<?xml version="1.0"?>
<myxml><testNode attr="test Attribute"><test/></testNode attr="test Attribute"></myxml>
The first version of the XML appears to be doing the right thing, because it has created a "self-closing tag". However, in the second, you can see that SimpleXML thinks that the tag name is 'testNode attr="test Attribute"', not just 'testNode', because that's what we told it.
The result is that it tries to put a closing tag with that "name", and ends up with </testNode attr="test Attribute">, which isn't valid XML.
Arguably, SimpleXML should protect you against this kind of thing, but now that you know, you can easily fix the code (demo):
// Make browser show plain output
header('Content-type: text/plain');
// Fixed example
$xml = new SimpleXMLElement('<myxml></myxml>');
$node = $xml->addChild('testNode');
$node->addAttribute('attr', 'test Attribute');
$node->addChild('test');
echo $xml->asXML();
Now, SimpleXML knows that the tag is just called 'testNode', so can create the correct closing tag when it needs to:
<?xml version="1.0"?>
<myxml><testNode attr="test Attribute"><test/></testNode></myxml>

Getting the content of an xml file creates an XML Parsing Error

I am trying to get the contents of an XML file in case that the file exists or I am generating a new xml file. The problem is that when I am trying to get the xml file I get this error:
XML Parsing Error: no element found Location:
http://mydomain.gr/generate.php Line Number 1, Column 1: ^
My code is this
<?php
include_once('xmlgenerator.php');
$xml = new XmlGenerator();
if($xml->cachefile_exists){
if(!$xml->is_uptodate()){
// echo "update it now";
$xml->createFile = 1;
$data = $xml->create();
Header('Content-type: text/xml');
print($data->asXML());
}else{
//echo "doesn't need any update.";
Header('Content-type: text/xml');
file_get_contents($xml->cached_file);
}
}else{
// echo "Didn't find any cache file. Lets Create it";
$xml->createFile = 1;
$data = $xml->create();
Header('Content-type: text/xml');
print($data->asXML());
}
?>
The XML structure is fine, and I double check about the XML file encoding or the php file that call the XML. Everything is UTF8 without BOM. When I open the XML file directly in a browsers it looks perfect and its a valid file ( checked it with w3c and online tools).
the 2 lines that create the problem(most probably):
Header('Content-type: text/xml');
file_get_contents($xml->cached_file)
I even deleted anything and just used this 2 lines and got the same error.
So what can be wrong? Is there any proper way to include an XML file in a php file, change the headers and show it to the end user? I really don't want to redirect, I need to stay to the same php file just show XML content.
echo file_get_contents($xml->cached_file)

My PHP code to produce XML document is not working right

I want to create/produce XML document from PHP, but not working.
I have this code right here:
<?php
$xml = new SimpleXMLElement("<evalues></evalues>");
while ($row = mysqli_fetch_array($result)){
$evalue = $xml->addChild('evalue', $row[1]);
$evalue->addAttribute('id', $row[0]);
}
echo $xml->saveXML();
?>
the result looks like this, in one long line
1513971901.549931756795512.970022842372553.270163414774046.390384216874570.370032821081734.920144539784.98
The source code looks like this, which is correct, but I want it to look like below in the browser as an XML document without having to view the source code.
<?xml version="1.0"?>
<evalues>
<evalue id="5">1513971901.54993</evalue>
<evalue id="6">1756795512.97002</evalue>
<evalue id="7">2842372553.27016</evalue>
<evalue id="8">3414774046.39038</evalue>
<evalue id="9">4216874570.37003</evalue>
<evalue id="10">2821081734.92014</evalue>
<evalue id="11">4539784.98</evalue>
</evalues>
Right now when i right click on the page and click Save Page As, it shows .htm at the end BUT i want it to show .xml
Just give your client a proper content type:
header('Content-Type: text/xml');
You should set the header content type to text/xml so that the browser could identify that the document is an xml document.
header('Content-Type: text/xml');
You should put this code before anything is outputted to the document. That is before any echo of print statement. In this case you case put the code before the loop.
Since there is no stylesheet attached to your xml document Internet Explorer may still display it as a plain text file. You can see the document as it is using Mozilla Firefox.
If you don't want to actually use the XML (just display it), change your last line to
echo '<pre>', htmlspecialchars($xml->saveXML()), '</pre>';

Output Contents of PHP File for a GET/POST request

I have a script which generates an XML file when the user accesses the link like so:
domain.com/dirList.php
I would like to print the contents of the XML to the user at the end of the POST or GET request (have not implemented this yet). At the moment I am just viewing the link through a web browser.
This is the code I am using to print the file contents.
# Open XML file and print contents
$filename = "test.xml";
$handle = fopen ($filename, "r");
$contents = fread ($handle, filesize ($filename));
fclose ($handle);
print $contents;
The problem is that it doesn't seem to be working. I think I might be either missing a header like this: ("Content-Type: text/xml"); or maybe my code is wrong.
What do I need to do to get the XML file to print to the browser?
You can just do:
header('Content-type: text/xml'); // set the correct MIME type before you print.
readfile('test.xml'); // output the complete file.
//Check there is any extra space before <? and after ?>
//or
header('Content-type: text/xml');
echo file_get_contents($path.$xmlfile);

Categories