0

I have this very simple sample xml file

<?xml version="1.0"?>
<root>
<Preferences>
<test attr="cookies"/>
<test attr="cookies"/>
</Preferences>
<Present>
<test2 attr="cake"/>
</Present>
</root>

I am writing an XML Utility and one of the constraints is to take an xPath from the user to be used with scala's XML library. However, I'm having issues with figuring out the practice to both take this in as an argument and to combine it with the xml file I load from. I'm using the scallop library to take in command line arguments. Here is what I have tried so far:

  class CommandLineOptions(args: Array[String]) extends ScallopConf(args){
    banner("XML Tool to Modify Attributes in an XML file")
    val filename = opt[String]("filename", short = 'f', descr = "XML filename" , required = true)
    val xPath = opt[String]("xpath", short = 'x', descr = "XML Xpath" , required = true)

}

def main (args: Array[String]) {
    val opts = new CommandLineOptions(args)
    val xml=XML.loadFile(opts.filename())
    println(xml\opts.xPath())
    println(xml + opts.xPath())
  }

The first print line results in nothing and the second print line results in it appending to the end of all the xml.

When calling this -x option is sent as "\\\Preferences" to follow scala x path syntax and to escape bash constraints.

Zee
  • 1,321
  • 2
  • 18
  • 41
  • possible duplicate of [Is there a way to perform a XPath string query using Scala's XML library?](http://stackoverflow.com/questions/4228149/is-there-a-way-to-perform-a-xpath-string-query-using-scalas-xml-library) – Abel Sep 11 '15 at 14:17

1 Answers1

0

I think this is not trivial to do, but others may answer to the contrary. Since Scala is built on Java, have a look at this solution that shows how you can pass a regular XPath string to a JDOM.

The given solution provides a way to make this a bit more Scala-friendly.

Community
  • 1
  • 1
Abel
  • 56,041
  • 24
  • 146
  • 247