SlideShare a Scribd company logo
Scala – XML Power Play -Part 1
By VulcanMinds
Creating XML content
Scala treats XML as ‘first class citizen’. Scala provides easy syntax for creating XML content for
e.g.
scala> var someXML = <candidate><name> Manish </name> <title> Consultant</title> <city> Pune </city></candidate>
someXML: scala.xml.Elem = <candidate><name> Manish </name> <title> Consultant</title> <city> Pune </city></candidate>
scala> var employee = <Employee><ID> 456789</ID> <Name> Mark Holland </Name><AGE>34</AGE></Employee>
employee: scala.xml.Elem = <Employee><ID> 456789</ID> <Name> Mark Holland </Name><AGE>34</AGE></Employee>
You can save the XML content to a file easily like below. You must import XML module in the REPL (console) for this example.
scala> import scala.xml._;
import scala.xml._
scala> XML.save("sample.xml", employee );
Also you can load XML directly from an XML file like below.
scala> val xmlFromFile = XML.loadFile("sample.xml");
xmlFromFile: scala.xml.Elem = <candidate><name> Manish </name> <title> Scala Consultant</title> <city> Pune </city></candidate>
Searching in XML…..
Scala provides easy syntax for searching in the XML content
scala> var employee = <Employee><ID> 456789</ID> <Name> Mark Holland </Name><AGE>34</AGE></Employee>
employee: scala.xml.Elem = <Employee><ID> 456789</ID> <Name> Mark Holland </Name><AGE>34</AGE></Employee>
scala> var name = employee  "Name" text
name: String = " Mark Holland “
scala> var age = employee  "age" text // String searches are case sensitive
age: String = ""
scala> var age = employee  "AGE" text
age: String = 34
Using wild card character ‘_’ will get everything within <employee> tag
scala> var age = employee  "_"
age: scala.xml.NodeSeq = NodeSeq(<ID> 456789</ID>, <Name> Mark Holland </Name>, <AGE>34</AGE>)
For deep diving search in the XML jungle which finds tags deep inside the XML jungle you can use ‘’ operator like below….
Note that <CITY> … </CITY> tag is inside the <ADDRESS> </ADDRESS> tag.
scala> var employee = <Employee><ID> 456789</ID> <Name> Mark Holland </Name><AGE>34</AGE><ADDRESS><CITY>New
York</CITY></ADDRESS></Employee>
employee: scala.xml.Elem = <Employee><ID> 456789</ID> <Name> Mark Holland </Name><AGE>34</AGE><ADDRESS><CITY>New
York</CITY></ADDRESS></Employee>
scala> var city = employee  “CITY“
city: scala.xml.NodeSeq = NodeSeq(<CITY>New York</CITY>)
Processing XML…..
Scala provides elegant syntax for processing content in XML tags…..
For e.g. For the example members.xml file
<Members>
<Member> <Name> THOMAS D'SILVA </Name>
<Address>123 PARK AVENUE, GRANT ROAD </Address>
<CITY> MUMBAI </CITY>
<COUNTRY>INDIA </COUNTRY>
</Member>
<Member> <Name> ALVIN NAZARETH</Name>
<Address> 524 ELEPHANT GRASS,BEDFORD DRIVE,</Address>
<CITY>SASKATEN </CITY>
<COUNTRY>INDIA </COUNTRY>
</Member>
<Member> <Name>ANTONIO ALVAREZ</Name>
<Address>123 PARK AVENUE GRANT ROAD </Address>
<CITY>MUMBAI</CITY>
<COUNTRY>INDIA</COUNTRY>
</Member>
<Member> <Name>JONAS WHITTAKER</Name>
<Address>B20345 ANDROMEDA HOUSE, PARAMOUNT ESTATE, </Address>
<CITY>TIMBUKTU</CITY>
<COUNTRY>EGYPTIA</COUNTRY>
</Member>
</Members>
Processing XML…..
Following code prints the names of members from India.
scala> var members=XML.loadFile("members2.xml")
members: scala.xml.Elem =
<Members>
<Member> <Name> THOMAS D'SILVA </Name>
<Address>123 PARK AVENUE, GRANT ROAD </Address>
<CITY>MUMBAI</CITY>
<COUNTRY>INDIA</COUNTRY>
………
……….
</Members>
scala> for(val aMember <- members  "Member") {
if( (aMember  "COUNTRY").text.trim.equals("INDIA") ) {
println((aMember "Name").text);
}
}
THOMAS D'SILVA
ALVIN NAZARETH
ANTONIO ALVAREZ
Processing XML…..
Following code prints all the cities the members are from ….
scala> var cities = members ”CITY”
cities: scala.xml.NodeSeq =
NodeSeq(<CITY> MUMBAI </CITY>,
<CITY> SASKATEN</CITY>,
<CITY> MUMBAI</CITY>,
<CITY>TIMBUKTU</CITY>)
Following code prints the total number of members
scala> var memberElements = members ”Member”
…………..
scala> println(memberElements.length)
4
Following code finds the first and the last member in the sequence
scala> println(memberElements.first)
<Member> <Name> THOMAS D'SILVA </Name>
<Address>123 PARK AVENUE, GRANT ROAD </Address>
<CITY> MUMBAI</CITY>
<COUNTRY> INDIA </COUNTRY>
</Member>
scala> println(memberElements.last)
<Member> <Name> JONAS WHITTAKER</Name>
<Address> B20345 ANDROMEDA HOUSE,
PARAMOUNT ESTATE, </Address>
<CITY> TIMBUKTU</CITY>
<COUNTRY> EGYPTIA</COUNTRY>
</Member>
Processing XML…..
Following code gets a slice of member nodes from all nodes with “from” index including to “toIndex” excluding.
scala> println(memberElements.slice(0,2))
<Member> <Name> THOMAS D'SILVA </Name>
<Address>123 PARK AVENUE, GRANT ROAD </Address>
<CITY> MUMBAI</CITY>
<COUNTRY> INDIA </COUNTRY>
</Member>
<Member> <Name> ALVIN NAZARETH</Name>
<Address> 524 ELEPHANT GRASS, BEDFORD DRIVE,</Address>
<CITY> SASKATEN </CITY>
<COUNTRY> INDIA</COUNTRY>
</Member>

