6 xml parsing

118
XML Parsing Atul Kahate [email protected]

Upload: gauravashq

Post on 06-May-2015

1.181 views

Category:

Education


2 download

DESCRIPTION

XML Parsing

TRANSCRIPT

Page 1: 6   xml parsing

XML Parsing

Atul Kahate

[email protected]

Page 2: 6   xml parsing

XML Parsing | Atul Kahate 2

Agenda XML Parsing/Processing Basics

Simple API for XML (SAX) Document Object Model (DOM)

XML and Java using JAXP XML and ASP.NET

Page 3: 6   xml parsing

XML Parsing/Processing

Page 4: 6   xml parsing

XML Parsing | Atul Kahate 4

XML Processing XML processing means

Reading an XML document Parsing it in the desired manner

Allows handling the contents of an XML document the way we want

Page 5: 6   xml parsing

XML Parsing | Atul Kahate 5

XML Parser Software that sits between an

application and the XML files Shield programmers from having to

manually parse through XML documents

Programmers are free to concentrate on the contents of the XML file, not syntax

Programmers use the parser APIs to access/manipulate an XML file

Page 6: 6   xml parsing

XML Parsing | Atul Kahate 6

XML Processing Approaches Process as a sequence of events

Simple API for XML Processing (SAX)

Process as a hierarchy of nodes Document Object Model (DOM)

Pull approach Streaming API (StAX)

Page 7: 6   xml parsing

XML Parsing | Atul Kahate 7

SAX Versus DOM

Page 8: 6   xml parsing

XML Parsing | Atul Kahate 8

StAX Pulls events from the XML

document via the parser Also an event-based API, but

differs from SAX The application, and not the parser;

controls the flow

Page 9: 6   xml parsing

Simple API for XML (SAX)

Page 10: 6   xml parsing

XML Parsing | Atul Kahate 10

XML Processing as Sequence of Events – 1 Process as a sequence of events

Event is the occurrence of something noticeable

e.g. in Windows, mouse movement, keyboard input are events

The OS captures all events and sends messages to a program

The programmer has to take an appropriate action to deal with the event

Page 11: 6   xml parsing

XML Parsing | Atul Kahate 11

XML Processing as Sequence of Events – 2 Process as a sequence of events

Event-based model can be applied to XML documents also

Various events that occur while reading an XML document sequentially

Start of document Start tag of an element End tag of an element Comments

Page 12: 6   xml parsing

XML Parsing | Atul Kahate 12

XML Processing as Sequence of Events – 3 Process as a sequence of events

The programmer has to write code to handle these events

Called as event handlers

Page 13: 6   xml parsing

XML Parsing | Atul Kahate 13

Sequential Processing Example – 1 Consider the following XML document<?xml version=“1.0”?><books>

<book><name> Learning XML </name><author> Simon North </author><publication> TMH </publication>

</book><book>

<name> XML by Example </name><author> Don Box </author><publication> Pearson </publication>

</book></books>

Page 14: 6   xml parsing

XML Parsing | Atul Kahate 14

Sequential Processing Example – 2 Events generated when we read the

above XML fileStart documentStart element: booksStart element: bookStart element: nameCharacters: Learning XML End element: nameStart element: authorCharacters: Simon North End element: authorStart element: publication Characters: TMH End element: publication…End element: bookEnd document

Page 15: 6   xml parsing

XML Parsing | Atul Kahate 15

Sample XML Tree

Page 16: 6   xml parsing

XML Parsing | Atul Kahate 16

Tree Processing Sequence1

2 8

3 4 9 10 14 15

5 6 7 11 12 13 16 17

Page 17: 6   xml parsing

XML Parsing | Atul Kahate 17

Sequential Traversal: Summary Order

Top to bottom Left to right

Advantages Simple Fast Requires less amount of memory

Drawback Not possible to look ahead

Page 18: 6   xml parsing

XML Parsing | Atul Kahate 18

SAX Concept

Page 19: 6   xml parsing

JAXP

Java API for XML Processing

Page 20: 6   xml parsing

XML Parsing | Atul Kahate 20

JAXP Concept

Application program written in Java for working with XML

Java API for XML Processing (JAXP)

JAXP APIs

Simple API for XML Processing (SAX)

Document Object Model (DOM)

Sequential processing Tree-based processing

Page 21: 6   xml parsing

XML Parsing | Atul Kahate 21

JAXP Java API for XML Processing Standardized by Sun Very thin layer on top of SAX or DOM Makes application code parser-

independent Our programs should use JAXP,

which in turn, calls parser APIs Include package javax.xml.parsers.*

Page 22: 6   xml parsing

XML Parsing | Atul Kahate 22

JAXP: API or Abstraction? JAXP is an API, but is called as an

abstraction layer Does not provide new means of parsing

XML Does not add to SAX or DOM Does not give new functionality to Java

or XML handling Makes working with SAX and DOM easier It is vendor-neutral

Page 23: 6   xml parsing

XML Parsing | Atul Kahate 23

JAXP and Parsing JAXP is not a replacement for SAX, DOM, JDOM

etc Some vendor must supply the implementation of

SAX, DOM, etc JAXP provides APIs to use these implementations

In the early versions of JDK, Sun had supplied a parser called Crimson

Now, Sun provides Apache Xerces Both are not a part of JAXP API – they are part of JAXP

distribution In JDK, we can locate Xerces implementations in

the org.xml.sax and org.w3c.dom packages

Page 24: 6   xml parsing

XML Parsing | Atul Kahate 24

JAXP API The main JAXP APIs are defined in the

package javax.xml.parsers Contains two vendor-neutral factory

classes SAXParserFactory – Gives a SAXParser

object DocumentBuilderFactory – Gives a

DocumentBuilder object DocumentBuilder, in turn, gives Document object

Page 25: 6   xml parsing

XML Parsing | Atul Kahate 25

Package Details javax.xml.parsers

The JAXP APIs, which provide a common interface for different vendors' SAX and DOM parsers.

org.w3c.dom Defines the Document class (a DOM), as well as

classes for all of the components of a DOM. org.xml.sax

Defines the basic SAX APIs. javax.xml.transform

Defines the XSLT APIs that let you transform XML into other forms.

Page 26: 6   xml parsing

XML Parsing | Atul Kahate 26

Which Packages to use in JAXP? We need to include two sets of packages – one

for JAXP and the other for SAX/DOM, as appropriate

// JAXP import javax.xml.parsers.SAXParserFactory;

// SAX import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.SAXNotRecognizedException; import org.xml.sax.SAXNotSupportedException; import org.xml.sax.SAXParseException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.XMLReaderFactory;

