SlideShare una empresa de Scribd logo
1 de 24
Basic XML Processing In Scala




Neelkanth Sachdeva

Consultant / Software Engineer

Knoldus Software LLP , New Delhi

neelkanthsachdeva.wordpress.com

neelkanth@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. Tags


Text : 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 literals

Scala lets you type in XML as a literal anywhere that an
expression is valid. Simply type a start tag and then continue
writing XML content. The compile will go into an XML-input mode
and will read content as XML until it sees the end tag matching
the start tag you began with.
Important XML Classes

Class Node is the abstract superclass of all
XML node classes.
Class Text is a node holding just text. For
example, 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 apart

Extracting text :
By calling the text method on
any XML node you retrieve all of the text within
that 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 through
sub-sub-elements, etc., by using  instead of
the  operator.
Extracting attributes:
You can extract tag attributes using the same 
and  methods. Simply put an at sign (@) before
the attribute name:
Runtime Representation

XML data is represented as labeled trees.


You can conveniently create such labeled nodes
using 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 the
following 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> talks
XHTML</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 XML

Sometimes we face a situation that there are
multiple kinds of records within the data. In these
kind of scenarios we used to go with pattern
matching on XML.
object XMLTest3 {

  def search(node: scala.xml.Node): String =
    node match {
      case <a>{ contents }</a> => "It's an a
Catagory Item & The Item Is : " + contents
      case <b>{ contents }</b> => "It's as b
Catagory Item & The Item Is : " + contents
      case _ => "It's something else."
    }

    def main(args: Array[String]) {
      println(search(<a>Apple</a>))
       println(search(<b>Mango</b>))
    }
}
Cheers

Más contenido relacionado

La actualidad más candente

La actualidad más candente (19)

JavaScript Objects
JavaScript ObjectsJavaScript Objects
JavaScript Objects
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 JavaScript - Chapter 9 - TypeConversion and Regular Expressions  JavaScript - Chapter 9 - TypeConversion and Regular Expressions
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 
Sql Functions And Procedures
Sql Functions And ProceduresSql Functions And Procedures
Sql Functions And Procedures
 
Xml parsers
Xml parsersXml parsers
Xml parsers
 
3.1 javascript objects_DOM
3.1 javascript objects_DOM3.1 javascript objects_DOM
3.1 javascript objects_DOM
 
"Scala in Goozy", Alexey Zlobin
"Scala in Goozy", Alexey Zlobin "Scala in Goozy", Alexey Zlobin
"Scala in Goozy", Alexey Zlobin
 
JavaScript - Chapter 4 - Types and Statements
 JavaScript - Chapter 4 - Types and Statements JavaScript - Chapter 4 - Types and Statements
JavaScript - Chapter 4 - Types and Statements
 
DOSUG XML Beans overview by Om Sivanesian
DOSUG XML Beans overview by Om SivanesianDOSUG XML Beans overview by Om Sivanesian
DOSUG XML Beans overview by Om Sivanesian
 
JavaScript - Chapter 10 - Strings and Arrays
 JavaScript - Chapter 10 - Strings and Arrays JavaScript - Chapter 10 - Strings and Arrays
JavaScript - Chapter 10 - Strings and Arrays
 
Intro to Javascript
Intro to JavascriptIntro to Javascript
Intro to Javascript
 
JavaScript introduction 1 ( Variables And Values )
JavaScript introduction 1 ( Variables And Values )JavaScript introduction 1 ( Variables And Values )
JavaScript introduction 1 ( Variables And Values )
 
Java 8 Streams
Java 8 StreamsJava 8 Streams
Java 8 Streams
 
2014 holden - databricks umd scala crash course
2014   holden - databricks umd scala crash course2014   holden - databricks umd scala crash course
2014 holden - databricks umd scala crash course
 
JavaScript - Chapter 5 - Operators
 JavaScript - Chapter 5 - Operators JavaScript - Chapter 5 - Operators
JavaScript - Chapter 5 - Operators
 
javascript objects
javascript objectsjavascript objects
javascript objects
 
Introduction to JavaScript Programming
Introduction to JavaScript ProgrammingIntroduction to JavaScript Programming
Introduction to JavaScript Programming
 
Functional Java 8 in everyday life
Functional Java 8 in everyday lifeFunctional Java 8 in everyday life
Functional Java 8 in everyday life
 
Ad java prac sol set
Ad java prac sol setAd java prac sol set
Ad java prac sol set
 
