My php code generate xml file but the line that has the ampersand in the url is not displaying. Below is the php code
$dom = new DOMDocument('1.0','UTF-8');
$dom->formatOutput = true;
$root = $dom->createElement('journal');
$dom->appendChild($root);
$journal_metadata = $dom->createElement('journal_metadata');
$dom->appendChild($journal_metadata);
$issue_doi = $dom->createElement('doi', '11');
$issue_doi_data->appendChild($issue_doi);
$issue_resource = $dom->createElement('resource', 'http://localhost/fo/issues.php?jid=1&issueID=155');
$issue_doi_data->appendChild($issue_resource);
echo '<xmp>'. $dom->saveXML() .'</xmp>';
$dom->save('result.xml') or die('XML Create Error');
The line that has "url with ampersand" isn't displaying because $issue_doi_data variable which should contain that url was not declared and not appended to the initial document $dom.
Secondly, in case of getting warning message "unterminated entity reference" you may use htmlentities() (or htmlspecialchars()) for escaping supplied value.
Change your code as shown below:
$dom = new \DOMDocument('1.0','UTF-8');
$dom->formatOutput = true;
$root = $dom->createElement('journal');
$dom->appendChild($root);
$journal_metadata = $dom->createElement('journal_metadata', '...');
$root->appendChild($journal_metadata);
// modify this line with your prefered name and value
$issue_doi_data = $dom->createElement('doi_data', '');
$issue_doi = $dom->createElement('doi', '11');
$issue_doi_data->appendChild($issue_doi);
$issue_resource = $dom->createElement('resource', htmlspecialchars('http://localhost/fo/issues.php?jid=1&issueID=155'));
$issue_doi_data->appendChild($issue_resource);
$root->appendChild($issue_doi_data);
// save xml into file
$dom->save('result.xml') or die('XML Create Error');
// outputting xml file content
echo '<xmp>'. html_entity_decode(file_get_contents('result.xml')) .'</xmp>';
// the output:
<?xml version="1.0" encoding="UTF-8"?>
<journal>
<journal_metadata>...</journal_metadata>
<doi_data>
<doi>11</doi>
<resource>http://localhost/fo/issues.php?jid=1&issueID=155</resource>
</doi_data>
</journal>
You will have to "escape" it. Try using: & instead of just &.
Related
I'm trying add some data to an existing XML file using PHP's SimpleXML. The problem is it adds all the data in a single line:
<name>blah</name><class>blah</class><area>blah</area> ...
And so on. All in a single line. How to introduce line breaks?
How do I make it like this?
<name>blah</name>
<class>blah</class>
<area>blah</area>
I am using asXML() function.
Thanks.
You could use the DOMDocument class to reformat your code:
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($simpleXml->asXML());
echo $dom->saveXML();
Gumbo's solution does the trick. You can do work with simpleXml above and then add this at the end to echo and/or save it with formatting.
Code below echos it and saves it to a file (see comments in code and remove whatever you don't want):
//Format XML to save indented tree rather than one line
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($simpleXml->asXML());
//Echo XML - remove this and following line if echo not desired
echo $dom->saveXML();
//Save XML to file - remove this and following line if save not desired
$dom->save('fileName.xml');
Use dom_import_simplexml to convert to a DomElement. Then use its capacity to format output.
$dom = dom_import_simplexml($simple_xml)->ownerDocument;
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
echo $dom->saveXML();
As Gumbo and Witman answered; loading and saving an XML document from an existing file (we're a lot of newbies around here) with DOMDocument::load and DOMDocument::save.
<?php
$xmlFile = 'filename.xml';
if( !file_exists($xmlFile) ) die('Missing file: ' . $xmlFile);
else
{
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dl = #$dom->load($xmlFile); // remove error control operator (#) to print any error message generated while loading.
if ( !$dl ) die('Error while parsing the document: ' . $xmlFile);
echo $dom->save($xmlFile);
}
?>
I'm trying add some data to an existing XML file using PHP's SimpleXML. The problem is it adds all the data in a single line:
<name>blah</name><class>blah</class><area>blah</area> ...
And so on. All in a single line. How to introduce line breaks?
How do I make it like this?
<name>blah</name>
<class>blah</class>
<area>blah</area>
I am using asXML() function.
Thanks.
You could use the DOMDocument class to reformat your code:
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($simpleXml->asXML());
echo $dom->saveXML();
Gumbo's solution does the trick. You can do work with simpleXml above and then add this at the end to echo and/or save it with formatting.
Code below echos it and saves it to a file (see comments in code and remove whatever you don't want):
//Format XML to save indented tree rather than one line
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($simpleXml->asXML());
//Echo XML - remove this and following line if echo not desired
echo $dom->saveXML();
//Save XML to file - remove this and following line if save not desired
$dom->save('fileName.xml');
Use dom_import_simplexml to convert to a DomElement. Then use its capacity to format output.
$dom = dom_import_simplexml($simple_xml)->ownerDocument;
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
echo $dom->saveXML();
As Gumbo and Witman answered; loading and saving an XML document from an existing file (we're a lot of newbies around here) with DOMDocument::load and DOMDocument::save.
<?php
$xmlFile = 'filename.xml';
if( !file_exists($xmlFile) ) die('Missing file: ' . $xmlFile);
else
{
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dl = #$dom->load($xmlFile); // remove error control operator (#) to print any error message generated while loading.
if ( !$dl ) die('Error while parsing the document: ' . $xmlFile);
echo $dom->save($xmlFile);
}
?>
I am having some trouble mixing PHP with XML.
I currently have a PHP file that takes variables from the URL string and I need to calculate something based on these variables, and then return the output in an XTML format.
I currently have my main config file that links to my xml generating file:
include('Xml.php');
$x = new xml();
$x->generate();
And my XML generating file is as follows:
<?php
Class XML {
public function generate() {
$doc = new DOMDocument('1.0');
$doc->formatOutput = true;
$root = $doc->createElement('conv');
$root = $doc->appendChild($root);
$at = $doc->createElement('at');
$at = $root->appendChild($at);
$text = $doc->createTextNode("hi");
$text = $at->appendChild($text);
echo $doc->saveXML();
}
}
?>
But this doesn't work - what am I doing wrong here - I know it's probaby obvious but I am new to XML and can't seem to get it working!
Should I be doing it differently? If so ... how?
I've just tested your class with following code:
$xml = new XML();
$xml->generate();
And I've got this result:
[vyktor#grepfruit tmp]$ php test.php
<?xml version="1.0"?>
<conv>
<at>hi</at>
</conv>
So your class works just fine and your error is somewhere else, eg. including wrong file.
Turn on error_reporting and paste errors in comment.
Hi I am generating a xml file in php but getting error
XML Parsing Error: XML or text declaration not at start of entity
My code is---
<?php require_once('../../settings.php');
header("Content-type:text/xml");
$dom=new DOMDocument('1.0');
$dom->formatOutput = true;
$id=(int)$_GET['imageid'];
$query="select * from tbl_image_gallery where imageId='$id' ORDER BY gallId DESC ";
$select=mysql_query($query);
$content = $dom->appendChild($dom->createElement('content'));
while($res=mysql_fetch_array($select))
{
$image = $content->appendChild($dom->createElement('image'));
$small_image_path = $image->appendChild($dom->createElement('small_image_path'));
$small_image_path->appendChild($dom->createTextNode("image_gallery/load/images/small/".$res['image']));
$big_image_path = $image->appendChild($dom->createElement('big_image_path'));
$big_image_path->appendChild($dom->createTextNode("image_gallery/load/images/big/".$res['image']));
$description = $image->appendChild($dom->createElement('description'));
$description->appendChild($dom->createCDATASection($res['description']));
}
echo $test1 = $dom->saveXML();
?>
After search on google i found that it is problem related to space before
I remove space but not getting expected result.
Remove below line and try if the error message has gone Then surely you have space or something else outputted before in the settings.php file.
header("Content-type:text/xml");
Check settings.php file and remove the spaces if you have. Note that if settings.php file is also including some other files you have to check space in those file also.
Use ob_clean(); before echo $test1 = $dom->saveXML();
I'm trying add some data to an existing XML file using PHP's SimpleXML. The problem is it adds all the data in a single line:
<name>blah</name><class>blah</class><area>blah</area> ...
And so on. All in a single line. How to introduce line breaks?
How do I make it like this?
<name>blah</name>
<class>blah</class>
<area>blah</area>
I am using asXML() function.
Thanks.
You could use the DOMDocument class to reformat your code:
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($simpleXml->asXML());
echo $dom->saveXML();
Gumbo's solution does the trick. You can do work with simpleXml above and then add this at the end to echo and/or save it with formatting.
Code below echos it and saves it to a file (see comments in code and remove whatever you don't want):
//Format XML to save indented tree rather than one line
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($simpleXml->asXML());
//Echo XML - remove this and following line if echo not desired
echo $dom->saveXML();
//Save XML to file - remove this and following line if save not desired
$dom->save('fileName.xml');
Use dom_import_simplexml to convert to a DomElement. Then use its capacity to format output.
$dom = dom_import_simplexml($simple_xml)->ownerDocument;
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
echo $dom->saveXML();
As Gumbo and Witman answered; loading and saving an XML document from an existing file (we're a lot of newbies around here) with DOMDocument::load and DOMDocument::save.
<?php
$xmlFile = 'filename.xml';
if( !file_exists($xmlFile) ) die('Missing file: ' . $xmlFile);
else
{
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dl = #$dom->load($xmlFile); // remove error control operator (#) to print any error message generated while loading.
if ( !$dl ) die('Error while parsing the document: ' . $xmlFile);
echo $dom->save($xmlFile);
}
?>