• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Xml processing in scala

Xml processing in scala



Xml processing in scala

Xml processing in scala



Total Views
Views on SlideShare
Embed Views



10 Embeds 1,380

http://blog.knoldus.com 996
http://neelkanthsachdeva.wordpress.com 229
http://www.sukruuzel.com 131
http://www.google.com 8
http://search.yahoo.com 5
http://www.bing.com 4
http://www.soso.com 2
http://yandex.ru 2
http://www.linkedin.com 2
http://boutofcontext.com 1



Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

    Xml processing in scala Xml processing in scala Presentation Transcript

    • Basic XML Processing In ScalaNeelkanth SachdevaConsultant / Software EngineerKnoldus Software LLP , New Delhineelkanthsachdeva.wordpress.comneelkanth@knoldus.com
    • What is XML ?→ XML is a form of semi-structured data.→ It is more structured than plain strings, because it organizes the contents of the data into a tree.→ There are many forms of semi-structured data, but XML is the most widely used.
    • XML overview→ XML is built out of two basic elements : 1. Text 2. TagsText : As usual, any sequence of characters.Tags: Consist of a less-than sign,an alphanumeric label, and a greater than sign.
    • Writing XML Tags● There is a shorthand notation for a start tag followed immediately by its matching end tag.● Simply write one tag with a slash put after the tag’s label. Such a tag comprises an empty element.e.g <pod>Three <peas/> in the </pod>● Start tags can have attributes attached to them.e.g <pod peas="3" strings="true"/>
    • XML literalsScala lets you type in XML as a literal anywhere that anexpression is valid. Simply type a start tag and then continuewriting XML content. The compile will go into an XML-input modeand will read content as XML until it sees the end tag matchingthe start tag you began with.
    • Important XML ClassesClass Node is the abstract superclass of allXML node classes.Class Text is a node holding just text. Forexample, the “Here” part of<a>Here</a> is of class Text.Class NodeSeq holds a sequence of nodes.
    • Evaluating Scala Code
    • Example of XML
    • Taking XML apartExtracting text :By calling the text method onany XML node you retrieve all of the text withinthat node, minus any element tags.
    • Extracting sub-elements :If you want to find a sub-element by tag name,simply call with the name of the tag:You can do a “deep search” and look throughsub-sub-elements, etc., by using instead ofthe operator.
    • Extracting attributes:You can extract tag attributes using the same and methods. Simply put an at sign (@) beforethe attribute name:
    • Runtime RepresentationXML data is represented as labeled trees.You can conveniently create such labeled nodesusing standard XML syntax.Consider the following XML document:
    • <html> <head> <title>Hello XHTML world</title> </head> <body> <h1>Hello world</h1> <p><a href="http://scala-lang.org/">Scala</a> talks XHTML</p> </body> </html>This document can be created by thefollowing Scala program as :
    • object XMLTest1 extends Application { val page = <html> <head> <title>Hello XHTML world</title> </head> <body> <h1>Hello world</h1> <p><a href="scala-lang.org">Scala</a> talksXHTML</p> </body> </html>; println(page.toString())}
    • It is possible to mix Scala expressions and XML :object XMLTest2 extends Application { import scala.xml._ val df = java.text.DateFormat.getDateInstance val dateString = df.format(new java.util.Date) def theDate(name: String) = <dateMsg addressedTo={ name }> Hello, { name }! Today is { dateString } </dateMsg>; println(theDate("Neelkanth Sachdeva").toString)}
    • Pattern matching on XMLSometimes we face a situation that there aremultiple kinds of records within the data. In thesekind of scenarios we used to go with patternmatching on XML.
    • object XMLTest3 { def search(node: scala.xml.Node): String = node match { case <a>{ contents }</a> => "Its an aCatagory Item & The Item Is : " + contents case <b>{ contents }</b> => "Its as bCatagory Item & The Item Is : " + contents case _ => "Its something else." } def main(args: Array[String]) { println(search(<a>Apple</a>)) println(search(<b>Mango</b>)) }}
    • Cheers