Scala in a nutshell by venkat
Scala in a nutshell by venkatScala in a nutshell by venkat
Scala in a nutshell by venkat
 

Similar a Xml processing in scala

chapter 4 web authoring unit 4 xml.pptx
chapter 4 web authoring  unit 4 xml.pptxchapter 4 web authoring  unit 4 xml.pptx
chapter 4 web authoring unit 4 xml.pptx
amare63
 
XML Presentation-2
XML Presentation-2XML Presentation-2
XML Presentation-2
Sudharsan S
 

Similar a Xml processing in scala (20)

XML
XMLXML
XML
 
Jaxp Xmltutorial 11 200108
Jaxp Xmltutorial 11 200108Jaxp Xmltutorial 11 200108
Jaxp Xmltutorial 11 200108
 
Chapter 18
Chapter 18Chapter 18
Chapter 18
 
Xml and DTD's
Xml and DTD'sXml and DTD's
Xml and DTD's
 
XML
XMLXML
XML
 
xml introduction in web technologies subject
xml introduction in web technologies subjectxml introduction in web technologies subject
xml introduction in web technologies subject
 
Xml writers
Xml writersXml writers
Xml writers
 
Xml
XmlXml
Xml
 
CTDA Workshop on XML and MODS
CTDA Workshop on XML and MODSCTDA Workshop on XML and MODS
CTDA Workshop on XML and MODS
 
XML notes.pptx
XML notes.pptxXML notes.pptx
XML notes.pptx
 
chapter 4 web authoring unit 4 xml.pptx
chapter 4 web authoring  unit 4 xml.pptxchapter 4 web authoring  unit 4 xml.pptx
chapter 4 web authoring unit 4 xml.pptx
 
XML Presentation-2
XML Presentation-2XML Presentation-2
XML Presentation-2
 
The xml
The xmlThe xml
The xml
 
Web data management (chapter-1)
Web data management (chapter-1)Web data management (chapter-1)
Web data management (chapter-1)
 
Xml session
Xml sessionXml session
Xml session
 
Advanced Web Programming Chapter 12
Advanced Web Programming Chapter 12Advanced Web Programming Chapter 12
Advanced Web Programming Chapter 12
 
02 xml schema
02 xml schema02 xml schema
02 xml schema
 
Introduction to xml
Introduction to xmlIntroduction to xml
Introduction to xml
 
XML
XMLXML
XML
 
Introduction to xml schema
Introduction to xml schemaIntroduction to xml schema
Introduction to xml schema
 

Más de Knoldus Inc.

Más de Knoldus Inc. (20)

Supply chain security with Kubeclarity.pptx
Supply chain security with Kubeclarity.pptxSupply chain security with Kubeclarity.pptx
Supply chain security with Kubeclarity.pptx
 
Mastering Web Scraping with JSoup Unlocking the Secrets of HTML Parsing
Mastering Web Scraping with JSoup Unlocking the Secrets of HTML ParsingMastering Web Scraping with JSoup Unlocking the Secrets of HTML Parsing
Mastering Web Scraping with JSoup Unlocking the Secrets of HTML Parsing
 
Akka gRPC Essentials A Hands-On Introduction
Akka gRPC Essentials A Hands-On IntroductionAkka gRPC Essentials A Hands-On Introduction
Akka gRPC Essentials A Hands-On Introduction
 
Entity Core with Core Microservices.pptx
Entity Core with Core Microservices.pptxEntity Core with Core Microservices.pptx
Entity Core with Core Microservices.pptx
 
Introduction to Redis and its features.pptx
Introduction to Redis and its features.pptxIntroduction to Redis and its features.pptx
Introduction to Redis and its features.pptx
 
GraphQL with .NET Core Microservices.pdf
GraphQL with .NET Core Microservices.pdfGraphQL with .NET Core Microservices.pdf
GraphQL with .NET Core Microservices.pdf
 
NuGet Packages Presentation (DoT NeT).pptx
NuGet Packages Presentation (DoT NeT).pptxNuGet Packages Presentation (DoT NeT).pptx
NuGet Packages Presentation (DoT NeT).pptx
 
Data Quality in Test Automation Navigating the Path to Reliable Testing
Data Quality in Test Automation Navigating the Path to Reliable TestingData Quality in Test Automation Navigating the Path to Reliable Testing
Data Quality in Test Automation Navigating the Path to Reliable Testing
 
