Skip to Content.
Sympa Menu

xom-interest - Re: [XOM-interest] how to add item to existing node?

xom-interest AT lists.ibiblio.org

Subject: XOM API for Processing XML with Java

List archive

Chronological Thread  
  • From: Elliotte Rusty Harold <elharo AT ibiblio.org>
  • To: XOM API for Processing XML with Java <xom-interest AT lists.ibiblio.org>
  • Subject: Re: [XOM-interest] how to add item to existing node?
  • Date: Tue, 21 Dec 2010 07:12:37 -0500

On Mon, Dec 20, 2010 at 2:12 PM, getagrip <getagrip AT web.de> wrote:
> Hi, I am trying to figure out how to add children to a Node after having
> used the Node.query() method:
>
> <roles>
>  <role id="employee">
>  <user>bob</user>
>  <user>sally</user>
> </role>
> <role id="boss"/>
> </roles>
>
> Here's the query:
>
> val roles = doc.query("/roles/role")
>
> val role = roles.get(0)
> val users = role.query("user")
> users.get(0).detach // bob is removed
>
> Now I would like to ADD a node/element to the <role id="boss"/> Node.
> Something like this:
>
> roles.get(1).attach(new Element("user").addChild("myself"))
>
> Appart from having a detach() method there is no attach()-method though :-(

There is an appendChild method though:

roles.get(1).appendChild(new Element("user").appendChild("myself"))

or some such (not sure exactly what you want but should be something like
that)

> Also I would need to have a ParentNode to add a child but in my case as you
> can see the <role id="boss"/> node is empty.

Only because you haven't added anything yet. You can append a child to
an empty element, just as you might add an item to an empty list, and
then it won;t be empty any more. :-)


--
Elliotte Rusty Harold
elharo AT ibiblio.org




Archive powered by MHonArc 2.6.24.

Top of Page