More Related Content

Similar to Scala xml power play part 1 (20)

XML Business Rules Validation with Schematron
XML Business Rules Validation with SchematronXML Business Rules Validation with Schematron
XML Business Rules Validation with Schematron
Emiel Paasschens
 
treeview
treeviewtreeview
treeview
tutorialsruby
 
treeview
treeviewtreeview
treeview
tutorialsruby
 
Xml overview
Xml overviewXml overview
Xml overview
Saran Yuwanna
 
Transforming xml with XSLT
Transforming  xml with XSLTTransforming  xml with XSLT
Transforming xml with XSLT
Malintha Adikari
 
Xml 215-presentation
Xml 215-presentationXml 215-presentation
Xml 215-presentation
Simsima Tchakma
 
Xsd
XsdXsd
Xsd
xavier john
 
Xsd
XsdXsd
Xsd
prathap kumar
 
Xml processing in scala
Xml processing in scalaXml processing in scala
Xml processing in scala
Neelkanth Sachdeva
 
Xml processing in scala
Xml processing in scalaXml processing in scala
Xml processing in scala
Knoldus Inc.
 
XML notes.pptx
XML notes.pptxXML notes.pptx
XML notes.pptx
AmarYa2
 
The Ebook Developer's Toolbox - ebookcraft 2016 - Sanders Kleinfeld
The Ebook Developer's Toolbox - ebookcraft 2016 - Sanders Kleinfeld The Ebook Developer's Toolbox - ebookcraft 2016 - Sanders Kleinfeld
The Ebook Developer's Toolbox - ebookcraft 2016 - Sanders Kleinfeld
BookNet Canada
 
Big Data processing with Spark, Scala or Java?
Big Data processing with Spark, Scala or Java?Big Data processing with Spark, Scala or Java?
Big Data processing with Spark, Scala or Java?
Erik-Berndt Scheper
 
Basics of XML
Basics of XMLBasics of XML
Basics of XML
indiangarg
 
distributed system concerned lab sessions
distributed system concerned lab sessionsdistributed system concerned lab sessions
distributed system concerned lab sessions
milkesa13
 
XSD Incomplete Overview Draft
XSD Incomplete Overview DraftXSD Incomplete Overview Draft
XSD Incomplete Overview Draft
Pedro De Almeida
 
