Basic XML Processing In ScalaNeelkanth SachdevaConsultant / Software EngineerKnoldus Software LLP , New Delhineelkanthsach...
What is XML ?→ XML is a form of semi-structured data.→ It is more structured than plain strings, because it organizes the ...
XML overview→ XML is built out of two basic elements :   1. Text   2. TagsText : As usual, any sequence of characters.Tags...
Writing XML Tags●    There is a shorthand notation for a start tag    followed immediately by its matching end tag.●    Si...
XML literalsScala lets you type in XML as a literal anywhere that anexpression is valid. Simply type a start tag and then ...
Important XML ClassesClass Node is the abstract superclass of allXML node classes.Class Text is a node holding just text. ...
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, m...
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 ...
Extracting attributes:You can extract tag attributes using the same and  methods. Simply put an at sign (@) beforethe attr...
Runtime RepresentationXML data is represented as labeled trees.You can conveniently create such labeled nodesusing standar...
<html>    <head>      <title>Hello XHTML world</title>    </head>    <body>      <h1>Hello world</h1>      <p><a href="htt...
object XMLTest1 extends Application {  val page =  <html>    <head>      <title>Hello XHTML world</title>    </head>    <b...
It is possible to mix Scala expressions and XML :object XMLTest2 extends Application {  import scala.xml._  val df = java....
Pattern matching on XMLSometimes we face a situation that there aremultiple kinds of records within the data. In thesekind...
object XMLTest3 {  def search(node: scala.xml.Node): String =    node match {      case <a>{ contents }</a> => "Its an aCa...
Cheers
Xml processing in scala
Xml processing in scala
Xml processing in scala
Xml processing in scala
Xml processing in scala
Xml processing in scala
Upcoming SlideShare
Loading in...5
×

Xml processing in scala

3,029

Published on

Xml processing in scala

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,029
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
23
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Xml processing in scala

  1. 1. Basic XML Processing In ScalaNeelkanth SachdevaConsultant / Software EngineerKnoldus Software LLP , New Delhineelkanthsachdeva.wordpress.comneelkanth@knoldus.com
  2. 2. 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.
  3. 3. 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.
  4. 4. 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"/>
  5. 5. 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.
  6. 6. 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.
  7. 7. Evaluating Scala Code
  8. 8. Example of XML
  9. 9. Taking XML apartExtracting text :By calling the text method onany XML node you retrieve all of the text withinthat node, minus any element tags.
  10. 10. 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.
  11. 11. Extracting attributes:You can extract tag attributes using the same and methods. Simply put an at sign (@) beforethe attribute name:
  12. 12. Runtime RepresentationXML data is represented as labeled trees.You can conveniently create such labeled nodesusing standard XML syntax.Consider the following XML document:
  13. 13. <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 :
  14. 14. 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())}
  15. 15. 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)}
  16. 16. 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.
  17. 17. 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>)) }}
  18. 18. Cheers
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×