2

パッケージ rgexf で .gefx ファイルを作成するために XML に渡す必要がある Web からダウンロードした文字列のベクトルがあります。

問題のある文字列を特定しましたが、いくつかの試行 (以下を参照) の後でも、正規表現でサニタイズする方法がわかりません。4行目で確認できますnodes

library(rgexf)

nodes <- data.frame(matrix(c("1","one",
                             "2","two",
                             "3","three",
                             "4","C//DTD XHTML 1.0 Transitional//EN\"\n    \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\" id=\"sixapart-standard\">\n<head>\n    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8"),
                           ncol=2,byrow=T))

edges <- data.frame(matrix(c("1","2",
                             "3","4",
                             "4","3",
                             "2","1"),
                           ncol=2,byrow=T))

# My attempts to sanitize the string
nodes[,2] <- gsub("<","",nodes[,2])
nodes[,2] <- gsub(">","",nodes[,2])
nodes[,2] <- gsub("&quot","\"",nodes[,2])
nodes[,2] <- gsub("=\"","",nodes[,2])
nodes[,2] <- gsub("EN\"\n","",nodes[,2])

write.gexf(nodes=nodes, edges=edges, output="test.gexf")

xmlビルダーのエラーメッセージは

attributes construct error
Couldn't find end of Start Tag node line 1
Error: 1: attributes construct error
2: Couldn't find end of Start Tag node line 1
4

1 に答える 1

4

XML パッケージを使用して、文字列を適切にエスケープすることができます。

library(XML)
string = "C//DTD XHTML 1.0 Transitional//EN\"\n    \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\" id=\"sixapart-standard\">\n<head>\n    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8"

as.character(XML::xmlTextNode(string))[6]
# [1] "C//DTD XHTML 1.0 Transitional//EN&quot;\n    &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;\n&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xml:lang=&quot;en&quot; lang=&quot;en&quot; id=&quot;sixapart-standard&quot;&gt;\n&lt;head&gt;\n    &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8"

編集:具体的には、

sanitizeForXml <- function (string) {
      string <- as.character(XML::xmlTextNode(string))[6]
    }

vector <- vapply(vector, sanitizeForXml, FUN.VALUE = character(1), USE.NAMES = FALSE)
于 2013-09-04T15:37:50.420 に答える