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().
Related
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 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 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 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
I'm using PHP to extract data from a MySQL database. I am able to build an XML file using DOM functions. Then using echo $dom->saveXML(); , I am able to return the XML from an AJAX call. Instead of using AJAX to get the XML, how would I save the XML file to a spot on the server? Thanks
Use the DOMDocument::save() method to save the XML document into a file:
$dom->save('document.xml');
Doesn't DOMDocument::save() help you?
Use PHP XML DOM Parser to create and save XML file. The following code and tutorial would be found from here - Create and Save XML File using PHP
$xmlString = 'Insert XML Content';
$dom = new DOMDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->loadXML($xmlString);
$dom->save('fileName.xml');
Besides "save" option of the DOM itself stated by two previous ansers, you could also use this piece of code:
$strxml = $dom->saveXML();
$handle = fopen("yourxmlfile.xml", "w");
fwrite($handle, $strxml);
fclose($handle);
And you are done.
Remember that the user running your application server (Apache, probably) will need permissions to write in the directory you are placing the XML file.