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>';
Related
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();
I'm extracting data from MySQL into flash. First I use PHP to query the DB, and then I echo the results.
$sql = "SELECT path FROM video WHERE id = 52";
$resource = mysqli_query($conn,$sql);
$row = mysqli_fetch_assoc($resource);
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
echo "<video>\n";
echo "<path>" . $row['path'] . "</path>\n";
echo "</video>\n";
I only have one root element, and I don't see anything wrong with my markup following the root element...but then again I'm wrong and I'm sure there is an error.
--- Changed my Title so I added the error message here ---
TypeError: Error #1088: The markup in the document following the root element must be well-formed.
------EDITED ------
The value of $row['path'] is a string from my DB. I know it's valid because I deleted all xml tags and echoed $row['path'] which is the url path for the video. I also used gettype(), and it outputs a string.
When I open this file into my browser, I actually don't see anything. It's a blank page, but it's because the browser is rendering it as html elements, so I don't think that's the problem. But when I view source, I get this
<?xml version="1.0" encoding="UTF-8"?>
<video>
<path>1.mp4</path>
</video>
In addition, I created a random file with an xml extension with the xml contents above, (copied it form View Source that my php file outputs) and it works fine. I see the contents in Flash. What could be wrong with echoing it out in a php file? I'm using textWrangler. It might have something to do with BOM, any suggestions?
----EDIT NUMBER TWO ---
Here's my actionscript code. I'm still new so I didn't put it in a class but inside the first frame in my actions layer. I'm just trying to get the basics to work.
var theXML:XML;
var xmlReq:URLRequest = new URLRequest("../folderExample/poop.php");
var xmlLoader:URLLoader = new URLLoader();
function xmlLoaded(event:Event):void{
theXML = new XML(xmlLoader.data); // The Error occurs here.
trace(theXML.toXMLString());
}
xmlLoader.load(xmlReq);
xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
From http://www.flashdevelop.org/community/viewtopic.php?f=13&t=3128
For well-formed XML returning
TypeError: Error #1088: The markup in the document following the root element must be well-formed,
often the problem is the BOM.
Actionscript XML does not strip the BOM (Byte Order Mark) from incoming text. The BOM is an invisible character (#65279) created at the beginning of a ASP or php "echo" string, as well as some text editors, to indicate the byte-ordering of multibyte text.
You can confirm if your string has this BOM character like this:
trace( sXMLString.charCodeAt( 0 ) ); //outputs 65279
The quick solution is
sXMLString= sXMLString.replace( String.fromCharCode(65279), "" ) ;
myXML = new XML(sXMLString);
See also:
Reading php generated XML in flash?
How to load PHP dynamically generated XML in FLASH
If you don't have it already, from the second link above, try adding:
header("Content-Type: text/xml");
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>
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)
I really am trying to code from PHP to have a XML file. I got the code that can display items from PHPmyadmin. but how can I display those things in XML file is really my problem, I tried all the codes. I could find. But, still no luck.
While generating xml your first line in php file should be
header ("content-type: text/xml; charset=utf-8");
then use echo statement & print whatever you want within xml tags like
echo "<status>0</status>";
just follow xml standerds & test using IE will be better.