【发布时间】:2016-07-27 12:12:02
【问题描述】:
我在解析包含非拉丁字符数据的 xml 时遇到问题。例如,我正在尝试解析以下 xml:
<PersonFullName>
<PersonCode>
9999999999999
</PersonCode>
<FirstName>
ANDŽĀRS
</FirstName>
<LastName>
DŽANDĒRĒKĀ
</LastName>
</PersonFullName>
当我使用以下代码时
library(XML)
input <- xmlTreeParse(file = "test.xml", encoding = "UTF-8")
print(input)
我得到以下结果
<?xml version="1.0" encoding="UTF-8"?>
<PersonFullNameVSAA>
<PersonCode>9999999999999
</PersonCode>
<FirstName>ANDŽĀRS
</FirstName>
<LastName>DŽANDĒRĒKĀ
</LastName>
</PersonFullNameVSAA>
xml 以 UTF-8 正确编码。我不知道我还能做些什么来获得正确格式的字符。
【问题讨论】:
-
调用
xmlTreeParse()函数时不指定编码会怎样?喜欢:xmlTreeParse(file = "test.xml")? -
同样的事情发生
-
运行
Sys.getlocale('LC_CTYPE')以查看您的本地编码是什么。如果不是UTF-8,请确保将其设置为UTF-8。如果您已经设置了UTF-8,那么您可能需要将xml文件作为文本导入并使用stringi::stri_conv()将文本从任何编码转换为UTF-8。 -
感谢@Abdou,但没有任何建议有帮助:/