[Km] [wp] [3] [assignment]
[Km] [wp] [3] [assignment][Km] [wp] [3] [assignment]
[Km] [wp] [3] [assignment]
Azam Charaniya
 
Xml part1
Xml part1  Xml part1
Xml part1
NOHA AW
 
SQL/XML on Oracle
SQL/XML on OracleSQL/XML on Oracle
SQL/XML on Oracle
torp42
 
XML-Motor
XML-MotorXML-Motor
XML-Motor
Abhishek Kumar
 
XML Business Rules Validation with Schematron
XML Business Rules Validation with SchematronXML Business Rules Validation with Schematron
XML Business Rules Validation with Schematron
Emiel Paasschens
 
Transforming xml with XSLT
Transforming  xml with XSLTTransforming  xml with XSLT
Transforming xml with XSLT
Malintha Adikari
 
Xml processing in scala
Xml processing in scalaXml processing in scala
Xml processing in scala
Knoldus Inc.
 
XML notes.pptx
XML notes.pptxXML notes.pptx
XML notes.pptx
AmarYa2
 
The Ebook Developer's Toolbox - ebookcraft 2016 - Sanders Kleinfeld
The Ebook Developer's Toolbox - ebookcraft 2016 - Sanders Kleinfeld The Ebook Developer's Toolbox - ebookcraft 2016 - Sanders Kleinfeld
The Ebook Developer's Toolbox - ebookcraft 2016 - Sanders Kleinfeld
BookNet Canada
 
Big Data processing with Spark, Scala or Java?
Big Data processing with Spark, Scala or Java?Big Data processing with Spark, Scala or Java?
Big Data processing with Spark, Scala or Java?
Erik-Berndt Scheper
 
distributed system concerned lab sessions
distributed system concerned lab sessionsdistributed system concerned lab sessions
distributed system concerned lab sessions
milkesa13
 
XSD Incomplete Overview Draft
XSD Incomplete Overview DraftXSD Incomplete Overview Draft
XSD Incomplete Overview Draft
Pedro De Almeida
 
[Km] [wp] [3] [assignment]
[Km] [wp] [3] [assignment][Km] [wp] [3] [assignment]
[Km] [wp] [3] [assignment]
Azam Charaniya
 
Xml part1
Xml part1  Xml part1
Xml part1
NOHA AW
 
SQL/XML on Oracle
SQL/XML on OracleSQL/XML on Oracle
SQL/XML on Oracle
torp42
 

More from VulcanMinds (8)

Dig up the gold in your godowns
Dig up the gold in your godownsDig up the gold in your godowns
Dig up the gold in your godowns
VulcanMinds
 
Mongo DB in Health Care Part 1
Mongo DB in Health Care Part 1Mongo DB in Health Care Part 1
Mongo DB in Health Care Part 1
VulcanMinds
 
Designing a play framework application
Designing a play framework applicationDesigning a play framework application
Designing a play framework application
VulcanMinds
 
Scala case of case classes
Scala   case of case classesScala   case of case classes
Scala case of case classes
VulcanMinds
 
Tupple ware in scala
Tupple ware in scalaTupple ware in scala
Tupple ware in scala
VulcanMinds
 
Scala elegant and exotic part 1
Scala  elegant and exotic part 1Scala  elegant and exotic part 1
Scala elegant and exotic part 1
VulcanMinds
 
Data choreography in mongo
Data choreography in mongoData choreography in mongo
Data choreography in mongo
VulcanMinds
 
Munching the mongo
Munching the mongoMunching the mongo
Munching the mongo
VulcanMinds
 
Dig up the gold in your godowns
Dig up the gold in your godownsDig up the gold in your godowns
Dig up the gold in your godowns
VulcanMinds
 
Mongo DB in Health Care Part 1
Mongo DB in Health Care Part 1Mongo DB in Health Care Part 1
Mongo DB in Health Care Part 1
VulcanMinds
 
Designing a play framework application
Designing a play framework applicationDesigning a play framework application
Designing a play framework application
VulcanMinds
 
Scala case of case classes
Scala   case of case classesScala   case of case classes
Scala case of case classes
VulcanMinds
 
Tupple ware in scala
Tupple ware in scalaTupple ware in scala
Tupple ware in scala
VulcanMinds
 
Scala elegant and exotic part 1
Scala  elegant and exotic part 1Scala  elegant and exotic part 1
Scala elegant and exotic part 1
VulcanMinds
 
