[R] XML and str

Martin Maechler 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",
                                       package="XML"))

    Ashley> It appears to be OK however examining it with str() gives an apparent
    Ashley> error

    >> 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.
After setting  
      options(error = recover)

I do

   > obj <- xeg$doc
   > mode(obj)     # "list"
   [1] "list"
   > is.list(obj)  # TRUE
   [1] TRUE
   > length(obj)   # 3
   [1] 3
   > obj[[3]]      # ---> the error you see above.
   Error in obj$children[[...]] : subscript out of bounds

   Enter a frame number, or 0 to exit   

   1: obj[[3]]
   2: `[[.XMLDocumentContent`(obj, 3)

   Selection: 0

   > obj$children  # works, should be identical to obj[[3]]
   $comment
   <!--A comment-->

   $foo
   <foo x="1">
    <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 ...

Martin

    Ashley> examining components eg 
    >> str(xeg$doc$children,2)

    Ashley> List of 2
    Ashley> $ comment: list()
    Ashley> ..- attr(*, "class")= chr [1:2] "XMLComment" "XMLNode"
    Ashley> etc 

    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> this.

    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> ______________________________________________
    Ashley> R-help at stat.math.ethz.ch mailing list
    Ashley> https://stat.ethz.ch/mailman/listinfo/r-help
    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 mailing list