Page 27: 6   xml parsing

SAX Programming in JAXP

Page 28: 6   xml parsing

XML Parsing | Atul Kahate 28

SAX Approach

Page 29: 6   xml parsing

XML Parsing | Atul Kahate 29

Key SAX APIs – 1 SAXParserFactory

Creates an instance of the parser determined by the system property, javax.xml.parsers.SAXParserFactory.

  SAXParser

An interface that defines several kinds of parse() methods. In general, you pass an XML data source and a DefaultHandler object to the parser, which processes the XML and invokes the appropriate methods in the handler object.

   

Page 30: 6   xml parsing

XML Parsing | Atul Kahate 30

Key SAX APIs – 2 SAXReader

The SAXParser wraps a SAXReader. Typically, you don't care about that, but every once in a while you need to get hold of it using SAXParser's getXMLReader(), so you can configure it. It is the SAXReader which carries on the conversation with the SAX event handlers you define.

DefaultHandler Not shown in the diagram, a DefaultHandler

implements the ContentHandler, ErrorHandler, DTDHandler, and EntityResolver interfaces (with null methods), so you can override only the ones you're interested in.

Page 31: 6   xml parsing

Design Patterns

Factory Pattern

Page 32: 6   xml parsing

XML Parsing | Atul Kahate 32

“new” means “Concrete”Vehicle vehicle = new Car ();

We want to use an interface (say Vehicle) to keep code flexible

However, we must create an instance of a concrete class (e.g. Car)

Makes the code more fragile and less flexible – Why? See next slide.

Page 33: 6   xml parsing

XML Parsing | Atul Kahate 33

Using “new” – 1

Vehicle vehicle;

if (picnic) vehicle = new Car ();else if (work) vehicle = new Bus ();else vehicle = new Scooter ();

Page 34: 6   xml parsing

XML Parsing | Atul Kahate 34

Using “new” – 2 We do not know until run time which

class to instantiate Whenever code needs to be changed,

we need to reopen this code and examine what needs to be added or removed

Mandates application changes at multiple places, making it difficult to maintain

Page 35: 6   xml parsing

XML Parsing | Atul Kahate 35

What is wrong with “new”? Nothing as such Problem is changes to code and their

impact on “new” By coding to an interface, we know that

we are insulated from changes made to a system

This is because different classes would implement the interface using polymorphism appropriately

Page 36: 6   xml parsing

XML Parsing | Atul Kahate 36

Key OO Principle Identify the aspects of code

that vary and separate them from what stays the same

Code should be open for extension, but closed for modifications

Page 37: 6   xml parsing

XML Parsing | Atul Kahate 37

Pizza Class – Ideal SituationPizza orderPizza () { Pizza pizza = new Pizza ();

Pizza.prepare (); Pizza.bake (); Pizza.cut (); Pizza.pack();

return pizza;}

Ideally, we would like this to be an abstract class or an interface, but we cannot directly instantiate an abstract class or an interface!

Page 38: 6   xml parsing

XML Parsing | Atul Kahate 38

Pizza Class – Ideal SituationPizza orderPizza () { Pizza pizza = new CheesePizza ();

Pizza.prepare (); Pizza.bake (); Pizza.cut (); Pizza.pack();

return pizza;}

We are left with no choice but to instantiate a concrete class

Page 39: 6   xml parsing

XML Parsing | Atul Kahate 39

SolutionPizza orderPizza (String type) { Pizza pizza;

if (type.equals (“cheese”)) { pizza = new CheesePizza (); } else if (type.equals (“corn”)) { pizza = new CornPizza (); } else { pizza = new GeneralPizza (); } Pizza.prepare (); Pizza.bake (); Pizza.cut (); Pizza.pack(); return pizza; }

1. We are passing the type of pizza to orderPizza () method.

2. Based on the type of pizza, we instantiate the correct concrete class. Each pizza has to implement the Pizza interface.

3. Each pizza sub-type (e.g. cheese) knows how to prepare itself.

Page 40: 6   xml parsing

XML Parsing | Atul Kahate 40

Is this correct? Let us review the principles:

Identify the aspects of code that vary and separate them from what stays the same

Code should be open for extension, but closed for modifications

Page 41: 6   xml parsing

XML Parsing | Atul Kahate 41

Problems What if we remove one pizza type,

and add another?

We need to touch the code

See next slide

Page 42: 6   xml parsing

XML Parsing | Atul Kahate 42

Problem – CodePizza orderPizza (String type) { Pizza pizza;

if (type.equals (“veg”)) { pizza = new VeggiePizza (); } else if (type.equals (“corn”)) { pizza = new CornPizza (); } else { pizza = new GeneralPizza (); } Pizza.prepare (); Pizza.bake (); Pizza.cut (); Pizza.pack(); return pizza; return pizza;}

1. Problem is that we end up touching code for modifications.

2. This is not what we want.

3. What is the solution?

Page 43: 6   xml parsing

XML Parsing | Atul Kahate 43

Code Modified FurtherPizza orderPizza (String type) { Pizza pizza;

if (type.equals (“cheese”)) {if (type.equals (“cheese”)) { pizza = new CheesePizza ();pizza = new CheesePizza (); }} else if (type.equals (“corn”)) {else if (type.equals (“corn”)) { pizza = new CornPizza ();pizza = new CornPizza (); }} else {else { pizza = new GeneralPizza ();pizza = new GeneralPizza (); }} Pizza.prepare (); Pizza.bake (); Pizza.cut (); Pizza.pack(); return pizza; return pizza;}

if (type.equals (“cheese”)) { pizza = new CheesePizza (); } else if (type.equals (“corn”)) { pizza = new CornPizza (); } else { pizza = new GeneralPizza (); }

Abstract out the code that varies, and put it into a separate class, that would only worry about how to create objects. If any other object needs a pizza object, this is the class to come to.

Page 44: 6   xml parsing

XML Parsing | Atul Kahate 44

This new Class is our “Factory”public class PizzaFactory { public Pizza createPizza (String type) { Pizza pizza = null; if (type.equals (“cheese”)) { pizza = new CheesePizza (); } else if (type.equals (“corn”)) { pizza = new CornPizza (); } else { pizza = new GeneralPizza (); } return pizza; }}

1. This new class creates new pizzas for its clients.

2. It has a createPizza () method, which all clients will use to instantiate new objects.

3. It contains code plucked out of the orderPizza () method.

Page 45: 6   xml parsing

XML Parsing | Atul Kahate 45