Data choreography in mongo
Data choreography in mongoData choreography in mongo
Data choreography in mongo
VulcanMinds
 
Munching the mongo
Munching the mongoMunching the mongo
Munching the mongo
VulcanMinds
 

Recently uploaded (20)

AI Trends - Mary Meeker
AI Trends - Mary MeekerAI Trends - Mary Meeker
AI Trends - Mary Meeker
Razin Mustafiz
 
Talk: On an adventure into the depths of Maven - Kaya Weers
Talk: On an adventure into the depths of Maven - Kaya WeersTalk: On an adventure into the depths of Maven - Kaya Weers
Talk: On an adventure into the depths of Maven - Kaya Weers
Kaya Weers
 
Supercharge Your AI Development with Local LLMs
Supercharge Your AI Development with Local LLMsSupercharge Your AI Development with Local LLMs
Supercharge Your AI Development with Local LLMs
Francesco Corti
 
SDG 9000 Series: Unleashing multigigabit everywhere
SDG 9000 Series: Unleashing multigigabit everywhereSDG 9000 Series: Unleashing multigigabit everywhere
SDG 9000 Series: Unleashing multigigabit everywhere
Adtran
 
Cyber Security Legal Framework in Nepal.pptx
Cyber Security Legal Framework in Nepal.pptxCyber Security Legal Framework in Nepal.pptx
Cyber Security Legal Framework in Nepal.pptx
Ghimire B.R.
 
cloudgenesis cloud workshop , gdg on campus mita
cloudgenesis cloud workshop , gdg on campus mitacloudgenesis cloud workshop , gdg on campus mita
cloudgenesis cloud workshop , gdg on campus mita
siyaldhande02
 
Fully Open-Source Private Clouds: Freedom, Security, and Control
Fully Open-Source Private Clouds: Freedom, Security, and ControlFully Open-Source Private Clouds: Freedom, Security, and Control
Fully Open-Source Private Clouds: Freedom, Security, and Control
ShapeBlue
 
Dr Jimmy Schwarzkopf presentation on the SUMMIT 2025 A
Dr Jimmy Schwarzkopf presentation on the SUMMIT 2025 ADr Jimmy Schwarzkopf presentation on the SUMMIT 2025 A
Dr Jimmy Schwarzkopf presentation on the SUMMIT 2025 A
Dr. Jimmy Schwarzkopf
 
Offshore IT Support: Balancing In-House and Offshore Help Desk Technicians
Offshore IT Support: Balancing In-House and Offshore Help Desk TechniciansOffshore IT Support: Balancing In-House and Offshore Help Desk Technicians
Offshore IT Support: Balancing In-House and Offshore Help Desk Technicians
john823664
 
What is DePIN? The Hottest Trend in Web3 Right Now!
What is DePIN? The Hottest Trend in Web3 Right Now!What is DePIN? The Hottest Trend in Web3 Right Now!
What is DePIN? The Hottest Trend in Web3 Right Now!
cryptouniversityoffi
 
TrustArc Webinar: Mastering Privacy Contracting
TrustArc Webinar: Mastering Privacy ContractingTrustArc Webinar: Mastering Privacy Contracting
TrustArc Webinar: Mastering Privacy Contracting
TrustArc
 
Let’s Get Slack Certified! 🚀- Slack Community
Let’s Get Slack Certified! 🚀- Slack CommunityLet’s Get Slack Certified! 🚀- Slack Community
Let’s Get Slack Certified! 🚀- Slack Community
SanjeetMishra29
 
Evaluation Challenges in Using Generative AI for Science & Technical Content
Evaluation Challenges in Using Generative AI for Science & Technical ContentEvaluation Challenges in Using Generative AI for Science & Technical Content
Evaluation Challenges in Using Generative AI for Science & Technical Content
Paul Groth
 
Kubernetes Cloud Native Indonesia Meetup - May 2025
Kubernetes Cloud Native Indonesia Meetup - May 2025Kubernetes Cloud Native Indonesia Meetup - May 2025
Kubernetes Cloud Native Indonesia Meetup - May 2025
Prasta Maha
 
Gihbli AI and Geo sitution |use/misuse of Ai Technology
Gihbli AI and Geo sitution |use/misuse of Ai TechnologyGihbli AI and Geo sitution |use/misuse of Ai Technology
Gihbli AI and Geo sitution |use/misuse of Ai Technology
zainkhurram1111
 
