O'Reilly logo

Pragmatic Scala by Venkat Subramaniam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Reading and Writing XML

Once we get an XML document in memory, we know how to parse it. The next step is to figure out a way to get an XML document loaded into the program and to save an in-memory document to a file. As an example, let’s load an XML file that contains symbols and units, increase the units by 1, and store the updated content back into another XML file. Let’s first tackle the step of loading the file.

Here’s a sample file stocks.xml that we’ll use:

UsingScala/stocks.xml
 
<symbols>
 
<symbol​ ticker=​"AAPL"​​>​​<units>​200​</units>​​</symbol>
 
<symbol​ ticker=​"ADBE"​​>​​<units>​125​</units>​​</symbol>
 
<symbol​ ticker=​"ALU"​​>​​<units>​150​</units>​​</symbol>
 
<symbol​ ticker=​"AMD"​​>​​<units>​150​</units>​​</symbol> ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required