【问题标题】:R xml encountering and dealing with html entities in an xml fileR xml遇到和处理xml文件中的html实体
【发布时间】:2012-06-02 08:54:00
【问题描述】:

Hello R 的 XML 包用户,

我在解析 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 文件时消除这些问题的任何想法

非常感谢您的帮助和想法 希瓦尼

【问题讨论】:

    标签: html xml r xml-parsing html-entities


    【解决方案1】:

    在 XML 包中尝试 readHTML;它有强大的方法可以处理很多这样的情况。另见Scraping html tables into R data frames using the XML package

    【讨论】:

      【解决方案2】:

      如果您只是想删除所有命名的 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)))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-04
        相关资源
        最近更新 更多