I'm struggling with package XML to parse a Garmin file (named *.tcx). I wonder if it's form is incomplete, but appreciably reluctant to paste even a shortened version. The output below shows I can get nodes, but an attempt at value of a single node comes up empty (even though there is data there. One question: Has anybody succeeded parsing Garmin .tcx (xml) files? Thanks! Michael _______________________>doc2 = xmlRoot(xmlTreeParse("HR.reduced3.tcx",useInternalNodes = TRUE)) >xpathApply(doc2, "//*", xmlName)[[1]] [1] "TrainingCenterDatabase" [[2]] [1] "Activities" [[3]] [1] "Activity" [[4]] [1] "Id" [[5]] [1] "Lap" [[6]] [1] "TotalTimeSeconds"> xpathApply(doc2, "//TotalTimeSeconds", xmlValue)list()>
Duncan Temple Lang
2011-Mar-30 21:09 UTC
[R] Package XML: Parse Garmin *.tcx file problems
Hi Michael Almost certainly, the problem is that the document has a default namespace. You need to identify the namespace in the XPath query. xpathApply() endeavors to make this simple: xpathApply(doc2, "//x:TotalTimeSeconds", xmlValue, namespaces = "x") I suspect that will give you back something more meaningful. The "x" in the query (x:TotalTimeSeconds) is mapped to x = URI in namespaces and since the URI is not specified, we use the default namespace on the root node of the document. Some documents don't have a default namespace, and then you can use the prefix on the root node corresponding to the namespace of interest. D On 3/30/11 1:15 PM, Folkes, Michael wrote:> I'm struggling with package XML to parse a Garmin file (named *.tcx). > I wonder if it's form is incomplete, but appreciably reluctant to paste > even a shortened version. > The output below shows I can get nodes, but an attempt at value of a > single node comes up empty (even though there is data there. > > One question: Has anybody succeeded parsing Garmin .tcx (xml) files? > Thanks! > Michael > _______________________ > >> doc2 = xmlRoot(xmlTreeParse("HR.reduced3.tcx",useInternalNodes = TRUE)) >> xpathApply(doc2, "//*", xmlName) > [[1]] > [1] "TrainingCenterDatabase" > > [[2]] > [1] "Activities" > > [[3]] > [1] "Activity" > > [[4]] > [1] "Id" > > [[5]] > [1] "Lap" > > [[6]] > [1] "TotalTimeSeconds" > > >> xpathApply(doc2, "//TotalTimeSeconds", xmlValue) > list() >> > > ______________________________________________ > R-help at r-project.org mailing list > 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.