K8sGPTThe AI​ way to diagnose Kubernetes
K8sGPTThe AI​ way to diagnose KubernetesK8sGPTThe AI​ way to diagnose Kubernetes
K8sGPTThe AI​ way to diagnose Kubernetes
 
Introduction to Circle Ci Presentation.pptx
Introduction to Circle Ci Presentation.pptxIntroduction to Circle Ci Presentation.pptx
Introduction to Circle Ci Presentation.pptx
 
Robusta -Tool Presentation (DevOps).pptx
Robusta -Tool Presentation (DevOps).pptxRobusta -Tool Presentation (DevOps).pptx
Robusta -Tool Presentation (DevOps).pptx
 
Optimizing Kubernetes using GOLDILOCKS.pptx
Optimizing Kubernetes using GOLDILOCKS.pptxOptimizing Kubernetes using GOLDILOCKS.pptx
Optimizing Kubernetes using GOLDILOCKS.pptx
 
Azure Function App Exception Handling.pptx
Azure Function App Exception Handling.pptxAzure Function App Exception Handling.pptx
Azure Function App Exception Handling.pptx
 
CQRS Design Pattern Presentation (Java).pptx
CQRS Design Pattern Presentation (Java).pptxCQRS Design Pattern Presentation (Java).pptx
CQRS Design Pattern Presentation (Java).pptx
 
ETL Observability: Azure to Snowflake Presentation
ETL Observability: Azure to Snowflake PresentationETL Observability: Azure to Snowflake Presentation
ETL Observability: Azure to Snowflake Presentation
 
Scripting with K6 - Beyond the Basics Presentation
Scripting with K6 - Beyond the Basics PresentationScripting with K6 - Beyond the Basics Presentation
Scripting with K6 - Beyond the Basics Presentation
 
Getting started with dotnet core Web APIs
Getting started with dotnet core Web APIsGetting started with dotnet core Web APIs
Getting started with dotnet core Web APIs
 
Introduction To Rust part II Presentation
Introduction To Rust part II PresentationIntroduction To Rust part II Presentation
Introduction To Rust part II Presentation
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
Configuring Workflows & Validators in JIRA
Configuring Workflows & Validators in JIRAConfiguring Workflows & Validators in JIRA
Configuring Workflows & Validators in JIRA
 

Último

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Último (20)

Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 

Xml processing in scala

  • 1. Basic XML Processing In Scala Neelkanth Sachdeva Consultant / Software Engineer Knoldus Software LLP , New Delhi neelkanthsachdeva.wordpress.com neelkanth@knoldus.com
  • 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. XML overview → XML is built out of two basic elements : 1. Text 2. Tags Text : As usual, any sequence of characters. Tags: Consist of a less-than sign,an alphanumeric label, and a greater than sign.
  • 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. XML literals Scala lets you type in XML as a literal anywhere that an expression is valid. Simply type a start tag and then continue writing XML content. The compile will go into an XML-input mode and will read content as XML until it sees the end tag matching the start tag you began with.
  • 6.
  • 7. Important XML Classes Class Node is the abstract superclass of all XML node classes. Class Text is a node holding just text. For example, the “Here” part of <a>Here</a> is of class Text. Class NodeSeq holds a sequence of nodes.
  • 10. Taking XML apart Extracting text : By calling the text method on any XML node you retrieve all of the text within that node, minus any element tags.
  • 11.
  • 12. 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 through sub-sub-elements, etc., by using instead of the operator.
  • 13.
  • 14.
  • 15. Extracting attributes: You can extract tag attributes using the same and methods. Simply put an at sign (@) before the attribute name:
  • 16.
  • 17.
  • 18. Runtime Representation XML data is represented as labeled trees. You can conveniently create such labeled nodes using standard XML syntax. Consider the following XML document:
  • 19. <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 the following Scala program as :
  • 20. 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> talks XHTML</p> </body> </html>; println(page.toString()) }
  • 21. 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) }
  • 22. Pattern matching on XML Sometimes we face a situation that there are multiple kinds of records within the data. In these kind of scenarios we used to go with pattern matching on XML.
  • 23. object XMLTest3 { def search(node: scala.xml.Node): String = node match { case <a>{ contents }</a> => "It's an a Catagory Item & The Item Is : " + contents case <b>{ contents }</b> => "It's as b Catagory Item & The Item Is : " + contents case _ => "It's something else." } def main(args: Array[String]) { println(search(<a>Apple</a>)) println(search(<b>Mango</b>)) } }