Replacing XML node with Anti-XML

I am trying to replace an XML element with another using the anti-xml library. For example, I have:

<root> <sub> <keep /> <replace /> <keeptoo /> </sub> </root> 

and fragment:

 <inserted key="value"> <foo>foo</foo> <bar>bar</bar> </inserted> 

I would like to create:

 <root> <sub> <keep /> <inserted key="value"> <foo>foo</foo> <bar>bar</bar> </inserted> <keeptoo /> </sub> </root> 

Note: The save order <sub> must be saved.

+4
source share
2 answers

First we define the root document:

 val root = <root> <sub> <keep /> <replace /> <keeptoo /> </sub> </root>.convert val inserted = <inserted key="value"> <foo>foo</foo> <bar>bar</bar> </inserted>.convert 

then we get the element:

 val replace = root \\ 'replace 

and finally we get xml with the updated <replace/> node:

 replace.updated(0, inserted).unselect 

if we get multiple <replace/> nodes, we can iterate over replace to update each node.

+3
source

You can replace selected elements with multiple nodes using flatMap , for example. replace.flatMap(_ => someListOfNodes).unselect

(Sorry for making a separate answer, it seems that I cannot comment on the existing ones.)

+2
source

Source: https://habr.com/ru/post/1382133/


All Articles