xmlEventParse(包XML)混乱编码

时间:2013-07-10 10:05:00

标签: xml r character-encoding

我很难阅读带有德语变音符号(ä,ü,ö)和其他特殊字符的XML文件。奇怪的是,当只使用'readLines()'时,R设法正确读入文件。但是,只要函数'get.all.text()'的xmlEventParse部分在处理函数'sE2()','eE2()'和'tS2'的帮助下完成'gather.text'的建立,所有特殊字符都被破坏了。似乎在每个字符之前插入了偶数符号。

XML文件说'?xml version =“1.0”encoding =“iso-8859-1”?'在第一行。不过,我认为这应该不是问题,因为'readLines()'函数正确读入文本。

你们有什么指示我如何继续吗?

谢谢!

get.all.text <- function (file) {
  read.file <- paste(readLines(file), collapse = " ")
  assign("collected.text", c(), env = .GlobalEnv)
  assign("get.text", F, env = .GlobalEnv)
  xmlEventParse(read.file, asText = T, list(startElement = sE2,
                                            endElement = eE2,
                                            text = tS2),
                error = function (...) { },
                saxVersion = 1)
  head(collected.text)
}

sE2 <- function (name, attrs) {
  if (name == "s") {
    get.text <<- T }
}

eE2 <- function (name, attrs) {
  if (name == "s") {
    get.text <<- F
  }
}

tS2 <- function (content, ...) {
  if (get.text & nchar(content) > 0) {
    collected.text <<- c(collected.text, content)
  }
}

0 个答案:

没有答案
相关问题