R-如何使用xmlEventParse()获取XML中的特定属性

R-如何使用xmlEventParse()获取XML中的特定属性,r,xml,sax,R,Xml,Sax,我有一个如下所示的XML: <?xml version="1.0" encoding="utf-8"?> <posts> <row Id="1" PostTypeId="1" AcceptedAnswerId="15" CreationDate="2010-07-19T19:12:12.510" Score="27" ViewCount="1647" Body="some text;" OwnerUserId="8"

我有一个如下所示的XML:

<?xml version="1.0" encoding="utf-8"?>
<posts>
  <row Id="1" PostTypeId="1" 
       AcceptedAnswerId="15" CreationDate="2010-07-19T19:12:12.510" Score="27" 
       ViewCount="1647" Body="some text;" OwnerUserId="8" 
       LastActivityDate="2010-09-15T21:08:26.077" 
       Title="title" AnswerCount="5" CommentCount="1" FavoriteCount="17" />
[...]
非常感谢

您可以使用XML库的xpathApply函数在一行中执行相同的操作

查看此代码

library(XML)
a <- xmlParse('<posts>
                <row Id="1" PostTypeId="1" 
              AcceptedAnswerId="15" CreationDate="2010-07-19T19:12:12.510" Score="27" 
              ViewCount="1647" Body="some text;" OwnerUserId="8" 
              LastActivityDate="2010-09-15T21:08:26.077" 
              Title="title" AnswerCount="5" CommentCount="1" FavoriteCount="17" /> </posts>')
xpathApply(a,"/posts/row",xmlGetAttr,"ViewCount")[[1]]
库(XML)
a您可以使用XML库的xpathApply函数在一行中执行相同的操作

查看此代码

library(XML)
a <- xmlParse('<posts>
                <row Id="1" PostTypeId="1" 
              AcceptedAnswerId="15" CreationDate="2010-07-19T19:12:12.510" Score="27" 
              ViewCount="1647" Body="some text;" OwnerUserId="8" 
              LastActivityDate="2010-09-15T21:08:26.077" 
              Title="title" AnswerCount="5" CommentCount="1" FavoriteCount="17" /> </posts>')
xpathApply(a,"/posts/row",xmlGetAttr,"ViewCount")[[1]]
库(XML)

xml2库可以提供比xml库更简单的语法。有一种使用xml2库的替代解决方案:

library(xml2)

doc<-read_xml('<posts>
  <row Id="1" PostTypeId="1" 
              AcceptedAnswerId="15" CreationDate="2010-07-19T19:12:12.510" Score="27" 
              ViewCount="1647" Body="some text;" OwnerUserId="8" 
              LastActivityDate="2010-09-15T21:08:26.077" 
              Title="title" AnswerCount="5" CommentCount="1" FavoriteCount="17" /></posts>')

#find all of the "row" nodes
row<-xml_find_all(doc, "row")

#find attribute of interest in the nodes
xml_attr(row, "ViewCount")
库(xml2)

docxml2库可以提供比xml库更简单的语法。有一种使用xml2库的替代解决方案:

library(xml2)

doc<-read_xml('<posts>
  <row Id="1" PostTypeId="1" 
              AcceptedAnswerId="15" CreationDate="2010-07-19T19:12:12.510" Score="27" 
              ViewCount="1647" Body="some text;" OwnerUserId="8" 
              LastActivityDate="2010-09-15T21:08:26.077" 
              Title="title" AnswerCount="5" CommentCount="1" FavoriteCount="17" /></posts>')

#find all of the "row" nodes
row<-xml_find_all(doc, "row")

#find attribute of interest in the nodes
xml_attr(row, "ViewCount")
库(xml2)

doc在代码周围添加一些上下文将大大改进您的答案。如果因为文件类似于50GB++,我必须使用xmlEventParse()怎么办?我还可以使用xpath吗?是的,因为您的文件大小非常大,所以必须使用xmlEventParse,是的,之后可以应用xpath函数。我几乎尝试过使用如此大的文件,但在其他方面它对我有效。在代码周围添加一些上下文将大大改进您的答案。如果我必须使用xmlEventParse(),因为该文件类似于50GB++,该怎么办?我还可以使用xpath吗?是的,因为您的文件大小非常大,所以必须使用xmlEventParse,是的,之后可以应用xpath函数。我几乎尝试过这样大的文件,但除此之外,它对我有效。