Modified Client Codepublic class PizzaStore { PizzaFactory factory = new PizzaFactory ();

public Pizza orderPizza (String type) { Pizza pizza = factory.createPizza (“Cheese”);

pizza.prepare (); pizza.bake (); pizza.cut (); pizza.pack ();

return pizza; }}

A variation of this is:

PizzaFactory factory = PizzaFactory.newInstance ();

In this case, newInstance () would be a static method in PizzaFactory, since we are not creating any object of PizzaFactory here

Page 46: 6   xml parsing

Another Factory Pattern Example

Page 47: 6   xml parsing

XML Parsing | Atul Kahate 47

Factory Pattern – The Need – 1 Consider this:Connection connection = new OracleConnection ();Connection connection = new SqlServerConnection

();Connection connection = new DB2Connection ();...

What are the problems? How to resolve them?

Page 48: 6   xml parsing

XML Parsing | Atul Kahate 48

Problems Summarized Sometimes, an Application (or framework) at runtime,

cannot anticipate the class of object that it must create. The Application (or framework) may know that it has to instantiate classes, but it may only know about abstract classes (or interfaces), which it cannot instantiate. Thus the Application class may only know when it has to instantiate a new Object of a class, not what kind of subclass to create.

A class may want it's subclasses to specify the objects to be created.

A class may delegate responsibility to one of several helper subclasses so that knowledge can be localized to specific helper subclasses.

Page 49: 6   xml parsing

XML Parsing | Atul Kahate 49