Building Agents with LangGraph & Gemini
Building Agents with LangGraph &  GeminiBuilding Agents with LangGraph &  Gemini
Building Agents with LangGraph & Gemini
HusseinMalikMammadli
 
Contributing to WordPress With & Without Code.pptx
Contributing to WordPress With & Without Code.pptxContributing to WordPress With & Without Code.pptx
Contributing to WordPress With & Without Code.pptx
Patrick Lumumba
 
Agentic AI Explained: The Next Frontier of Autonomous Intelligence & Generati...
Agentic AI Explained: The Next Frontier of Autonomous Intelligence & Generati...Agentic AI Explained: The Next Frontier of Autonomous Intelligence & Generati...
Agentic AI Explained: The Next Frontier of Autonomous Intelligence & Generati...
Aaryan Kansari
 
Droidal: AI Agents Revolutionizing Healthcare
Droidal: AI Agents Revolutionizing HealthcareDroidal: AI Agents Revolutionizing Healthcare
Droidal: AI Agents Revolutionizing Healthcare
Droidal LLC
 
UiPath Community Zurich: Release Management and Build Pipelines
UiPath Community Zurich: Release Management and Build PipelinesUiPath Community Zurich: Release Management and Build Pipelines
UiPath Community Zurich: Release Management and Build Pipelines
UiPathCommunity
 
AI Trends - Mary Meeker
AI Trends - Mary MeekerAI Trends - Mary Meeker
AI Trends - Mary Meeker
Razin Mustafiz
 
Talk: On an adventure into the depths of Maven - Kaya Weers
Talk: On an adventure into the depths of Maven - Kaya WeersTalk: On an adventure into the depths of Maven - Kaya Weers
Talk: On an adventure into the depths of Maven - Kaya Weers
Kaya Weers
 
Supercharge Your AI Development with Local LLMs
Supercharge Your AI Development with Local LLMsSupercharge Your AI Development with Local LLMs
Supercharge Your AI Development with Local LLMs
Francesco Corti
 
SDG 9000 Series: Unleashing multigigabit everywhere
SDG 9000 Series: Unleashing multigigabit everywhereSDG 9000 Series: Unleashing multigigabit everywhere
SDG 9000 Series: Unleashing multigigabit everywhere
Adtran
 
Cyber Security Legal Framework in Nepal.pptx
Cyber Security Legal Framework in Nepal.pptxCyber Security Legal Framework in Nepal.pptx
Cyber Security Legal Framework in Nepal.pptx
Ghimire B.R.
 
cloudgenesis cloud workshop , gdg on campus mita
cloudgenesis cloud workshop , gdg on campus mitacloudgenesis cloud workshop , gdg on campus mita
cloudgenesis cloud workshop , gdg on campus mita
siyaldhande02
 
Fully Open-Source Private Clouds: Freedom, Security, and Control
Fully Open-Source Private Clouds: Freedom, Security, and ControlFully Open-Source Private Clouds: Freedom, Security, and Control
Fully Open-Source Private Clouds: Freedom, Security, and Control
ShapeBlue
 
Dr Jimmy Schwarzkopf presentation on the SUMMIT 2025 A
Dr Jimmy Schwarzkopf presentation on the SUMMIT 2025 ADr Jimmy Schwarzkopf presentation on the SUMMIT 2025 A
Dr Jimmy Schwarzkopf presentation on the SUMMIT 2025 A
Dr. Jimmy Schwarzkopf
 
Offshore IT Support: Balancing In-House and Offshore Help Desk Technicians
Offshore IT Support: Balancing In-House and Offshore Help Desk TechniciansOffshore IT Support: Balancing In-House and Offshore Help Desk Technicians
Offshore IT Support: Balancing In-House and Offshore Help Desk Technicians
john823664
 
What is DePIN? The Hottest Trend in Web3 Right Now!
What is DePIN? The Hottest Trend in Web3 Right Now!What is DePIN? The Hottest Trend in Web3 Right Now!
What is DePIN? The Hottest Trend in Web3 Right Now!
cryptouniversityoffi
 
TrustArc Webinar: Mastering Privacy Contracting
TrustArc Webinar: Mastering Privacy ContractingTrustArc Webinar: Mastering Privacy Contracting
TrustArc Webinar: Mastering Privacy Contracting
TrustArc
 
