SlideShare ist ein Scribd-Unternehmen logo
1 von 39
Java and XML
(DOM, SAX, JDOM)

Raji GHAWI

20/01/2009
Outlines
1.
2.
3.

DOM
SAX
JDOM

20/01/2009

2
1.

DOM

Document Object Model
<inventory>
<book year="2000">
<title>Snow Crash</title>
<author>Neal Stephenson</author>
<publisher>Spectra</publisher>
<isbn>0553380958</isbn>
<price>14.95</price>
</book>
<book year="2005">
<title>Burning Tower</title>
<author>Larry Niven</author>
<author>Jerry Pournelle</author>
<publisher>Pocket</publisher>
<isbn>0743416910</isbn>
<price>5.99</price>
</book>
<book year="1995">
<title>Zodiac</title>
<author>Neal Stephenson</author>
<publisher>Spectra</publisher>
<isbn>0553573862</isbn>
<price>7.50</price>
</book>
<!-- more books... -->
</inventory>

20/01/2009

4
Import required packages

import javax.xml.parsers.*;
import org.w3c.dom.*;

20/01/2009

5
Create the parser
DOM parser factory
try {
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();

DocumentBuilder builder = factory.newDocumentBuilder();
// ....
} catch (Exception e) {
e.printStackTrace(System.out);
}

DOM parser

20/01/2009

IOException
ParserConfigurationException
SAXException
6
Parse an XML file

Document document = builder.parse("../inventory.xml");

the entire XML file (as a tree)
(the Document Object Model)

20/01/2009

7
Root element
the root element
Element root = document.getDocumentElement();
System.out.println(root.getTagName());

20/01/2009

8
Nodes
Node

Text

Element

may have children

Attr

leaves
Operations on Nodes

Element

Text

Attr

getNodeName()

tag name

"#text"

name of attribute

getNodeValue()

null

text contents

value of attribute

getNodeType()

ELEMENT_NODE

TEXT_NODE

ATTRIBUTE_NODE

getAttributes()

NamedNodeMap

null

null

20/01/2009

9
Distinguishing Node types

switch(node.getNodeType()) {
case Node.ELEMENT_NODE:
Element element = (Element)node;
...;
break;
case Node.TEXT_NODE:
Text text = (Text)node;
...
break;
case Node.ATTRIBUTE_NODE:
Attr attr = (Attr)node;
...
break;
default: ...
}

20/01/2009

10
Operations on Nodes










getParentNode()
getFirstChild()
getNextSibling()
getPreviousSibling()
getLastChild()
hasAttributes()
hasChildNodes()

20/01/2009

11
Travel through children nodes

if (element.hasChildNodes()) {
Node child = element.getFirstChild();
while (child != null) {
// ....
child = child.getNextSibling();
}
}

20/01/2009

12
Operations for Elements







String getTagName()
boolean hasAttribute(String name)
String getAttribute(String name)
boolean hasAttributes()
NamedNodeMap
getAttributes()

20/01/2009

13
NamedNodeMap





Node getNamedItem(String name)
int getLength()
Node item(int index)
NamedNodeMap map = element.getAttributes();
for (int i = 0; i < map.getLength(); i++) {
Attr attr = (Attr) map.item(i);
System.out.println(attr.getNodeName()
+ "='"+ attr.getNodeValue()+"'");
}

20/01/2009

14
Operations on Texts




String getData()
int getLength()
String substringData(int offset, int count)

20/01/2009

15
Operations on Attrs




String getName()
Element getOwnerElement()
String getValue()

20/01/2009

16
2.

SAX

Simple API for XML
Import required packages

import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

20/01/2009

18
Create the parser
// Create a parser factory
SAXParserFactory factory = SAXParserFactory.newInstance();
// Tell factory that the parser must understand namespaces
factory.setNamespaceAware(true);
try {
// Make the parser
SAXParser saxParser = factory.newSAXParser();
XMLReader parser = saxParser.getXMLReader();
} catch(Exception e){
e.printStackTrace();
}

20/01/2009

IOException
ParserConfigurationException
SAXException
19
Parse an XML file
// Create a handler
Handler handler = new Handler();
// Tell the parser to use this handler
parser.setContentHandler(handler);
// Finally, read and parse the document
parser.parse("./inventory.xml");

