val template = <root>
<component>
<mother-board>
<manufacturer>
<name>
</name
</manufacturer>
<price>
555
</price>
<chipset>
Intel
</chipset>
</mother-board>
</component>
</root>
val xPaths = Map("root/component/mother-board/manufacturer/name" -> "Asus")
val output = <root>
<component>
<mother-board>
<manufacturer>
<name>
Asus
</name>
</manufacturer>
<price>
555
</price>
<chipset>
Intel
</chipset>
</mother-board>
</component>
</root>
The real template is over 250 lines and the number of xpaths that need to be populated is over 70. Any suggestions regarding libraries or any other approach that can accomplish this is welcome. Thank you.
Update to add info about my approach
What I have done so far that does not work:
I create XML based on XPaths available. But if the order in which XPaths are processed changes, the XML changes too which is not acceptable to my use case.
object Main extends App {
val props = Map(
"a/b/c" -> "val1",
"a/b/d/f" -> "val3",
"a/b/d/e" -> "val2"
)
println(props.keys.foldLeft(<root/>: Node)((acc, current) => {
create(Elem(acc.prefix, acc.label, acc.attributes, acc.scope, true, acc.child: _*), current.split("/").toList, current, props)
}))
def create(node: Elem, path: List[String], completePath: String, propMap: Map[String, String]): Node = {
if(path.size > 0) {
val current = path.head
node.child.filter(x => x.label.equals(current)).headOption.map(childToTraverse => {
val newChild = create(Elem(null, childToTraverse.label, childToTraverse.attributes, childToTraverse.scope, false, childToTraverse.child: _*), path.tail, completePath, propMap)
node.copy(child = node.child.filter(x => !x.label.equals(current)) ++ newChild)
}).getOrElse({
val newNode = Elem(null, current, node.attributes, node.scope, false)
val newChild = create(newNode, path.tail, completePath, propMap)
node.copy(child = node.child ++ newChild)
})
}
else {
node.copy(child = new Text(propMap.get(completePath).getOrElse("Value Not Available")))
}
}
}
Problem:
For input:
"a/b/c" -> "val1",
"a/b/d/f" -> "val3",
"a/b/d/e" -> "val2"
It generates:
<root>
<a>
<b>
<c>
val1
</c>
<d>
<f>
val3
</f>
<e>
val2
</e>
</d>
</b>
</a>
</root>
For input:
"a/b/c" -> "val1",
"a/b/d/e" -> "val2",
"a/b/d/f" -> "val3"
It generates:
<root>
<a>
<b>
<c>
val1
</c>
<d>
<e>
val2
</e>
<f>
val3
</f>
</d>
</b>
</a>
</root>
Notice the difference in two XMLs generated from identical XPaths processed in different order.
I know the correct order. So I wanted to have template and change the template based on XPaths instead of generating the XML from scratch.