Factory Pattern – The Need – 2public Connection createConnection (String type) {

if (type.equals ("Oracle") { return new OracleConnection (); } else if (type.equals ("SQL Server") { return new SqlServerConnection (); } else if (type.equals ("DB2") { return new DB2Connection (); }}

Does it resolve all problems?

Page 50: 6   xml parsing

XML Parsing | Atul Kahate 50

More on Factory Pattern – 1 Factory Method is a creational pattern. This

pattern helps to model an interface for creating an object which at creation time can let its subclasses decide which class to instantiate. We call this a Factory Pattern since it is responsible for "Manufacturing" an Object. It helps instantiate the appropriate Subclass by creating the right Object from a group of related classes. The Factory Pattern promotes loose coupling by eliminating the need to bind application-specific classes into the code.

Factories have a simple function: Churn out objects.

Page 51: 6   xml parsing

XML Parsing | Atul Kahate 51

More on Factory Pattern – 2 Obviously, a factory is not needed to make an

object. A simple call to new will do it for you. However, the use of factories gives the programmer the opportunity to abstract the specific attributes of an Object into specific subclasses which create them.

The Factory Pattern is all about "Define an interface for creating an object, but let the subclasses decide which class to instantiate. The Factory method lets a class defer instantiation to subclasses"

Page 52: 6   xml parsing

XML Parsing | Atul Kahate 52

Factory Pattern – The Need – 3

We still need to add new code for a new connection type

The existing class needs to undergo changes every time

When object creation changes a lot, use a factory

Page 53: 6   xml parsing

XML Parsing | Atul Kahate 53

Factory Pattern – The Need – 4

Client code to use the factoryFirstFactory factory = FirstFactory.getInstance ();

Connection connection = factory.createConnection (“Oracle”);

Page 54: 6   xml parsing

XML Parsing | Atul Kahate 54

The Factory Class – 1public class FirstFactory { protected static String type;

public static FirstFactory getInstance () { type = null;

return new FirstFactory (); }

...}

Page 55: 6   xml parsing

XML Parsing | Atul Kahate 55

The Factory Class – 2public class FirstFactory {

protected static String type;

public static FirstFactory getInstance () { type = ""; return new FirstFactory (); }

public Connection createConnection (String t) {

type = t;

if (type.equals ("Oracle")) { return new OracleConnection (); } else if (type.equals ("SQL Server")) { return new SQLServerConnection (); } else { //if (type.equals ("DB2")) { return new DB2Connection (); } }

}

Page 56: 6   xml parsing

XML Parsing | Atul Kahate 56

Connection Classespublic interface Connection { public String description ();}

public class OracleConnection implements Connection { public OracleConnection () {

// Logic specific to Oracle }

public String description () { return "Oracle"; }}

public class SQLServerConnection implements Connection { public SQLServerConnection () {

// Logic specific to SQL Server }

public String description () { return "SQL Server"; }}

public class DB2Connection implements Connection { public DB2Connection () {

// Logic specific to DB2 }

public String description () { return "DB2"; }}

Page 57: 6   xml parsing

XML Parsing | Atul Kahate 57

Client Codepublic class TestConnection {

public static void main (String args []) {

FirstFactory factory = FirstFactory.getInstance ();

Connection connection = factory.createConnection ("DB2");

System.out.println ("You are connected with " + connection.description ());

}}

Page 58: 6   xml parsing

Factory Pattern - Exercise

Page 59: 6   xml parsing

XML Parsing | Atul Kahate 59

Exercise We want to be able to create any

of the following objects that have some similarities and some differences. Design using factory method design pattern. Employee Student Player

Page 60: 6   xml parsing

SAX

Page 61: 6   xml parsing

XML Parsing | Atul Kahate 61

Sequential Traversal: SAX SAX (Simple API for XML)1. Specify the parser to be used2. Create a parser instance3. Create an event handler to

respond to parsing events4. Invoke the parser with the

designated content handler and document

Page 62: 6   xml parsing

XML Parsing | Atul Kahate 62

1 – Specify the Parser Various approaches are possible

Set a system property for javax.xml.parsers.SAXParserfactory

Specify the parser in jre_dir/lib/jaxp.properties

Use system-dependent default parser (check documentation)

Usually done at the time of JDK installation itself automatically

Page 63: 6   xml parsing

XML Parsing | Atul Kahate 63

1 – Specify the Parser Example

Public static void main (String [] args){

String jaxpPropertyName = “javax.xml.parsers.SAXParserFactory”;…

}

Page 64: 6   xml parsing

XML Parsing | Atul Kahate 64

2 – Create a Parser Instance Steps

i. Create an instance of a parser factoryii. Use that to create a SAXParser object

Example

SAXParserFactory factory = SAXParserFactory.newInstance ();

SaxParser p = factory.newSAXParser ();

Page 65: 6   xml parsing

XML Parsing | Atul Kahate 65

3 – Create an Event Handler Event handler responds to

parsing events It is a subclass of DefaultHandler

public class MyHandler extends DefaultHandler { … }

Main event methods (callbacks) startDocument, endDocument startElement, endElement characters, ignoreableWhitespace

Page 66: 6   xml parsing

XML Parsing | Atul Kahate 66

3 – Create an Event Handler Example method: startElementDeclaration

public void startElement (String nameSpaceURI, String localName, String qualifiedName, Attributes attributes)

throws SASExceptionArgumentsnameSpaceURI URI identifying the namespace uniquelylocalName Element name without namespace prefixqualifiedName Complete element name, including

namespace prefixattributes Attributes object, representing attributes of the

element

Page 67: 6   xml parsing

XML Parsing | Atul Kahate 67

3 – Create an Event Handler

nameSpaceURI

<cwp:book xmlns:cwp=“http://www.test.com/xml/”>

qualifiedName attribute[1]

<cwp:chapter number=“23” part=“Server programming”>

<cwp:title> XML made easy </cwp:title></cwp:chapter>

localName

</cwp:book>

Page 68: 6   xml parsing

XML Parsing | Atul Kahate 68

4 – Invoke the Parser Call the parse method, supplying:

The content handler The XML document

File or Input stream

p.parse (file name, handler);

Page 69: 6   xml parsing

XML Parsing | Atul Kahate 69

Sample XML File (emp.xml) <?xml version="1.0" encoding="UTF-8"?>

<root> <employee>test 1</employee> <employee>test 1</employee> <employee>test 1</employee> <employee>test 1</employee> <employee>test 1</employee> <employee>test 1</employee> <employee>test 1</employee> </root>

Page 70: 6   xml parsing

XML Parsing | Atul Kahate 70

Java Program to Count Total Number of Elements import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.*; import org.xml.sax.helpers.DefaultHandler;

public class SAXEmployeeCount extends DefaultHandler { int tagCount = 0;

public void startElement (String uri, String localName, String rawName, Attributes attributes) { tagCount++;

}

public void endDocument() { System.out.println("There are " + tagCount + " elements."); }

public static void main(String[] args) { SAXEmployeeCount handler = new SAXEmployeeCount ();

try { SAXParserFactory spf = SAXParserFactory.newInstance (); SAXParser parser = spf.newSAXParser ();

parser.parse("employee.xml", handler); } catch (Exception ex) { System.out.println(ex); } } }

Page 71: 6   xml parsing

XML Parsing | Atul Kahate 71

Count Only Book Elements<?xml version="1.0"?><books> <book category="reference"> <author>Nigel Rees</author> <title>Sayings of the Century</title> <price>8.95</price> </book> <book category="fiction"> <author>Evelyn Waugh</author> <title>Sword of Honour</title> <price>12.99</price> </book> <book category="fiction"> <author>Herman Melville</author> <title>Moby Rick</title> <price>8.99</price> </book></books>

Page 72: 6   xml parsing

XML Parsing | Atul Kahate 72

Parsing Code in JAXP import java.io.IOException; import java.lang.*; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.Locator; import org.xml.sax.SAXException; import org.xml.sax.SAXNotRecognizedException; import org.xml.sax.SAXNotSupportedException; import org.xml.sax.SAXParseException; import org.xml.sax.XMLReader; import org.xml.sax.ext.LexicalHandler; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.ParserAdapter; import org.xml.sax.helpers.XMLReaderFactory;

public class BookCount extends DefaultHandler{

private int count = 0;

public void startDocument() throws SAXException { System.out.println("Start document ..."); }

public void startElement(String uri, String local, String raw, Attributes attrs) throws SAXException {

int year = 0; String attrValue;

System.out.println ("Current element = " + raw);

if (raw.equals ("book")) { count++; } }

public void endDocument() throws SAXException { System.out.println("The total number of books = " + count); }

public static void main (String[] args) throws Exception { BookCount handler = new BookCount ();

try { SAXParserFactory spf = SAXParserFactory.newInstance (); SAXParser parser = spf.newSAXParser (); parser.parse ("book.xml", handler); } catch (SAXException e) { System.err.println(e.getMessage()); } } }

Page 73: 6   xml parsing

XML Parsing | Atul Kahate 73

Specifying Parser Name import java.io.IOException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.Locator; import org.xml.sax.SAXException; import org.xml.sax.SAXNotRecognizedException; import org.xml.sax.SAXNotSupportedException; import org.xml.sax.SAXParseException; import org.xml.sax.XMLReader; import org.xml.sax.ext.LexicalHandler; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.ParserAdapter; import org.xml.sax.helpers.XMLReaderFactory;

public class SAXApp extends DefaultHandler{

// default parser to use protected static final String DEFAULT_PARSER_NAME = "org.apache.xerces.parsers.SAXParser";

private int count = 0;

public void countTopics () throws IOException, SAXException { // create parser try { System.out.println ("Inside countTopics"); } catch (Exception e) { e.printStackTrace(System.err); } }

public void startElement(String uri, String local, String raw, Attributes attrs) throws SAXException { if (raw.equals("topic")) count++; System.out.println (raw); }

public void endDocument() throws SAXException { System.out.println("There are " + count + " topics"); }

public static void main (String[] args) throws Exception{ System.out.println ("Inside main ...");

SAXApp handler = new SAXApp();

try { SAXParserFactory spf = SAXParserFactory.newInstance (); SAXParser parser = spf.newSAXParser (); parser.parse ("contents.xml", handler); } catch (SAXException e) { System.err.println(e.getMessage()); } } }

Page 74: 6   xml parsing

XML Parsing | Atul Kahate 74

Exercise Consider the following XML file and write a program to

count the number of elements that have at least one attribute.

<?xml version="1.0"?><BOOKS>

<BOOK pubyear="1929"><BOOK_TITLE>Look Homeward, Angel</BOOK_TITLE><AUTHOR>Wolfe, Thomas</AUTHOR>

</BOOK><BOOK pubyear="1973">

<BOOK_TITLE>Gravity's Rainbow</BOOK_TITLE><AUTHOR>Pynchon, Thomas</AUTHOR>

</BOOK><BOOK pubyear="1977">

<BOOK_TITLE>Cards as Weapons</BOOK_TITLE><AUTHOR>Jay, Ricky</AUTHOR>

</BOOK><BOOK pubyear="2001">

<BOOK_TITLE>Computer Networks</BOOK_TITLE><AUTHOR>Tanenbaum, Andrew</AUTHOR>

</BOOK></BOOKS>

Page 75: 6   xml parsing

XML Parsing | Atul Kahate 75

Solution import java.io.IOException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.Locator; import org.xml.sax.SAXException; import org.xml.sax.SAXNotRecognizedException; import org.xml.sax.SAXNotSupportedException; import org.xml.sax.SAXParseException; import org.xml.sax.XMLReader; import org.xml.sax.ext.LexicalHandler; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.ParserAdapter; import org.xml.sax.helpers.XMLReaderFactory;

public class countAttr extends DefaultHandler{ private int count = 0; public void startDocument() throws SAXException { System.out.println("Start document ..."); } public void startElement(String uri, String local, String raw, Attributes attrs) throws SAXException { System.out.println ("Current element = " + raw); if (attrs.getLength () != 0) { count++; } } public void endDocument() throws SAXException { System.out.println("The total number of attributes = " + count); }

public static void main (String[] args) throws Exception { countAttr handler = new countAttr (); try { SAXParserFactory spf = SAXParserFactory.newInstance (); SAXParser parser = spf.newSAXParser (); parser.parse ("countAttr.xml", handler); } catch (SAXException e) { System.err.println(e.getMessage()); } } }

Page 76: 6   xml parsing

XML Parsing | Atul Kahate 76

Exercise For the same XML file, display

element names only if the book is published in the 1970s.

Page 77: 6   xml parsing

XML Parsing | Atul Kahate 77

Solution import java.io.IOException; import java.lang.*; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.Locator; import org.xml.sax.SAXException; import org.xml.sax.SAXNotRecognizedException; import org.xml.sax.SAXNotSupportedException; import org.xml.sax.SAXParseException; import org.xml.sax.XMLReader; import org.xml.sax.ext.LexicalHandler; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.ParserAdapter; import org.xml.sax.helpers.XMLReaderFactory;

public class seventiesBooks extends DefaultHandler{ private int count = 0; public void startDocument() throws SAXException { System.out.println("Start document ..."); } public void startElement(String uri, String local, String raw, Attributes attrs) throws SAXException { int year = 0; String attrValue; System.out.println ("Current element = " + raw); if (attrs.getLength () > 0) { attrValue = attrs.getValue (0); year = Integer.parseInt (attrValue); if (year < 1970) { count++; } } } public void endDocument() throws SAXException { System.out.println("The total number of matching elements = " + count); }

public static void main (String[] args) throws Exception { seventiesBooks handler = new seventiesBooks(); try { SAXParserFactory spf = SAXParserFactory.newInstance (); SAXParser parser = spf.newSAXParser (); parser.parse ("countAttr.xml", handler); } catch (SAXException e) { System.err.println(e.getMessage()); } } }

Page 78: 6   xml parsing

XML Parsing | Atul Kahate 78

Exercise Consider the following XML document (stock.xml) <?xml version="1.0"?>

<stock> <stockinfo symbol="IFL"> <company>i-flex solutions limited</company> <price>2500</price> </stockinfo> <stockinfo symbol="HLL"> <company>Hindustan Lever</company> <price>1840</price> </stockinfo> <stockinfo symbol="LT"> <company>Laresn and Toubro</company> <price>2678</price> </stockinfo> <stockinfo symbol="Rel"> <company>Reliance Communications</company> <price>1743</price> </stockinfo> </stock> Produce output as shown on the next slide

Page 79: 6   xml parsing

XML Parsing | Atul Kahate 79

Expected Output

Page 80: 6   xml parsing

XML Parsing | Atul Kahate 80

Solution import java.io.*; import org.xml.sax.*; import org.xml.sax.helpers.*; import javax.xml.parsers.*;

public class DisplayStockDetails extends DefaultHandler {

public void startDocument () throws SAXException { System.out.println ("\nDisplaying Stock Details"); System.out.println ("=========================\n"); }

public void endDocument () throws SAXException { System.out.println ("\nEnd of Details"); System.out.println ("==============\n"); }

public void startElement (String uri, String local, String raw, Attributes attrs) throws SAXException {

// Skip processing root element if (local.equals ("stock")) return;

// Skip processing if there are no attributes if (attrs == null) return;

for (int i=0; i<attrs.getLength (); i++) { System.out.println ("[Symbol: " + attrs.getValue (i) + "]"); } }

public void endElement (String uri, String local, String raw) throws SAXException { // System.out.println (); }

public void characters (char[] ch, int start, int length) throws SAXException { System.out.println (new String (ch, start, length)); }

public static void main (String[] args) throws Exception {

DisplayStockDetails handler = new DisplayStockDetails ();

try { SAXParserFactory spf = SAXParserFactory.newInstance (); SAXParser parser = spf.newSAXParser (); parser.parse ("stock.xml", handler); } catch (SAXException e) { System.err.println(e.getMessage()); } } }

Page 81: 6   xml parsing

XML Parsing | Atul Kahate 81

Exercise Consider the following XML file and write a program to find out and display the total cost for

all CDs.

<?xml version="1.0" encoding="ISO-8859-1"?> <catalog> <cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <country>USA</country> <company>Columbia</company> <price>10.90</price> <year>1985</year> </cd> <cd> <title>Candle in the wind</title> <artist>Elton John</artist> <country>UK</country> <company>HMV</company> <price>8.20</price> <year>1998</year> </cd> </catalog>

Page 82: 6   xml parsing

XML Parsing | Atul Kahate 82

Solution import java.io.IOException; import java.lang.*; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.Locator; import org.xml.sax.SAXException; import org.xml.sax.SAXNotRecognizedException; import org.xml.sax.SAXNotSupportedException; import org.xml.sax.SAXParseException; import org.xml.sax.XMLReader; import org.xml.sax.ext.LexicalHandler; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.ParserAdapter; import org.xml.sax.helpers.XMLReaderFactory;

public class CDPrice extends DefaultHandler{

private int count = 0, total = 0; private boolean flagIsAvailable = false, flagIsCurrentElementPrice = false;

public void startDocument() throws SAXException { System.out.println("Start document ..."); }

public void startElement(String uri, String local, String raw, Attributes attrs) throws SAXException {

int year = 0; String attrValue;

System.out.println ("Current element = " + raw);

if (raw.equals ("price")) { flagIsCurrentElementPrice = true; System.out.println ("INSIDE if of startElement ==="); }

}

public void characters (char [] ch, int start, int len) throws SAXException {

if (flagIsCurrentElementPrice) {

System.out.println ("ch = " + ch); System.out.println ("start = " + start); System.out.println ("len = " + len);

StringBuffer buffer = new StringBuffer ();

for (int i=0; i<len; i++) { buffer.append (ch[start+i]); }

System.out.println ("*** buffer = " + buffer + " ***");

String str = buffer.substring (0); int uprice = Integer.parseInt(str);

total += uprice; flagIsCurrentElementPrice = false; System.out.println ("Current total = " + total);

}

}

public void endDocument() throws SAXException { System.out.println("The total price of available CDs = " + total); }

public static void main (String[] args) throws Exception { CDPrice handler = new CDPrice();

try { SAXParserFactory spf = SAXParserFactory.newInstance (); SAXParser parser = spf.newSAXParser (); parser.parse ("cdcatalog2.xml", handler); } catch (SAXException e) { System.err.println(e.getMessage()); } } }

Page 83: 6   xml parsing

Document Object Model (DOM)

Page 84: 6   xml parsing

XML Parsing | Atul Kahate 84

DOM – Basic Flow

Page 85: 6   xml parsing

XML Parsing | Atul Kahate 85

Basic Concepts

Page 86: 6   xml parsing

XML Parsing | Atul Kahate 86

JAXP and DOM – Overview Class DocumentBuilderFactory

public abstract class javax.xml.parsers.DocumentBuilderFactory extends java.lang.object

Defines a factory API that enables applications to obtain a parser that produces DOM object trees from XML documents

parse method: Parses the contents of an XML document and returns the contents as a new Document object

Page 87: 6   xml parsing

XML Parsing | Atul Kahate 87

JAXP and DOM – Overview Class DocumentBuilder

public abstract class javax.xml.parsers. DocumentBuilder extends java.lang.Object

Defines the API to obtain DOM Document instances from an XML document

Page 88: 6   xml parsing

XML Parsing | Atul Kahate 88

JAXP and DOM – Overview Interface Document

public interface Document extends Node

The Document interface represents the entire HTML or XML document

Conceptually, it is the root of the document tree, and provides the primary access to the document's data

Page 89: 6   xml parsing

XML Parsing | Atul Kahate 89

JAXP and DOM – Overview Interface Element

public interface Element extends Node The Element interface represents an

element in an HTML or XML document Elements may have attributes associated

with them Inherits from Node, the generic Node

interface attributes may be used to retrieve the set of

all attributes for an element

Page 90: 6   xml parsing

XML Parsing | Atul Kahate 90

JAXP and DOMDocumentBuilderFactory factory =

DocumentBuilderFactory.newInstance ();DocumentBuilder builder =

factory.newDocumentBuilder ();Document document = builder.parse (fileName);Element root = document.getDocumentElement

();

Page 91: 6   xml parsing

XML Parsing | Atul Kahate 91

Example – XML File Count the number of Employee elements from this XML

using DOM <?xml version="1.0" encoding="UTF-8"?> <root> <employee>test 1</employee> <employee>test 1</employee> <employee>test 1</employee> <employee>test 1</employee> <employee>test 1</employee> <employee>test 1</employee> <employee>test 1</employee> </root>

Page 92: 6   xml parsing

XML Parsing | Atul Kahate 92

Example – Java Code package javaapplication1;

import org.w3c.dom.*;

public class Main { public static void main(String[] args) {

try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance (); DocumentBuilder builder = factory.newDocumentBuilder (); Document document = builder.parse ("cdcatalog.xml"); Element root = document.getDocumentElement (); NodeList nodes = document.getElementsByTagName("employee"); System.out.println("There are " + nodes.getLength() + " elements.");

} catch (Exception ex) { System.out.println(ex); } } }

Page 93: 6   xml parsing

XML Parsing | Atul Kahate 93

Check if a File is Well-Formed package sicsr; import javax.xml.parsers.*;

public class IsWellFormed {

/** * @param args */ public static void main(String[] args) {

try { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder domBuilder = domFactory.newDocumentBuilder(); domBuilder.parse("NWF.xml"); }

catch (org.xml.sax.SAXException ex) { System.out.println("File is not well-formed"); }

catch (FactoryConfigurationError ex) { System.out.println(ex.toString ()); }

catch (ParserConfigurationException ex) { System.out.println(ex.toString ()); }

catch (Exception ex) { System.out.println(ex.toString ());

} } }

Page 94: 6   xml parsing

XML Parsing | Atul Kahate 94

JAXP Code to Open an XML Fileimport org.w3c.dom.*;import javax.xml.parsers.*;import org.xml.sax.*;

public class DOMExample1 { public static void main (String[] args) { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance (); DocumentBuilder builder = factory.newDocumentBuilder (); Document document = builder.parse ("cdcatalog.xml"); Element root = document.getDocumentElement (); System.out.println ("In main ... XML file openend successfully ..."); } catch (ParserConfigurationException e1) { System.out.println ("Exception: " + e1); } catch (SAXException e2) { System.out.println ("Exception: " + e2); } catch (java.io.IOException e3) { System.out.println ("Exception: " + e3); } }}

Page 95: 6   xml parsing

XML Parsing | Atul Kahate 95

Case Study – XML File<?xml version="1.0"?><catalog> <cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <country>USA</country> <company>Columbia</company> <price>10</price> <year>1985</year> </cd> <cd> <title>Candle in the wind</title> <artist>Elton John</artist> <country>UK</country> <company>HMV</company> <price>8</price> <year>1998</year> </cd></catalog>

Page 96: 6   xml parsing

XML Parsing | Atul Kahate 96

Problem Write a program to find out if an

element by the name price exists in the XML file and display its contents

Page 97: 6   xml parsing

XML Parsing | Atul Kahate 97

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

public class DOMExample2 { public static void main (String[] args) { NodeList elements; String elementName = "price"; try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance (); DocumentBuilder builder = factory.newDocumentBuilder (); Document document = builder.parse ("cdcatalog.xml"); Element root = document.getDocumentElement (); System.out.println ("In main ... XML file openend successfully ..."); elements = document.getElementsByTagName(elementName);

// is there anything to do? if (elements == null) { return; }

// print all elements int elementCount = elements.getLength(); System.out.println ("Count = " + elementCount); for (int i = 0; i < elementCount; i++) { Element element = (Element) elements.item(i); System.out.println ("Element Name = " + element.getNodeName()); System.out.println ("Element Type = " + element.getNodeType()); System.out.println ("Element Value = " + element.getNodeValue()); System.out.println ("Has attributes = " + element.hasAttributes()); } } catch (ParserConfigurationException e1) { System.out.println ("Exception: " + e1); } catch (SAXException e2) { System.out.println ("Exception: " + e2); } catch (DOMException e2) { System.out.println ("Exception: " + e2); } catch (java.io.IOException e3) { System.out.println ("Exception: " + e3); } } }

Page 98: 6   xml parsing

XML Parsing | Atul Kahate 98

Problem Write a program to display

element names and their attribute names and values

Page 99: 6   xml parsing

XML Parsing | Atul Kahate 99

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

public class DOMExample3 { public static void main (String[] args) { NodeList elements; String elementName = "cd"; try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance (); DocumentBuilder builder = factory.newDocumentBuilder (); Document document = builder.parse ("cdcatalog.xml"); Element root = document.getDocumentElement (); System.out.println ("In main ... XML file openend successfully ..."); elements = document.getElementsByTagName(elementName);

// is there anything to do? if (elements == null) { return; }

// print all elements int elementCount = elements.getLength(); System.out.println ("Count = " + elementCount); for (int i = 0; i < elementCount; i++) { Element element = (Element) elements.item(i); System.out.println ("Element Name = " + element.getNodeName()); System.out.println ("Element Type = " + element.getNodeType()); System.out.println ("Element Value = " + element.getNodeValue()); System.out.println ("Has attributes = " + element.hasAttributes()); // If attributes exist, print them if(element.hasAttributes()) { // if it does, store it in a NamedNodeMap object NamedNodeMap AttributesList = element.getAttributes(); // iterate through the NamedNodeMap and get the attribute names and values for(int j = 0; j < AttributesList.getLength(); j++) { System.out.println("Attribute: " + AttributesList.item(j).getNodeName() + " = " + AttributesList.item(j).getNodeValue()); } } } } catch (ParserConfigurationException e1) { System.out.println ("Exception: " + e1); } catch (SAXException e2) { System.out.println ("Exception: " + e2); } catch (DOMException e2) { System.out.println ("Exception: " + e2); } catch (java.io.IOException e3) { System.out.println ("Exception: " + e3); } } }

Page 100: 6   xml parsing

XML Parsing | Atul Kahate 100

Problem For a given element, find out all

the child elements and display their types

Page 101: 6   xml parsing

XML Parsing | Atul Kahate 101

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

public class DOMExample4 { public static void main (String[] args) { NodeList elements, Children; String elementName = "cd"; String local = ""; Element element = null; try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance (); DocumentBuilder builder = factory.newDocumentBuilder (); Document document = builder.parse ("cdcatalog.xml"); Element root = document.getDocumentElement (); System.out.println ("In main ... XML file openend successfully ..."); elements = document.getElementsByTagName(elementName);

// is there anything to do? if (elements == null) { return; }

// print all elements int elementCount = elements.getLength(); System.out.println ("Count = " + elementCount); for (int i = 0; i < elementCount; i++) { element = (Element) elements.item(i); System.out.println ("Element Name = " + element.getNodeName()); System.out.println ("Element Type = " + element.getNodeType()); System.out.println ("Element Value = " + element.getNodeValue()); System.out.println ("Has attributes = " + element.hasAttributes()); // Find out if child nodes exist for this element Children = element.getChildNodes();

if (Children != null) { for (int j=0; j< Children.getLength(); j++) { local = Children.item(j).getNodeName(); System.out.println ("Child element name = " + local); } } } } catch (ParserConfigurationException e1) { System.out.println ("Exception: " + e1); } catch (SAXException e2) { System.out.println ("Exception: " + e2); } catch (DOMException e2) { System.out.println ("Exception: " + e2); } catch (java.io.IOException e3) { System.out.println ("Exception: " + e3); } } }

Page 102: 6   xml parsing

XML Parsing | Atul Kahate 102

Node Types 1 ELEMENT_NODE Element The element name

2 ATTRIBUTE_NODE Attribute The attribute name 3 TEXT_NODE Text #text 4 CDATA_SECTION_NODE CDATA #cdata-section 5 ENTITY_REFERENCE_NODE Entity reference The entity reference name 6 ENTITY_NODE Entity The entity name 7 PROCESSING_INSTRUCTION_NODE PI The PI target 8 COMMENT_NODE Comment #comment 9 DOCUMENT_NODE Document #document 10 DOCUMENT_TYPE_NODE DocType Root element 11 DOCUMENT_FRAGMENT_NODE DocumentFragment #document-fragment 12 NOTATION_NODE Notation The notation name

Page 103: 6   xml parsing

XML Parsing | Atul Kahate 103

Making Use of Node Types import org.w3c.dom.*; import javax.xml.parsers.*; import org.xml.sax.*;

public class DOMExample4 { public static void main (String[] args) { NodeList elements, Children; String elementName = "cd"; String local = ""; Element element = null; try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance (); DocumentBuilder builder = factory.newDocumentBuilder (); Document document = builder.parse ("cdcatalog.xml"); Element root = document.getDocumentElement (); System.out.println ("In main ... XML file openend successfully ..."); elements = document.getElementsByTagName(elementName);

// is there anything to do? if (elements == null) { return; }

// print all elements int elementCount = elements.getLength(); System.out.println ("Count = " + elementCount); for (int i = 0; i < elementCount; i++) { element = (Element) elements.item(i); System.out.println ("Element Name = " + element.getNodeName()); System.out.println ("Element Type = " + element.getNodeType()); System.out.println ("Element Value = " + element.getNodeValue()); System.out.println ("Has attributes = " + element.hasAttributes()); // Find out if child nodes exist for this element Children = element.getChildNodes();

if (Children != null) { for (int j=0; j< Children.getLength(); j++) { local = Children.item(j).getNodeName(); System.out.println ("Child element name = " + local); } } } } catch (ParserConfigurationException e1) { System.out.println ("Exception: " + e1); } catch (SAXException e2) { System.out.println ("Exception: " + e2); } catch (DOMException e2) { System.out.println ("Exception: " + e2); } catch (java.io.IOException e3) { System.out.println ("Exception: " + e3); } } }

Page 104: 6   xml parsing

XML Parsing | Atul Kahate 104

Problem Write a program to create XML

contents dynamically and write them to a file on the disk

Page 105: 6   xml parsing

XML Parsing | Atul Kahate 105

Solution import java.io.File; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer;

import org.w3c.dom.*; import javax.xml.parsers.*; import org.xml.sax.*; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.Source; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.Result; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory;

public class DOMExample5 { public static void main (String[] args) { Source source; File file; Result result; try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance (); DocumentBuilder builder = factory.newDocumentBuilder (); // Create a new XML document Document document = builder.newDocument (); Element root = (Element) document.createElement("Order"); // Insert child Manifest document.appendChild(root); Node manifestChild = document.createElement("Manifest"); root.appendChild(manifestChild); // Insert Items CreateOrderDOM co = new CreateOrderDOM (); co.insertItem(document, manifestChild, "101", "Name one", "$29.99"); co.insertItem(document, manifestChild, "108", "Name two", "$19.99"); co.insertItem(document, manifestChild, "125", "Name three", "$39.99"); co.insertItem(document, manifestChild, "143", "Name four", "$59.99"); co.insertItem(document, manifestChild, "118", "Name five", "$99.99");

// Normalizing the DOM document.getDocumentElement().normalize(); // Prepare the DOM document for writing source = new DOMSource(document);

// Prepare the output file file = new File("test.xml"); result = new StreamResult(file);

// Write the DOM document to the file // Get Transformer Transformer xformer = TransformerFactory.newInstance().newTransformer(); // Write to a file xformer.transform(source, result);

} catch ( Exception ex ) { ex.printStackTrace(); } }

}

Page 106: 6   xml parsing

Stream API (StAX) – Brief Overview

To be covered in more depth in “Web Services”

Page 107: 6   xml parsing

XML Parsing | Atul Kahate 107

What is StAX? Addition in Java EE 5.0 Pull approach Event-based API Different from SAX, since

application pulls event from the XML document/parser, and not the other way round

Can do read and write

Page 108: 6   xml parsing

XML Parsing | Atul Kahate 108

StAX Classification Two APIs

Cursor-based API Allows walk-through of an XML document

in document order and Provides access to all the structural and

content information in the form of event objects

Iterator-based API Similar to cursor API, but does not

provide low level access

Page 109: 6   xml parsing

Using XSLT in JAXP

Page 110: 6   xml parsing

XML Parsing | Atul Kahate 110

Applying an XSLT to an XML File Programatically package sicsr;

import javax.xml.transform.*; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import java.io.*;

public class ApplyXSLT {

public static void main(String[] args) {

try {

StreamSource xmlFile = new StreamSource (new File ("history.xml")); StreamSource xslFile = new StreamSource (new File ("history.xsl"));

TransformerFactory xslFactory = TransformerFactory.newInstance(); Transformer transformer = xslFactory.newTransformer (xslFile);

StreamResult resultStream = new StreamResult (System.out);

transformer.transform(xmlFile, resultStream); }

catch (Exception ex) { ex.printStackTrace(); }

}

}

Page 111: 6   xml parsing

XML Parsing | Atul Kahate 111

Details about the Transformer TransformerFactory is an abstract

class in javax.xml.transform package Can be used to create a Transformer

object Transformer is also an abstract class

in javax.xml.transform.Transformer package An instance of this class can transform a

source tree into a result tree

Page 112: 6   xml parsing

XML and ASP.NET – An Overview

Page 113: 6   xml parsing

XML Parsing | Atul Kahate 113

XmlReader and XmlWriter XMLReader

Pull-style API for XML Forward-only, read-only access to XML

documents XMLReader is an abstract class that other

classes derive from, to provide specific concrete instances such as XmlTextReader and XmlNodeReader

In ASP.NET 2.0, XMLReader is a factory We need not specify which implementation of

XMLReader needs to be used We use a static Create method, and supply necessary

parameters and let .NET decide how to instantiate it

Page 114: 6   xml parsing

XML Parsing | Atul Kahate 114

Example – XML Document <?xml version="1.0" encoding="utf-8" ?> <bookstore> <book genre ="autobiography" publicationdate="1981" ISBN="1-861003-11-0"> <title>The Autobiography of Benjamin Franklin</title> <author> <first-name>Benjamin</first-name> <last-name>Franklin</last-name> </author> <price>8.99</price> </book> <book genre ="novel" publicationdate="1967" ISBN="0-201-65512-2"> <title>The Confidence Man</title> <author> <first-name>Herman</first-name> <last-name>Melville</last-name> </author> <price>11.99</price> </book> <book genre ="philosophy" publicationdate="1991" ISBN="1-861001-57-6"> <title>The Gorgias</title> <author> <first-name>Sidas</first-name> <last-name>Plato</last-name> </author> <price>9.99</price> </book> </bookstore>

Page 115: 6   xml parsing

XML Parsing | Atul Kahate 115

Example – ASP.NET Page using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Xml; using System.IO;

public partial class XMLReader2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { int bookCount = 0; XmlReaderSettings settings = new XmlReaderSettings();

settings.IgnoreWhitespace = true; settings.IgnoreComments = true;

string booksFile = Path.Combine(Request.PhysicalApplicationPath, "Books.xml");

using (XmlReader reader = XmlReader.Create(booksFile, settings)) { while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && "book" == reader.LocalName) { bookCount++; } } } Response.Write(String.Format("Found {0} books!", bookCount));

} }

Page 116: 6   xml parsing

XML Parsing | Atul Kahate 116

Validating an XML Against a Schema using System.Xml.Schema; using System; using System.Xml; using System.IO;

public partial class XMLReader3 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { int bookCount = 0; XmlReaderSettings settings = new XmlReaderSettings();

string booksSchemaFile = Path.Combine(Request.PhysicalApplicationPath, "books.xsd"); settings.Schemas.Add (null, XmlReader.Create (booksSchemaFile)); settings.ValidationType = ValidationType.Schema; settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings; settings.ValidationEventHandler += new ValidationEventHandler (settings_ValidationEventHandler); settings.IgnoreWhitespace = true; settings.IgnoreComments = true;

string booksFile = Path.Combine(Request.PhysicalApplicationPath, "Books.xml");

using (XmlReader reader = XmlReader.Create(booksFile, settings)) { while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && "book" == reader.LocalName) { bookCount++; } } } Response.Write(String.Format("Found {0} books!", bookCount)); }

void settings_ValidationEventHandler(object sender, System.Xml.Schema.ValidationEventArgs e) { Response.Write(e.Message); } }

Page 117: 6   xml parsing

XML Parsing | Atul Kahate 117

Creating an XML Document using System.Xml.Schema; using System; using System.Xml; using System.IO;

public partial class XMLReader3 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { int bookCount = 0; XmlReaderSettings settings = new XmlReaderSettings();

string booksSchemaFile = Path.Combine(Request.PhysicalApplicationPath, "books.xsd"); settings.Schemas.Add (null, XmlReader.Create (booksSchemaFile)); settings.ValidationType = ValidationType.Schema; settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings; settings.ValidationEventHandler += new ValidationEventHandler (settings_ValidationEventHandler); settings.IgnoreWhitespace = true; settings.IgnoreComments = true;

string booksFile = Path.Combine(Request.PhysicalApplicationPath, "Books.xml");

using (XmlReader reader = XmlReader.Create(booksFile, settings)) { while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && "book" == reader.LocalName) { bookCount++; } } } Response.Write(String.Format("Found {0} books!", bookCount)); }

void settings_ValidationEventHandler(object sender, System.Xml.Schema.ValidationEventArgs e) { Response.Write(e.Message); } }

Page 118: 6   xml parsing

Thank you!

Any Questions?