我的php代码生成xml文件,但url中有与号的行没有显示。下面是 php 代码
$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');
具有
">带 & 符号的 url"的行未显示$issue_doi_data
因为应包含该 url 的变量未声明且未附加到初始文档$dom
。
其次,如果收到警告消息">未终止的实体引用",您可以使用htmlentities()
(或htmlspecialchars()
(来转义提供的值。
更改代码,如下所示:
$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>
你将不得不"逃避"它。尝试使用:&
而不仅仅是&
。