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");
Related
I have just load the xml file and save it immediately but then it become text file.
My XML structure is simple here:
<?xml version="1.0" encoding="UTF-8"?>
<ContentInfo>
<Content>
<PlayerID>P1</PlayerID>
<TVID>TV1</TVID>
<TVStatus>0</TVStatus>
</Content>
</ContentInfo>
Here is my php code:
<script type="text/javascript" src="jquery-2.1.4.js"></script>
<?php
ob_start();
$xml = new DOMDocument('1.0');
$xml->encoding = 'UTF-8';
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->load('TV_Status.xml');
htmlentities($xml->save('TV_Status.xml'));
header("Refresh: 120;url='index.php'");
?>
It automatically changes to text files although it is still ending with xml file and still can be edit.
XML files are text files. There should be no problem: https://en.wikipedia.org/wiki/XML
I just tried it and the new written file looks almost exactly the same as the source file.
XML is text.
You should remove ob_start();. If you mean that the format is not like you want it to be then you should play around with this part
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
a bit.
XML files are text files. Text files with a specific syntax that an application can parse. By default if you send data to the browser, PHP will send an additional information (HTTP header) that the data is HTML. If a browser receives data as HTML it will try to parse and render it. XML syntax uses the a comparable syntax, so the XML tags get parsed as HTML, but ignored as unknown HTML tags. The browser displays the text content of the XML file.
You can change that behavior be sending different content type headers:
Send as XML:
header("Content-Type: application/xml");
readfile('TV_Status.xml');
Send as text:
header("Content-Type: text/plain");
readfile('TV_Status.xml');
You don't need to load and parse an XML file for this, you can just read and pass it to the browser using readfile().
I am new to PHP. I am downloading an XML file from a web service using PHP. I can download the file using this code:
$sourcefile = "http...com?querystring=string";
$destinationfile = 'data\description.xml';
$xml = file_get_contents($sourcefile);
file_put_contents($destinationfile, $xml);
But when I open the XML file, it has < where < should be and > where > should be.
I added this line of code to decode it before saving it to file, which fixes the above problem:
$xml = html_entity_decode($xml);
This doesn't seem to me to be the right way to go about it. Also, I am getting a weird piece of text showing up in the XML file, which prevents me from parsing the XML file:
I tried using str_replace($xml) right before decoding it (and tried it after decoding it), but that wouldn't get rid of it.
What is the correct way to download an XML file using GET from a web service in PHP and will it get rid of that weird string ()?
I want to be able to create a XML File, add nodes and such to it, then output it to the screen without saving.
$bookxml = new DOMDocument('1.0', 'utf-8');
is how i have the XML file created, however i just can't anyway to display the XML file on the screen without saving it.
However i am having a problem with outputting even in save, this is the line i have
echo $bookxml->save("testing.xml");
All this does is return the file size of the newly created XML, and not the contents.
Any help would be awesome, i'm completely stumped on this.
What you're looking for is saveXML, additionally you can use htmlspecialchars to encode the xml so you can see it in your browser display.
echo htmlspecialchars($bookxml->saveXML());
You want the saveXML method, not the save method:
http://us3.php.net/manual/en/domdocument.savexml.php
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>';
I need to generate an editable xml file to supply content to a flash website.
I am generating my file with a html form, and htmlspecialchars e.g.:
$currentItem = htmlspecialchars(stripslashes($currentItem));
This is to prevent xml entries which would produce the error "XML Parsing Error: not well-formed", such as
<entry title="Words & Things">
---------------------^
It has the side effect of making the flash file display the html codes for the content, rather than the proper characters.
Is there a good way to convert the codes back, once they are read into the Flash file (as3)?
Maybe try:
public function htmlUnescape(str:String):String
{
return new XMLDocument(str).firstChild.nodeValue;
}
(Found at: http://www.razorberry.com/blog/archives/2007/11/02/converting-html-entities-in-as3/)
Use html_entity_decode: http://us.php.net/manual/en/function.html-entity-decode.php