Re: [R] Removing or overwriting an XML subtree

From: Steffen Neumann <sneumann_at_ipb-halle.de>
Date: Thu, 03 Jul 2008 23:26:01 +0200

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:

<root>

  <complex>stuff</complex>
  <list>
    <element>

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

    </element>

    <element>
      <param name="x" value="i"/>
      <param name="y" value="b"/>
      <more params/>

    </element>
  <list>
 <more complex>stuff</more complex>
</root>

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 ;-)

Yours,
Steffen

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.


______________________________________________

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.
Received on Thu 03 Jul 2008 - 21:29:43 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 03 Jul 2008 - 21:32:10 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive