I have a big XML file which I need to parse with xmlEventParse in R. Unfortunately on-line examples are more complex than I need, and I just want to flag a matching node tag to store the matched node text (not attribute), each text in a separate list, see the comments in the code below:
library(XML)
z <- xmlEventParse(
"my.xml",
handlers = list(
startDocument = function()
{
cat("Starting document\n")
},
startElement = function(name,attr)
{
if ( name == "myNodeToMatch1" ){
cat("FLAG Matched element 1\n")
}
if ( name == "myNodeToMatch2" ){
cat("FLAG Matched element 2\n")
}
},
text = function(text) {
if ( # Matched element 1 .... )
# Store text in element 1 list
if ( # Matched element 2 .... )
# Store text in element 2 list
},
endDocument = function()
{
cat("ending document\n")
}
),
addContext = FALSE,
useTagName = FALSE,
ignoreBlanks = TRUE,
trim = TRUE)
z$ ... # show lists ??
My question is, how to implement this flag in R (in a professional way :)? Plus: What's the best choice to evaluate N arbitrary nodes to match... if name = "myNodeToMatchN" ... nodes avoiding case matching?
my.xml could be just a naive XML like
<A>
<myNodeToMatch1>Text in NodeToMatch1</myNodeToMatch1>
<B>
<myNodeToMatch2>Text in NodeToMatch2</myNodeToMatch2>
...
</B>
</A>