XML R-programming: node-specific extraction

I was wondering how to navigate to a specific node using the R XML package. Here is an example of using R built into a dataset, mtcars.

fileName <- system.file("exampleData", "mtcars.xml", package="XML") 
doc <- xmlTreeParse(fileName)
doc$doc$children$dataset

Executing the above code gives me ff. Results:

....
 <record id="Fiat 128">32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1</record>
 <record id="Honda Civic">30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2</record>
 <record id="Toyota Corolla">33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1</record>
 <record id="Toyota Corona">21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1</record>
 <record id="Dodge Challenger">15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2</record>
 <record id="AMC Javelin">15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2</record>
 <record id="Camaro Z28">13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4</record>
 <record id="Pontiac Firebird">19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    
....

I am wondering how to select specific nodes and get their values ​​using xmlAttrs. For example, how to select node: <record id="Fiat 128">or node<record id="Honda Civic">

+5
source share
1 answer
doc <- xmlTreeParse(fileName)
doc <- xmlParse(fileName) 
xpathSApply(doc,"//*/record[@id=\"Fiat 128\"]",xmlValue)
xpathSApply(doc,"//*/record[@id=\"Honda Civic\"]",xmlValue)

use xmlParsewhat is equivalent xmlTreeParse(useInternalNodes=T)for info on xpathsee https://www.w3schools.com/xml/xpath_syntax.asp

+6
source

All Articles