MacQueen, Don
2014-Dec-13 20:36 UTC
[R] Assistance converting to R a python function that extracts from an XML file
I would appreciate assistance doing in R what a colleague has done in python. Unfortunately (for me), I have almost no experience with either python or xml. Within an xml file there is <CreaDate>20120627</CreaDate><CreaTime>07322600</CreaTime> and I need to extract those two values, 20120627 and 07322600 Here is the short python function. Even without knowing python, it's conceptually clear what it does. I would like to do the same in R. def readxmldate(xmlfile): tree = ET.parse(xmlfile) root = tree.getroot() for lev1 in root.findall('Esri'): xdate = lev1.find('CreaDate').text xtime = lev1.find('CreaTime').text return xdate, xtime Thanks in advance -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062
Duncan Temple Lang
2014-Dec-13 21:06 UTC
[R] Assistance converting to R a python function that extracts from an XML file
Hi Don library(XML) readxmldate = function(xmlfile) { doc = xmlParse(xmlfile) xpathSApply(doc, '//Esri/CreaDate | //Esri/CreaTime', xmlValue) } D. On 12/13/14, 12:36 PM, MacQueen, Don wrote:> I would appreciate assistance doing in R what a colleague has done in > python. Unfortunately (for me), I have almost no experience with either > python or xml. > > Within an xml file there is > <CreaDate>20120627</CreaDate><CreaTime>07322600</CreaTime> > and I need to extract those two values, 20120627 and 07322600 > > > Here is the short python function. Even without knowing python, it's > conceptually clear what it does. I would like to do the same in R. > > def readxmldate(xmlfile): > tree = ET.parse(xmlfile) > root = tree.getroot() > for lev1 in root.findall('Esri'): > xdate = lev1.find('CreaDate').text > xtime = lev1.find('CreaTime').text > return xdate, xtime > > > Thanks in advance > -Don >
Boris Steipe
2014-Dec-13 21:22 UTC
[R] Assistance converting to R a python function that extracts from an XML file
Or ... txt <- "<doc><CreaDate>20120627</CreaDate><CreaTime>07322600</CreaTime></doc>" if (!require(XML)) { install.packages("XML") library(XML) } result <- xmlParse(txt, asText=TRUE) # or ... result <- xmlParse(your-file-here.xml) toString.XMLNode(getNodeSet(result,'//CreaDate/text()')[[1]]) toString.XMLNode(getNodeSet(result,'//CreaTime/text()')[[1]]) B. On Dec 13, 2014, at 4:06 PM, Duncan Temple Lang <dtemplelang at ucdavis.edu> wrote:> Hi Don > > library(XML) > readxmldate = > function(xmlfile) > { > doc = xmlParse(xmlfile) > xpathSApply(doc, '//Esri/CreaDate | //Esri/CreaTime', xmlValue) > } > > D. > > On 12/13/14, 12:36 PM, MacQueen, Don wrote: >> I would appreciate assistance doing in R what a colleague has done in >> python. Unfortunately (for me), I have almost no experience with either >> python or xml. >> >> Within an xml file there is >> <CreaDate>20120627</CreaDate><CreaTime>07322600</CreaTime> >> and I need to extract those two values, 20120627 and 07322600 >> >> >> Here is the short python function. Even without knowing python, it's >> conceptually clear what it does. I would like to do the same in R. >> >> def readxmldate(xmlfile): >> tree = ET.parse(xmlfile) >> root = tree.getroot() >> for lev1 in root.findall('Esri'): >> xdate = lev1.find('CreaDate').text >> xtime = lev1.find('CreaTime').text >> return xdate, xtime >> >> >> Thanks in advance >> -Don >> > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
MacQueen, Don
2014-Dec-15 22:21 UTC
[R] Assistance converting to R a python function that extracts from an XML file
Duncan, Thank you very much. -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 12/13/14, 1:06 PM, "Duncan Temple Lang" <dtemplelang at ucdavis.edu> wrote:>Hi Don > >library(XML) >readxmldate = >function(xmlfile) >{ > doc = xmlParse(xmlfile) > xpathSApply(doc, '//Esri/CreaDate | //Esri/CreaTime', xmlValue) >} > > D. > >On 12/13/14, 12:36 PM, MacQueen, Don wrote: >> I would appreciate assistance doing in R what a colleague has done in >> python. Unfortunately (for me), I have almost no experience with either >> python or xml. >> >> Within an xml file there is >> <CreaDate>20120627</CreaDate><CreaTime>07322600</CreaTime> >> and I need to extract those two values, 20120627 and 07322600 >> >> >> Here is the short python function. Even without knowing python, it's >> conceptually clear what it does. I would like to do the same in R. >> >> def readxmldate(xmlfile): >> tree = ET.parse(xmlfile) >> root = tree.getroot() >> for lev1 in root.findall('Esri'): >> xdate = lev1.find('CreaDate').text >> xtime = lev1.find('CreaTime').text >> return xdate, xtime >> >> >> Thanks in advance >> -Don >> > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.