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);
}
?>
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);
}
?>
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 &.
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 want to append some text to divs which has same class.
$dom = new DOMdocument();
$dom->formatOutput = true;
#$dom->loadHTMLFile('first.html');
$xpath = new DOMXPath($dom)
$after = new DOMText('Newly appended text');
$elements = $xpath->query('//div[#class="mix"]');
foreach($elements as $element)
{
$element->appendChild($after);
//echo $dom->saveHTML();
}
$dom->saveHTMLFile('first.html');
But when I open first.html, The appended text is only appeded to last div of above class.
If I uncomment saveHTML() then it shows perfect result. Just problem after saving.
You cannot append the same DOM node to multiple points in the tree, which is what you are doing here. You need to create a separate (but identical) node each time:
foreach($elements as $element)
{
$after = new DOMText('Newly appended text'); // moved this inside the loop
$element->appendChild($after);
}
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.