Let’s Get Slack Certified! 🚀- Slack Community
Let’s Get Slack Certified! 🚀- Slack CommunityLet’s Get Slack Certified! 🚀- Slack Community
Let’s Get Slack Certified! 🚀- Slack Community
SanjeetMishra29
 
Evaluation Challenges in Using Generative AI for Science & Technical Content
Evaluation Challenges in Using Generative AI for Science & Technical ContentEvaluation Challenges in Using Generative AI for Science & Technical Content
Evaluation Challenges in Using Generative AI for Science & Technical Content
Paul Groth
 
Kubernetes Cloud Native Indonesia Meetup - May 2025
Kubernetes Cloud Native Indonesia Meetup - May 2025Kubernetes Cloud Native Indonesia Meetup - May 2025
Kubernetes Cloud Native Indonesia Meetup - May 2025
Prasta Maha
 
Gihbli AI and Geo sitution |use/misuse of Ai Technology
Gihbli AI and Geo sitution |use/misuse of Ai TechnologyGihbli AI and Geo sitution |use/misuse of Ai Technology
Gihbli AI and Geo sitution |use/misuse of Ai Technology
zainkhurram1111
 
Building Agents with LangGraph & Gemini
Building Agents with LangGraph &  GeminiBuilding Agents with LangGraph &  Gemini
Building Agents with LangGraph & Gemini
HusseinMalikMammadli
 
Contributing to WordPress With & Without Code.pptx
Contributing to WordPress With & Without Code.pptxContributing to WordPress With & Without Code.pptx
Contributing to WordPress With & Without Code.pptx
Patrick Lumumba
 
Agentic AI Explained: The Next Frontier of Autonomous Intelligence & Generati...
Agentic AI Explained: The Next Frontier of Autonomous Intelligence & Generati...Agentic AI Explained: The Next Frontier of Autonomous Intelligence & Generati...
Agentic AI Explained: The Next Frontier of Autonomous Intelligence & Generati...
Aaryan Kansari
 
Droidal: AI Agents Revolutionizing Healthcare
Droidal: AI Agents Revolutionizing HealthcareDroidal: AI Agents Revolutionizing Healthcare
Droidal: AI Agents Revolutionizing Healthcare
Droidal LLC
 
UiPath Community Zurich: Release Management and Build Pipelines
UiPath Community Zurich: Release Management and Build PipelinesUiPath Community Zurich: Release Management and Build Pipelines
UiPath Community Zurich: Release Management and Build Pipelines
UiPathCommunity
 

