Scala - XML inspection/modification

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

Answers


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>
}

Need Your Help

Understanding Alfresco Web Quick Start publishing

alfresco alfresco-share

For a better understanding of how things works, I am trying to build my own web site within Alfresco by replicating the same (hopefully) workflow publishing mechanism, but it's not working: I get n...

Mixing Android widget old & new themes

android themes android-edittext android-4.0

I'm starting my second Android app and find that I would like to mostly use HOLO dark theme for app.