0

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');
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
ali79
  • 23
  • 6
  • Possible duplicate of [CakePHP Xml utility library triggers DOMDocument warning](http://stackoverflow.com/questions/22956330/cakephp-xml-utility-library-triggers-domdocument-warning) – ThW Feb 13 '16 at 22:03

2 Answers2

0

You will have to "escape" it. Try using: &#38; instead of just &.

John
  • 1
  • 13
  • 98
  • 177
Yotam Omer
  • 15,310
  • 11
  • 62
  • 65
  • when I changed it to $issue_resource = $dom->createElement('resource', 'http://localhost/fo/issues.php?jid=1&issueID=155'); $issue_doi_data->appendChild($issue_resource); it displays http://localhost/fo/issues.php?jid=1&issueID=155 – ali79 Feb 13 '16 at 21:44
0

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>
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
  • Thanks for your comments. I want my output to display & instead of & – ali79 Feb 13 '16 at 22:05
  • thanks for your comments. it is working but the closing tag for journal and journal_metadata is not displaying in the output. Please help me resolve. Thanks – ali79 Feb 16 '16 at 21:10
  • xml markup(structure) should always provide the "root" element, which contains all the other elements. If 'journal' is intended as root essence then it should be filled appropriately. Check my update – RomanPerekhrest Feb 16 '16 at 21:30