Scala xml power play part 1

  • 1. Scala – XML Power Play -Part 1 By VulcanMinds
  • 2. Creating XML content Scala treats XML as ‘first class citizen’. Scala provides easy syntax for creating XML content for e.g. scala> var someXML = <candidate><name> Manish </name> <title> Consultant</title> <city> Pune </city></candidate> someXML: scala.xml.Elem = <candidate><name> Manish </name> <title> Consultant</title> <city> Pune </city></candidate> scala> var employee = <Employee><ID> 456789</ID> <Name> Mark Holland </Name><AGE>34</AGE></Employee> employee: scala.xml.Elem = <Employee><ID> 456789</ID> <Name> Mark Holland </Name><AGE>34</AGE></Employee> You can save the XML content to a file easily like below. You must import XML module in the REPL (console) for this example. scala> import scala.xml._; import scala.xml._ scala> XML.save("sample.xml", employee ); Also you can load XML directly from an XML file like below. scala> val xmlFromFile = XML.loadFile("sample.xml"); xmlFromFile: scala.xml.Elem = <candidate><name> Manish </name> <title> Scala Consultant</title> <city> Pune </city></candidate>
  • 3. Searching in XML….. Scala provides easy syntax for searching in the XML content scala> var employee = <Employee><ID> 456789</ID> <Name> Mark Holland </Name><AGE>34</AGE></Employee> employee: scala.xml.Elem = <Employee><ID> 456789</ID> <Name> Mark Holland </Name><AGE>34</AGE></Employee> scala> var name = employee "Name" text name: String = " Mark Holland “ scala> var age = employee "age" text // String searches are case sensitive age: String = "" scala> var age = employee "AGE" text age: String = 34 Using wild card character ‘_’ will get everything within <employee> tag scala> var age = employee "_" age: scala.xml.NodeSeq = NodeSeq(<ID> 456789</ID>, <Name> Mark Holland </Name>, <AGE>34</AGE>) For deep diving search in the XML jungle which finds tags deep inside the XML jungle you can use ‘’ operator like below…. Note that <CITY> … </CITY> tag is inside the <ADDRESS> </ADDRESS> tag. scala> var employee = <Employee><ID> 456789</ID> <Name> Mark Holland </Name><AGE>34</AGE><ADDRESS><CITY>New York</CITY></ADDRESS></Employee> employee: scala.xml.Elem = <Employee><ID> 456789</ID> <Name> Mark Holland </Name><AGE>34</AGE><ADDRESS><CITY>New York</CITY></ADDRESS></Employee> scala> var city = employee “CITY“ city: scala.xml.NodeSeq = NodeSeq(<CITY>New York</CITY>)
  • 4. Processing XML….. Scala provides elegant syntax for processing content in XML tags….. For e.g. For the example members.xml file <Members> <Member> <Name> THOMAS D'SILVA </Name> <Address>123 PARK AVENUE, GRANT ROAD </Address> <CITY> MUMBAI </CITY> <COUNTRY>INDIA </COUNTRY> </Member> <Member> <Name> ALVIN NAZARETH</Name> <Address> 524 ELEPHANT GRASS,BEDFORD DRIVE,</Address> <CITY>SASKATEN </CITY> <COUNTRY>INDIA </COUNTRY> </Member> <Member> <Name>ANTONIO ALVAREZ</Name> <Address>123 PARK AVENUE GRANT ROAD </Address> <CITY>MUMBAI</CITY> <COUNTRY>INDIA</COUNTRY> </Member> <Member> <Name>JONAS WHITTAKER</Name> <Address>B20345 ANDROMEDA HOUSE, PARAMOUNT ESTATE, </Address> <CITY>TIMBUKTU</CITY> <COUNTRY>EGYPTIA</COUNTRY> </Member> </Members>
  • 5. Processing XML….. Following code prints the names of members from India. scala> var members=XML.loadFile("members2.xml") members: scala.xml.Elem = <Members> <Member> <Name> THOMAS D'SILVA </Name> <Address>123 PARK AVENUE, GRANT ROAD </Address> <CITY>MUMBAI</CITY> <COUNTRY>INDIA</COUNTRY> ……… ………. </Members> scala> for(val aMember <- members "Member") { if( (aMember "COUNTRY").text.trim.equals("INDIA") ) { println((aMember "Name").text); } } THOMAS D'SILVA ALVIN NAZARETH ANTONIO ALVAREZ
  • 6. Processing XML….. Following code prints all the cities the members are from …. scala> var cities = members ”CITY” cities: scala.xml.NodeSeq = NodeSeq(<CITY> MUMBAI </CITY>, <CITY> SASKATEN</CITY>, <CITY> MUMBAI</CITY>, <CITY>TIMBUKTU</CITY>) Following code prints the total number of members scala> var memberElements = members ”Member” ………….. scala> println(memberElements.length) 4 Following code finds the first and the last member in the sequence scala> println(memberElements.first) <Member> <Name> THOMAS D'SILVA </Name> <Address>123 PARK AVENUE, GRANT ROAD </Address> <CITY> MUMBAI</CITY> <COUNTRY> INDIA </COUNTRY> </Member> scala> println(memberElements.last) <Member> <Name> JONAS WHITTAKER</Name> <Address> B20345 ANDROMEDA HOUSE, PARAMOUNT ESTATE, </Address> <CITY> TIMBUKTU</CITY> <COUNTRY> EGYPTIA</COUNTRY> </Member>
  • 7. Processing XML….. Following code gets a slice of member nodes from all nodes with “from” index including to “toIndex” excluding. scala> println(memberElements.slice(0,2)) <Member> <Name> THOMAS D'SILVA </Name> <Address>123 PARK AVENUE, GRANT ROAD </Address> <CITY> MUMBAI</CITY> <COUNTRY> INDIA </COUNTRY> </Member> <Member> <Name> ALVIN NAZARETH</Name> <Address> 524 ELEPHANT GRASS, BEDFORD DRIVE,</Address> <CITY> SASKATEN </CITY> <COUNTRY> INDIA</COUNTRY> </Member>