[R] Removing or overwriting an XML subtree

Steffen Neumann sneumann at ipb-halle.de
Thu Jul 3 23:26:01 CEST 2008

Hi Martin,

thanks for your reply, I'll give it a try. My current problem is 
to map my problem to your suggestion of modifying the tree 
*at parsing time*. 

(Of course) my problem is more complex than my first example,
sorry for any confusion caused.

The XML looks more like this:

      <param name="x" value="a"/>
      <param name="y" value="b"/>
      <more params/>
      <param name="x" value="i"/>
      <param name="y" value="b"/>
      <more params/>
  <more complex>stuff</more complex>

and I have my own set of values for "x",
so I want to use the first <element> as a template 
for my own <element>s, setting the value="" of name="x" 
to my new values. Finally I want to replace the whole <list> 
with my own <list> of my new <element>s.

My number of elements will not correspond 
to the number of <element>s in the template file.

So the handlers would need to read ahead to find the first <element>,
create copies of that for each of my values="" and return 
the new <list> instead of the one in the file.

I might have to read the file twice, first to obtain the <element>
as a template, create my <list> of these new <element>s, and then 
use the handler approach to insert my replacement <list>.

Because there is a lot of <complex stuff/> in there, 
I can't write the XML tree from scratch.
If anyone has suggestions how to parse the file, 
find the template <element>, create a <list> of them,
and finally replace the original <list>, I am all ears ;-)


On Wed, 2008-07-02 at 21:00 -0700, Martin Morgan wrote:
> Hi Steffen...
> Steffen Neumann <sneumann at ipb-halle.de> writes:
> I'm not sure how to do this with internal nodes, but
> handlers = list(
>   first=function(node, ...) {
>       init <- sub("^([[:upper:]]).*$", "\\1.", xmlValue(node))
>       xmlNode("initial", init)
>   })
> xml <- xmlTreeParse("~/tmp/tr.xml", handlers=handlers, asTree=TRUE)
> gives
> > xmlRoot(xml)
> <Duncan>
>  <name a="1" b="xyz">
>   <initial>D.</initial>
>   <last>Temple Lang</last>
>  </name>
> </Duncan>
> The documentation of xmlTreeParse is written to almost imply that this
> would also work with internal nodes, but I was not able to work
> through this.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20080703/cf0dd2fe/attachment.bin>

More information about the R-help mailing list