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: getagrip <getagrip AT web.de>
  • To: xom-interest AT lists.ibiblio.org
  • Subject: Re: [XOM-interest] how to add item to existing node?
  • Date: Mon, 20 Dec 2010 21:49:33 +0100

Not using XPath I can use Elements and add/remove children, but how to I filter the initial Sequence without XPath?

// I would like to filter by @id here, something like this:
val roles = doc.getRootElement.getChildElements("role[@id='boss']")

Sadly, this does not work

On 12/20/2010 08:12 PM, getagrip 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 :-(

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.

How can I add a child node here? Do I have to iterate the whole Document
again using "Element"?
But then I would have to explicitly parse every single node and write it
back to the doc which would be too tedious.

Thanks for hints, getagrip

_______________________________________________
XOM-interest mailing list
XOM-interest AT lists.ibiblio.org
http://lists.ibiblio.org/mailman/listinfo/xom-interest




Archive powered by MHonArc 2.6.24.

Top of Page