【发布时间】:2013-04-24 20:43:07
【问题描述】:
我在使用 xml 时遇到了一些问题。我知道这是一个常见的问题,但我找到的答案并没有解决我的问题。问题是,当我使用 php domdocument 在我的 xml 文件中添加 é 或 ä 或其他特殊字符时,它将 é 保存为 xE9,将 ä 保存为 xE4。我不知道这是否可以,但是当我想显示输出时,它会在这些地方显示问号。 我已经尝试了很多。就像在 php domdocument 的 de xml 标头中删除和添加编码一样。我还尝试使用 file_get_contents 并使用 php utf-8_decode 来获取 xml。我尝试使用 iso intead,但没有解决我的问题。相反,我有时会遇到 php xml 解析错误。我必须做错什么,但怎么办?那是我的问题以及我如何解决这个问题。 我的 xml 文件如下所示: xE9 和 xE4 有黑色背景。
<?xml version="1.0" encoding="UTF-8"?>
<root>
<row id="1">
<question>blah</question>
<answer>blah</answer>
</row>
<row id="2">
<question>xE9</question>
<answer>xE4</answer>
</row>
</root>
和我的 php xml 类的一部分
function __construct($filePath) {
$this->file = $filePath;
$this->label = array('Vraag', 'Antwoord');
$xmlStr = file_get_contents($filePath);
$xmlStr = utf8_decode($xmlStr);
$this->xmlDoc = new DOMDocument('1.0', 'UTF-8');
$this->xmlDoc->preserveWhiteSpace = false;
$this->xmlDoc->formatOutput = true;
//$this->xmlDoc->load($filePath);
$this->xmlDoc->loadXML($xmlStr);
}
这是添加新行功能
//creates new xml row and saves it in xml file
function addNewRow($question, $answer) {
$nextAttr = $this->getNextRowId();
$parentNode = $this->xmlDoc->documentElement;
$rowNode = $this->xmlDoc->createElement('row');
$rowNode = $parentNode->appendChild($rowNode);
$rowNode->setAttribute('id', $nextAttr);
$q = $this->xmlDoc->createElement('question');
$q = $rowNode->appendChild($q);
$qText = $this->xmlDoc->createTextNode($question);
$qText = $q->appendChild($qText);
$a = $this->xmlDoc->createElement('answer');
$a = $rowNode->appendChild($a);
$aText = $this->xmlDoc->createTextNode($answer);
$aText = $a->appendChild($aText);
$this->xmlDoc->save($this->file);
}
在我添加特殊字符之前一切正常。这些显示为问号。
【问题讨论】:
-
您引用了特殊字符,但您的 XML 示例没有任何字符。 它将é保存为xE9,将ä保存为xFC是什么意思。您的 PHP 代码只是显示您将 XML 加载到 DOMDocument 对象中。
-
xml例子只是xml结构的一个例子。正如我在上面解释的那样,如果我将 é 和 ä 添加到文件中,它会将 xE9 和 xFC 保存到 xml 文件中。这是通过我的类中带有 php 函数的 html 输入字段完成的。
-
没有真实的例子你希望如何获得帮助?
-
我编辑我的问题。我希望现在更清楚了。
标签: php xml special-characters decode encode