【问题标题】:xmlValue with xpathSApply concatenates all leaf values into one characterxmlValue 和 xpathSApply 将所有叶值连接成一个字符
【发布时间】:2014-12-17 04:56:02
【问题描述】:

我正在从如下所示的 xml 文件结构中解析数据:

<Filegram Status="Idle" Version="3.0.0.12">
<CXDVideo>
<ArrayOfDouble xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<double>0.074345000088214874</double>
<double>0.081780999898910522</double>
<double>0.089218996465206146</double>
<double>0.0966539978981018</double>
<double>0.10408999770879746</double>
<double>0.1115259975194931</double>
<double>0.11896199733018875</double>
</ArrayOfDouble>
</CXDVideo>
</Filegram>

使用doc1 &lt;- xmlParse() 导入xml 文件后,我尝试使用以下命令提取数字:

xpathSApply(doc1, "//Video/ArrayOfDouble", xmlValue) 

我将所有值作为单个字符获取:

[[1]]
[1] "00.00623949430882930760.0132457073777914050.0202680919319391250.0306097287684679030.0346436910331249240.0412975363433361050.0483214072883129120.0553476661443710330.0623622909188270570.0693683102726936340.0763849914073944090.083491444587707520.0904

使用 xmlToDataFrame 或 xmlToList 可以很好地解析此部分,但速度很慢。我希望使用 xpathSApply 加快数据提取过程,但无法正常工作。

【问题讨论】:

    标签: xml r xml-parsing


    【解决方案1】:

    那是因为您想要获取每个“双”节点的 xmlValue,而不是整个“ArrayOfDouble”xml 片段。试试

    as.numeric(xpathSApply(doc1, "//Video/ArrayOfDouble/double", xmlValue) )
    

    这也将字符值转换为数值。

    【讨论】:

    • 我需要在 XMLParse 调用成功之前修复 XML 错误(标签不匹配和数据提前结束)。
    • 我已将原始 XML 文件 (3MB) 缩短到相关部分 - 忘记添加/编辑标签。现已修复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-15
    • 2022-12-17
    • 1970-01-01
    • 1970-01-01
    • 2013-03-11
    相关资源
    最近更新 更多