20/01/2009

20
SAX handlers


A callback handler for SAX must implement four interfaces:
 interface ContentHandler
 interface DTDHandler
 interface EntityResolver
 interface ErrorHandler



It is easier to use an adapter class

20/01/2009

21
Class DefaultHandler





DefaultHandler is in package org.xml.sax.helpers
DefaultHandler implements ContentHandler, DTDHandler,
EntityResolver, and ErrorHandler
DefaultHandler is an adapter class




Provides empty methods for every method declared in each of the four
interfaces

To use this class, extend it and override the methods that are
important to your application

20/01/2009

22
The Handler class
class Handler extends DefaultHandler {
// SAX calls this method when it encounters a start tag
public void startElement(String namespaceURI,
String localName,
String qualifiedName,
Attributes attributes) throws SAXException {
System.out.println("startElement: " + qualifiedName);
}
// SAX calls this method to pass in character data
public void characters(char ch[], int start, int length)
throws SAXException {
System.out.println("characters: "" +
new String(ch, start, length) + """);
}
// SAX call this method when it encounters an end tag
public void endElement(String namespaceURI,
String localName,
String qualifiedName) throws SAXException {
System.out.println("endElement: /" + qualifiedName);
}

}
20/01/2009

23
<inventory>
<book year="2000">
<title>Snow Crash</title>
<author>Neal Stephenson</author>
<publisher>Spectra</publisher>
<isbn>0553380958</isbn>
<price>14.95</price>
</book>
<book year="2005">
<title>Burning Tower</title>
<author>Larry Niven</author>
<author>Jerry Pournelle</author>
<publisher>Pocket</publisher>
<isbn>0743416910</isbn>
<price>5.99</price>
</book>
<book year="1995">
<title>Zodiac</title>
<author>Neal Stephenson</author>
<publisher>Spectra</publisher>
<isbn>0553573862</isbn>
<price>7.50</price>
</book>
<!-- more books... -->
</inventory>
20/01/2009

startElement: inventory
characters: "
"
startElement: book
characters: "
"
startElement: title
characters: "Snow Crash"
endElement: /title
characters: "
"
startElement: author
characters: "Neal Stephenson"
endElement: /author
characters: "
"
startElement: publisher
characters: "Spectra"
endElement: /publisher
characters: "
...
"
endElement: /book
...
endElement: /inventory
24
Attributes











getLength()
getLocalName(index)
getQName(index)
getValue(index)
getType(index)

int getIndex(String qualifiedName)
int getIndex(String uri, String localName)
String getValue(String qualifiedName)
String getValue(String uri, String localName)

20/01/2009

25
Attributes

public void startElement(String namespaceURI,
String localName,
String qualifiedName,
Attributes attributes) throws SAXException {
// ....
for (int i = 0; i < attributes.getLength(); i++) {
String attName = attributes.getQName(i);
String attValue = attributes.getValue(i);
System.out.println(attName+"='"+attValue+"'");
}
// ....
}

20/01/2009

26
3.

JDOM

Java DOM
Import required packages
import
import
import
import

org.jdom.*;
org.jdom.input.*;
org.jdom.output.*;
org.jdom.adapters.*;

org.jdom
org.jdom.adapters
org.jdom.input
org.jdom.output
20/01/2009

28
Create the parser

try {
SAXBuilder builder = new SAXBuilder();

// ....
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (JDOMException je) {
je.printStackTrace();
}

20/01/2009

29
Parse an XML file

Document document = builder.build("../inventory.xml");

20/01/2009

30
Root element

Element root = document.getRootElement();
System.out.println(root.getName());

20/01/2009

31
Print out the document

XMLOutputter outputter = new XMLOutputter();
outputter.output(document, System.out);

StringWriter sw = new StringWriter();
XMLOutputter outputter = new XMLOutputter();
outputter.output(document, sw);
String xml = sw.toString();

Advantage 1:
20/01/2009

Output facility
32
Get children
• Get all direct children
List allChildren = element.getChildren();

• Get all direct children with a given name
List namedChildren = element.getChildren("book");

• Get the first child with a given name
Element child = element.getChild("book");

Advantage 2:
20/01/2009

supports Java Collections
33
Travel through children nodes

List children = element.getChildren();
for (int i = 0; i < children.size(); i++) {
Element elem = (Element) children.get(i);
// ....
}

20/01/2009

34
Get attributes
• Get all attributes
List attrs = element.getAttributes();
for (int i = 0; i < attrs.size(); i++)
Attribute attr = (Attribute) attrs.get(i);
System.out.println(attr.getName()+" = "+attr.getValue());
}

• Get an attribute with a given name
Attribute attr = element.getAttribute("year");

• Get an attribute value with a given name
String value = element.getAttributeValue("year");

20/01/2009

35
Reading Element Content
• The text content is directly available
String content = element.getText();

• Remove extra whitespace
String content = element.getTextTrim();

20/01/2009

36
Mixed Content
• Sometimes an element may contain comments, text content, and children
<table>
<!-- Some comment -->
Some text
<tr>Some child</tr>
</table>

String text = table.getTextTrim();
Element tr = table.getChild("tr");

20/01/2009

37
Mixed Content

List mixedContent = table.getContent();
Iterator iter = mixedContent.iterator();
while (iter.hasNext()) {
Object obj = iter.next();
if (obj instanceof Comment) {
System.out.println("Comment: " + obj);
} else if (obj instanceof String) {
System.out.println("String: " + obj);
} else if (obj instanceof Element) {
System.out.println("Element: " + ((Element)obj).getName());
}
}

20/01/2009

38
References


Processing XML with Java; Elliotte Rusty Harold
http://cafeconleche.org/books/xmljava/chapters/index.html

20/01/2009

39

Weitere ähnliche Inhalte

Was ist angesagt?

Dom(document object model)
Dom(document object model)Dom(document object model)
Dom(document object model)Partnered Health
 
Hibernate ppt
Hibernate pptHibernate ppt
Hibernate pptAneega
 
Java Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepJava Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepGuo Albert
 
Java Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsJava Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsAnton Keks
 
Spring Framework - AOP
Spring Framework - AOPSpring Framework - AOP
Spring Framework - AOPDzmitry Naskou
 
Angular - Chapter 4 - Data and Event Handling
 Angular - Chapter 4 - Data and Event Handling Angular - Chapter 4 - Data and Event Handling
Angular - Chapter 4 - Data and Event HandlingWebStackAcademy
 
Spring I/O 2012: Natural Templating in Spring MVC with Thymeleaf
Spring I/O 2012: Natural Templating in Spring MVC with ThymeleafSpring I/O 2012: Natural Templating in Spring MVC with Thymeleaf
Spring I/O 2012: Natural Templating in Spring MVC with ThymeleafThymeleaf
 
Advance OOP concepts in Python
Advance OOP concepts in PythonAdvance OOP concepts in Python
Advance OOP concepts in PythonSujith Kumar
 
jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...
jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...
jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...Edureka!
 
Introduction to Spring Boot!
Introduction to Spring Boot!Introduction to Spring Boot!
Introduction to Spring Boot!Jakub Kubrynski
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced JavascriptAdieu
 
Java Server Pages(jsp)
Java Server Pages(jsp)Java Server Pages(jsp)
Java Server Pages(jsp)Manisha Keim
 

Was ist angesagt? (20)

Dom(document object model)
Dom(document object model)Dom(document object model)
Dom(document object model)
 
Hibernate ppt
Hibernate pptHibernate ppt
Hibernate ppt
 
Spring Core
Spring CoreSpring Core
Spring Core
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Java Concurrency by Example
Java Concurrency by ExampleJava Concurrency by Example
Java Concurrency by Example
 
Java Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepJava Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By Step
 
Java Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsJava Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and Streams
 
Spring Framework - AOP
Spring Framework - AOPSpring Framework - AOP
Spring Framework - AOP
 
Core java
Core javaCore java
Core java
 
Struts
StrutsStruts
Struts
 
Angular - Chapter 4 - Data and Event Handling
 Angular - Chapter 4 - Data and Event Handling Angular - Chapter 4 - Data and Event Handling
Angular - Chapter 4 - Data and Event Handling
 
Servlets
ServletsServlets
Servlets
 
Spring I/O 2012: Natural Templating in Spring MVC with Thymeleaf
Spring I/O 2012: Natural Templating in Spring MVC with ThymeleafSpring I/O 2012: Natural Templating in Spring MVC with Thymeleaf
Spring I/O 2012: Natural Templating in Spring MVC with Thymeleaf
 
JQuery introduction
JQuery introductionJQuery introduction
JQuery introduction
 
Advance OOP concepts in Python
Advance OOP concepts in PythonAdvance OOP concepts in Python
Advance OOP concepts in Python
 
jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...
jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...
jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...
 
Introduction to Spring Boot!
Introduction to Spring Boot!Introduction to Spring Boot!
Introduction to Spring Boot!
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
 
Java Server Pages(jsp)
Java Server Pages(jsp)Java Server Pages(jsp)
Java Server Pages(jsp)
 
Jquery
JqueryJquery
Jquery
 

Andere mochten auch (10)

Oracle xmldb
Oracle xmldbOracle xmldb
Oracle xmldb
 
Xml
XmlXml
Xml
 
Grupo1
Grupo1Grupo1
Grupo1
 
Xml Java
Xml JavaXml Java
Xml Java
 
Java XML Parsing
Java XML ParsingJava XML Parsing
Java XML Parsing
 
Tema 3 xml processing ap is
Tema 3   xml processing ap isTema 3   xml processing ap is
Tema 3 xml processing ap is
 
XML y JAVA
XML y JAVAXML y JAVA
XML y JAVA
 
Integración de aplicaciones Java
Integración de aplicaciones JavaIntegración de aplicaciones Java
Integración de aplicaciones Java
 
DOM and SAX
DOM and SAXDOM and SAX
DOM and SAX
 
Paginas de matematicas
Paginas de matematicasPaginas de matematicas
Paginas de matematicas
 

Ähnlich wie Java and XML (20)

Xm lparsers
Xm lparsersXm lparsers
Xm lparsers
 
Xml parsers
Xml parsersXml parsers
Xml parsers
 
SAX, DOM & JDOM parsers for beginners
SAX, DOM & JDOM parsers for beginnersSAX, DOM & JDOM parsers for beginners
SAX, DOM & JDOM parsers for beginners
 
DB2 Native XML
DB2 Native XMLDB2 Native XML
DB2 Native XML
 
jQuery introduction
jQuery introductionjQuery introduction
jQuery introduction
 
Dom
DomDom
Dom
 
Advanced Web Programming Chapter 12
Advanced Web Programming Chapter 12Advanced Web Programming Chapter 12
Advanced Web Programming Chapter 12
 
Xml & Java
Xml & JavaXml & Java
Xml & Java
 
IT6801-Service Oriented Architecture-Unit-2-notes
IT6801-Service Oriented Architecture-Unit-2-notesIT6801-Service Oriented Architecture-Unit-2-notes
IT6801-Service Oriented Architecture-Unit-2-notes
 
Xml session
Xml sessionXml session
Xml session
 
PostgreSQL and XML
PostgreSQL and XMLPostgreSQL and XML
PostgreSQL and XML
 
Modern JavaScript Programming
Modern JavaScript Programming Modern JavaScript Programming
Modern JavaScript Programming
 
C# Advanced L03-XML+LINQ to XML
C# Advanced L03-XML+LINQ to XMLC# Advanced L03-XML+LINQ to XML
C# Advanced L03-XML+LINQ to XML
 
Simple xml in .net
Simple xml in .netSimple xml in .net
Simple xml in .net
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
 
XML
XMLXML
XML
 
srgoc
srgocsrgoc
srgoc
 
Xml And JSON Java
Xml And JSON JavaXml And JSON Java
Xml And JSON Java
 
java API for XML DOM
java API for XML DOMjava API for XML DOM
java API for XML DOM
 
Xml 2
Xml  2 Xml  2
Xml 2
 

Mehr von Raji Ghawi

Database Programming Techniques
Database Programming TechniquesDatabase Programming Techniques
Database Programming TechniquesRaji Ghawi
 
Java and XML Schema
Java and XML SchemaJava and XML Schema
Java and XML SchemaRaji Ghawi
 
Java and SPARQL
Java and SPARQLJava and SPARQL
Java and SPARQLRaji Ghawi
 
Ontology-based Cooperation of Information Systems
Ontology-based Cooperation of Information SystemsOntology-based Cooperation of Information Systems
Ontology-based Cooperation of Information SystemsRaji Ghawi
 
OWSCIS: Ontology and Web Service based Cooperation of Information Sources
OWSCIS: Ontology and Web Service based Cooperation of Information SourcesOWSCIS: Ontology and Web Service based Cooperation of Information Sources
OWSCIS: Ontology and Web Service based Cooperation of Information SourcesRaji Ghawi
 
Coopération des Systèmes d'Informations basée sur les Ontologies
Coopération des Systèmes d'Informations basée sur les OntologiesCoopération des Systèmes d'Informations basée sur les Ontologies
Coopération des Systèmes d'Informations basée sur les OntologiesRaji Ghawi
 
Building Ontologies from Multiple Information Sources
Building Ontologies from Multiple Information SourcesBuilding Ontologies from Multiple Information Sources
Building Ontologies from Multiple Information SourcesRaji Ghawi
 
Database-to-Ontology Mapping Generation for Semantic Interoperability
Database-to-Ontology Mapping Generation for Semantic InteroperabilityDatabase-to-Ontology Mapping Generation for Semantic Interoperability
Database-to-Ontology Mapping Generation for Semantic InteroperabilityRaji Ghawi
 

Mehr von Raji Ghawi (12)

Database Programming Techniques
Database Programming TechniquesDatabase Programming Techniques
Database Programming Techniques
 
Java and XML Schema
Java and XML SchemaJava and XML Schema
Java and XML Schema
 
Java and SPARQL
Java and SPARQLJava and SPARQL
Java and SPARQL
 
Java and OWL
Java and OWLJava and OWL
Java and OWL
 
SPARQL
SPARQLSPARQL
SPARQL
 
XQuery
XQueryXQuery
XQuery
 
XPath
XPathXPath
XPath
 
Ontology-based Cooperation of Information Systems
Ontology-based Cooperation of Information SystemsOntology-based Cooperation of Information Systems
Ontology-based Cooperation of Information Systems
 
OWSCIS: Ontology and Web Service based Cooperation of Information Sources
OWSCIS: Ontology and Web Service based Cooperation of Information SourcesOWSCIS: Ontology and Web Service based Cooperation of Information Sources
OWSCIS: Ontology and Web Service based Cooperation of Information Sources
 
Coopération des Systèmes d'Informations basée sur les Ontologies
Coopération des Systèmes d'Informations basée sur les OntologiesCoopération des Systèmes d'Informations basée sur les Ontologies
Coopération des Systèmes d'Informations basée sur les Ontologies
 
Building Ontologies from Multiple Information Sources
Building Ontologies from Multiple Information SourcesBuilding Ontologies from Multiple Information Sources
Building Ontologies from Multiple Information Sources
 
Database-to-Ontology Mapping Generation for Semantic Interoperability
Database-to-Ontology Mapping Generation for Semantic InteroperabilityDatabase-to-Ontology Mapping Generation for Semantic Interoperability
Database-to-Ontology Mapping Generation for Semantic Interoperability
 

Kürzlich hochgeladen

The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
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...Enterprise Knowledge
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
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 MenDelhi Call girls
 
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 SolutionsEnterprise Knowledge
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 

Kürzlich hochgeladen (20)

The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
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...
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
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
 
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
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 

Java and XML

  • 1. Java and XML (DOM, SAX, JDOM) Raji GHAWI 20/01/2009
  • 4. <inventory> <book year="2000"> <title>Snow Crash</title> <author>Neal Stephenson</author> <publisher>Spectra</publisher> <isbn>0553380958</isbn> <price>14.95</price> </book> <book year="2005"> <title>Burning Tower</title> <author>Larry Niven</author> <author>Jerry Pournelle</author> <publisher>Pocket</publisher> <isbn>0743416910</isbn> <price>5.99</price> </book> <book year="1995"> <title>Zodiac</title> <author>Neal Stephenson</author> <publisher>Spectra</publisher> <isbn>0553573862</isbn> <price>7.50</price> </book> <!-- more books... --> </inventory> 20/01/2009 4
  • 5. Import required packages import javax.xml.parsers.*; import org.w3c.dom.*; 20/01/2009 5
  • 6. Create the parser DOM parser factory try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); // .... } catch (Exception e) { e.printStackTrace(System.out); } DOM parser 20/01/2009 IOException ParserConfigurationException SAXException 6
  • 7. Parse an XML file Document document = builder.parse("../inventory.xml"); the entire XML file (as a tree) (the Document Object Model) 20/01/2009 7
  • 8. Root element the root element Element root = document.getDocumentElement(); System.out.println(root.getTagName()); 20/01/2009 8
  • 9. Nodes Node Text Element may have children Attr leaves Operations on Nodes Element Text Attr getNodeName() tag name "#text" name of attribute getNodeValue() null text contents value of attribute getNodeType() ELEMENT_NODE TEXT_NODE ATTRIBUTE_NODE getAttributes() NamedNodeMap null null 20/01/2009 9
  • 10. Distinguishing Node types switch(node.getNodeType()) { case Node.ELEMENT_NODE: Element element = (Element)node; ...; break; case Node.TEXT_NODE: Text text = (Text)node; ... break; case Node.ATTRIBUTE_NODE: Attr attr = (Attr)node; ... break; default: ... } 20/01/2009 10
  • 12. Travel through children nodes if (element.hasChildNodes()) { Node child = element.getFirstChild(); while (child != null) { // .... child = child.getNextSibling(); } } 20/01/2009 12
  • 13. Operations for Elements      String getTagName() boolean hasAttribute(String name) String getAttribute(String name) boolean hasAttributes() NamedNodeMap getAttributes() 20/01/2009 13
  • 14. NamedNodeMap    Node getNamedItem(String name) int getLength() Node item(int index) NamedNodeMap map = element.getAttributes(); for (int i = 0; i < map.getLength(); i++) { Attr attr = (Attr) map.item(i); System.out.println(attr.getNodeName() + "='"+ attr.getNodeValue()+"'"); } 20/01/2009 14
  • 15. Operations on Texts    String getData() int getLength() String substringData(int offset, int count) 20/01/2009 15
  • 16. Operations on Attrs    String getName() Element getOwnerElement() String getValue() 20/01/2009 16
  • 18. Import required packages import javax.xml.parsers.*; import org.xml.sax.*; import org.xml.sax.helpers.*; 20/01/2009 18
  • 19. Create the parser // Create a parser factory SAXParserFactory factory = SAXParserFactory.newInstance(); // Tell factory that the parser must understand namespaces factory.setNamespaceAware(true); try { // Make the parser SAXParser saxParser = factory.newSAXParser(); XMLReader parser = saxParser.getXMLReader(); } catch(Exception e){ e.printStackTrace(); } 20/01/2009 IOException ParserConfigurationException SAXException 19
  • 20. Parse an XML file // Create a handler Handler handler = new Handler(); // Tell the parser to use this handler parser.setContentHandler(handler); // Finally, read and parse the document parser.parse("./inventory.xml"); 20/01/2009 20
  • 21. SAX handlers  A callback handler for SAX must implement four interfaces:  interface ContentHandler  interface DTDHandler  interface EntityResolver  interface ErrorHandler  It is easier to use an adapter class 20/01/2009 21
  • 22. Class DefaultHandler    DefaultHandler is in package org.xml.sax.helpers DefaultHandler implements ContentHandler, DTDHandler, EntityResolver, and ErrorHandler DefaultHandler is an adapter class   Provides empty methods for every method declared in each of the four interfaces To use this class, extend it and override the methods that are important to your application 20/01/2009 22
  • 23. The Handler class class Handler extends DefaultHandler { // SAX calls this method when it encounters a start tag public void startElement(String namespaceURI, String localName, String qualifiedName, Attributes attributes) throws SAXException { System.out.println("startElement: " + qualifiedName); } // SAX calls this method to pass in character data public void characters(char ch[], int start, int length) throws SAXException { System.out.println("characters: "" + new String(ch, start, length) + """); } // SAX call this method when it encounters an end tag public void endElement(String namespaceURI, String localName, String qualifiedName) throws SAXException { System.out.println("endElement: /" + qualifiedName); } } 20/01/2009 23
  • 24. <inventory> <book year="2000"> <title>Snow Crash</title> <author>Neal Stephenson</author> <publisher>Spectra</publisher> <isbn>0553380958</isbn> <price>14.95</price> </book> <book year="2005"> <title>Burning Tower</title> <author>Larry Niven</author> <author>Jerry Pournelle</author> <publisher>Pocket</publisher> <isbn>0743416910</isbn> <price>5.99</price> </book> <book year="1995"> <title>Zodiac</title> <author>Neal Stephenson</author> <publisher>Spectra</publisher> <isbn>0553573862</isbn> <price>7.50</price> </book> <!-- more books... --> </inventory> 20/01/2009 startElement: inventory characters: " " startElement: book characters: " " startElement: title characters: "Snow Crash" endElement: /title characters: " " startElement: author characters: "Neal Stephenson" endElement: /author characters: " " startElement: publisher characters: "Spectra" endElement: /publisher characters: " ... " endElement: /book ... endElement: /inventory 24
  • 25. Attributes          getLength() getLocalName(index) getQName(index) getValue(index) getType(index) int getIndex(String qualifiedName) int getIndex(String uri, String localName) String getValue(String qualifiedName) String getValue(String uri, String localName) 20/01/2009 25
  • 26. Attributes public void startElement(String namespaceURI, String localName, String qualifiedName, Attributes attributes) throws SAXException { // .... for (int i = 0; i < attributes.getLength(); i++) { String attName = attributes.getQName(i); String attValue = attributes.getValue(i); System.out.println(attName+"='"+attValue+"'"); } // .... } 20/01/2009 26
  • 29. Create the parser try { SAXBuilder builder = new SAXBuilder(); // .... } catch (IOException ioe) { ioe.printStackTrace(); } catch (JDOMException je) { je.printStackTrace(); } 20/01/2009 29
  • 30. Parse an XML file Document document = builder.build("../inventory.xml"); 20/01/2009 30
  • 31. Root element Element root = document.getRootElement(); System.out.println(root.getName()); 20/01/2009 31
  • 32. Print out the document XMLOutputter outputter = new XMLOutputter(); outputter.output(document, System.out); StringWriter sw = new StringWriter(); XMLOutputter outputter = new XMLOutputter(); outputter.output(document, sw); String xml = sw.toString(); Advantage 1: 20/01/2009 Output facility 32
  • 33. Get children • Get all direct children List allChildren = element.getChildren(); • Get all direct children with a given name List namedChildren = element.getChildren("book"); • Get the first child with a given name Element child = element.getChild("book"); Advantage 2: 20/01/2009 supports Java Collections 33
  • 34. Travel through children nodes List children = element.getChildren(); for (int i = 0; i < children.size(); i++) { Element elem = (Element) children.get(i); // .... } 20/01/2009 34
  • 35. Get attributes • Get all attributes List attrs = element.getAttributes(); for (int i = 0; i < attrs.size(); i++) Attribute attr = (Attribute) attrs.get(i); System.out.println(attr.getName()+" = "+attr.getValue()); } • Get an attribute with a given name Attribute attr = element.getAttribute("year"); • Get an attribute value with a given name String value = element.getAttributeValue("year"); 20/01/2009 35
  • 36. Reading Element Content • The text content is directly available String content = element.getText(); • Remove extra whitespace String content = element.getTextTrim(); 20/01/2009 36
  • 37. Mixed Content • Sometimes an element may contain comments, text content, and children <table> <!-- Some comment --> Some text <tr>Some child</tr> </table> String text = table.getTextTrim(); Element tr = table.getChild("tr"); 20/01/2009 37
  • 38. Mixed Content List mixedContent = table.getContent(); Iterator iter = mixedContent.iterator(); while (iter.hasNext()) { Object obj = iter.next(); if (obj instanceof Comment) { System.out.println("Comment: " + obj); } else if (obj instanceof String) { System.out.println("String: " + obj); } else if (obj instanceof Element) { System.out.println("Element: " + ((Element)obj).getName()); } } 20/01/2009 38
  • 39. References  Processing XML with Java; Elliotte Rusty Harold http://cafeconleche.org/books/xmljava/chapters/index.html 20/01/2009 39