Reputation: 15341
I am new to both XML processing and Scala in general. I have few questions/things to achieve based on the example xml below
val xml = <begin>
<definition>
<var>x</var>
<install>new version</install>
</definition>
</begin>
The type of xml
is Elem. Now if I want to search for a given tag/subtags, let's say saying
xml \\ "definition"
I am getting Seq[Node]. What is the difference between an Elem
and this type? Can I get an Elem
as a result of querying for specific tag i.e. get back elem starting at that tag?
My second question is about modifying the XML. is there an easy way to achieve sth of the form: Each value between "install" tag -> substitute with XXXX, the rest should stay the same. Is there a method that allows you to achieve that? Thanks
Upvotes: 1
Views: 125
Reputation: 35443
To understand the difference between Elem (a DOM Element) and Node, check out this link first:
What's the difference between an element and a node in XML?
At a high level a Node is an abstract concept representing a location in an XML document (attribute, text node, element) and an Element is a concrete type of Node represented by something like this;
<install>foo</install>
Now, for the second part of your question, I would focus more on putting dynamic content into a fixed set of XML instead of trying to mutate existing XML. You could do that like so:
def buildXml(version:String) = {
<begin>
<definition>
<var>x</var>
<install>{version}</install>
</definition>
</begin>
}
Upvotes: 1