2012-05-27 25 views
1

Hello R的XML包用户,R xml遇到和处理xml文件中的html实体

我在解析XML时遇到了一个奇怪的错误。它与解析XML文件时遇到像mdash和ndash这样的HTML实体有关。

这是我使用的代码:

InText = readLines(xmlFileName,n=-1) 
Text = xmlValue(xmlRoot(xmlTreeParse(InText,trim=FALSE))) 

我目前消除这些实体,如MDASH和ndash使用以下

InText = gsub("\\&mdash"," ",InText); 
InText = gsub("\\&ndash"," ",InText); 

但可以很乏味,因为我看到名单可能的HTML.4.0实体列表。

我如何能消除这些在解析XML文件

非常感谢您的帮助和想法 Shivani

回答

1

如果你只是想删除所有名为HTML实体,使用正则表达式的任何想法:

library("XML") 

InText <- "<html>\ 
<head>\ 
    <title>Test &amp; Test again</title>\ 
</head>\ 
    <body>Hello &nbsp; world</body>\ 
</html>" 

InText <- gsub("\\&[^;]+;","",InText) 

Text <- xmlValue(xmlRoot(xmlTreeParse(InText,trim=FALSE)))