[R] XML and str
maechler at stat.math.ethz.ch
Wed Feb 7 21:02:02 CET 2007
>>>>> "Ashley" == Ashley Ford <ford at signal.QinetiQ.com>
>>>>> on Wed, 07 Feb 2007 17:18:56 +0000 writes:
Ashley> If I read in an .xml file eg with
>> xeg <- xmlTreeParse(system.file("exampleData", "test.xml",
Ashley> It appears to be OK however examining it with str() gives an apparent
>> str(xeg, 2)
Ashley> List of 2
Ashley> $ doc:List of 3
Ashley> ..$ file : list()
Ashley> .. ..- attr(*, "class")= chr [1:2] "XMLComment" "XMLNode"
Ashley> ..$ version :List of 4
Ashley> .. ..- attr(*, "class")= chr "XMLNode"
Ashley> ..$ children:Error in obj$children[[...]] : subscript out of bounds
Ashley> I am unsure if this is a feature or a bug and if the latter whether it
Ashley> is in XML or str, it is not causing a problem but I would like to
Ashley> understand what is happening, any ideas ?
Yes - thank you for providing a well-reproducible example.
options(error = recover)
> obj <- xeg$doc
> mode(obj) # "list"
> is.list(obj) # TRUE
> length(obj) # 3
> obj[] # ---> the error you see above.
Error in obj$children[[...]] : subscript out of bounds
Enter a frame number, or 0 to exit
2: `[[.XMLDocumentContent`(obj, 3)
> obj$children # works, should be identical to obj[]
<element attrib1="my value"/>
This shows that the XML package implements the "[[" method
wrongly IMHO and also inconsistently with the "$" method.
>From a strict OOP view, the XML author could argue that
this is not a bug in XML but rather str() which assumes that
x[[length(x)]] works for objects of mode "list" even when they
are not of *class* "list", but I hope he would still rather
consider changing [[.XMLDocumentContent ...
Ashley> examining components eg
Ashley> List of 2
Ashley> $ comment: list()
Ashley> ..- attr(*, "class")= chr [1:2] "XMLComment" "XMLNode"
Ashley> is OK.
Ashley> XML Version 1.4-1,
Ashley> same behaviour on Windows and Linux, R version 2.4.1 (2006-12-18)
Ashley> The information contained in this E-Mail and any subsequent
Ashley> correspondence is private and is intended solely for the intended
Ashley> recipient(s). The information in this communication may be confidential
Ashley> and/or legally privileged. Nothing in this e-mail is intended to
Ashley> conclude a contract on behalf of QinetiQ or make QinetiQ subject to any
Ashley> other legally binding commitments, unless the e-mail contains an express
Ashley> statement to the contrary or incorporates a formal Purchase Order.
Ashley> For those other than the recipient any disclosure, copying,
Ashley> distribution, or any action taken or omitted to be taken in reliance on
Ashley> such information is prohibited and may be unlawful.
Ashley> Emails and other electronic communication with QinetiQ may be monitored
Ashley> and recorded for business purposes including security, audit and
Ashley> archival purposes. Any response to this email indicates consent to
Ashley> Telephone calls to QinetiQ may be monitored or recorded for quality
Ashley> control, security and other business purposes.
Ashley> QinetiQ Group plc,
Ashley> Company Registration No: 4586941,
Ashley> Registered office: 85 Buckingham Gate, London SW1E 6PD
Ashley> R-help at stat.math.ethz.ch mailing list
Ashley> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
Ashley> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help