Discussion:
[XOM-interest] how to add item to existing node?
getagrip
2010-12-20 19:12:29 UTC
Permalink
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
getagrip
2010-12-20 20:49:33 UTC
Permalink
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
Post by getagrip
Hi, I am trying to figure out how to add children to a Node after having
<roles>
<role id="employee">
<user>bob</user>
<user>sally</user>
</role>
<role id="boss"/>
</roles>
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.
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
Elliotte Rusty Harold
2010-12-21 12:14:20 UTC
Permalink
Post by getagrip
Not using XPath I can use Elements and add/remove children, but how to I
filter the initial Sequence without XPath?
Nodes roles = doc.getRootElement().query(""role[@id='boss']"");
--
Elliotte Rusty Harold
elharo at ibiblio.org
Elliotte Rusty Harold
2010-12-21 12:12:37 UTC
Permalink
Post by getagrip
Hi, I am trying to figure out how to add children to a Node after having
<roles>
?<role id="employee">
?<user>bob</user>
?<user>sally</user>
</role>
<role id="boss"/>
</roles>
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.
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)
Post by getagrip
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
Continue reading on narkive:
Loading...