将xml解析列表转换为R中的字符

时间:2022-03-21 14:57:52

I have read and parsed a url using htmlParse().

我已经使用htmlParse()读取并解析了一个url。

I have used xpathApply to filter out and get the exact content that I want.

我使用了xpathApply来过滤掉并得到我想要的确切内容。

I want to convert the list created into character. When I try to convert I get this error:

我想把创建的列表转换成字符。当我试图转换时,我得到了这个错误:

"<pointer: 0x000000001673db70>"

I am assuming that the parsed content is pointed in the main object using pointers.

我假设解析后的内容使用指针指向主对象。

The content that I want might have some some XML syntax in it, so xmlValue will do no good.

我想要的内容可能包含一些XML语法,所以xmlValue不会有什么好处。

1 个解决方案

#1


6  

You can use saveXML to convert the internal nodes to characters:

可以使用saveXML将内部节点转换为字符:

library(XML)
appUrl <- 'http://cran.r-project.org/'
doc <- htmlParse(appUrl)
out1 <- xpathSApply(doc, "//*/frame")
out2 <- xpathSApply(doc, "//*/frame", saveXML)
> str(out1)
List of 3
 $ :Classes 'XMLInternalElementNode', 'XMLInternalNode', 'XMLAbstractNode' <externalptr> 
 $ :Classes 'XMLInternalElementNode', 'XMLInternalNode', 'XMLAbstractNode' <externalptr> 
 $ :Classes 'XMLInternalElementNode', 'XMLInternalNode', 'XMLAbstractNode' <externalptr> 
> str(out2)
 chr [1:3] "<frame src=\"logo.html\" name=\"logo\" frameborder=\"0\"/>" ...

#1


6  

You can use saveXML to convert the internal nodes to characters:

可以使用saveXML将内部节点转换为字符:

library(XML)
appUrl <- 'http://cran.r-project.org/'
doc <- htmlParse(appUrl)
out1 <- xpathSApply(doc, "//*/frame")
out2 <- xpathSApply(doc, "//*/frame", saveXML)
> str(out1)
List of 3
 $ :Classes 'XMLInternalElementNode', 'XMLInternalNode', 'XMLAbstractNode' <externalptr> 
 $ :Classes 'XMLInternalElementNode', 'XMLInternalNode', 'XMLAbstractNode' <externalptr> 
 $ :Classes 'XMLInternalElementNode', 'XMLInternalNode', 'XMLAbstractNode' <externalptr> 
> str(out2)
 chr [1:3] "<frame src=\"logo.html\" name=\"logo\" frameborder=\"0\"/>" ...