28-Jun-15 StAX Streaming API for XML. XML parser comparisons DOM is Memory intensive Read-write Typically used for documents smaller than 10 MB SAX is.

Slides:



Advertisements
Similar presentations
J0 1 Marco Ronchetti - Web architectures – Laurea Specialistica in Informatica – Università di Trento Java XML parsing.
Advertisements

Introduction to Java 2 Programming Lecture 7 IO, Files, and URLs.
PRESENTED BY SASANKA SEKHAR BANERJEE. During this presentation, we will discuss the following: Need for XML Brief overview of XML Different methods of.
XML, Uploading, Importing... Joshua Scotton.
1 XML: Advanced Guide Holly A. Hyland, FSA Andrew Smalera, XML Framework Session 14.
XML Parsing Using Java APIs AIP Independence project Fall 2010.
1 SAX and more… CS , Spring 2008/9. 2 SAX Parser SAX = Simple API for XML XML is read sequentially When a parsing event happens, the parser invokes.
SAX A parser for XML Documents. XML Parsers What is an XML parser? –Software that reads and parses XML –Passes data to the invoking application –The application.
14-Jun-15 DOM. SAX and DOM SAX and DOM are standards for XML parsers-- program APIs to read and interpret XML files DOM is a W3C standard SAX is an ad-hoc.
31 Signs That Technology Has Taken Over Your Life: #6. When you go into a computer store, you eavesdrop on a salesperson talking with customers -- and.
XML Parser. Why Need a XML Parser ? Check XML syntax. ( is well-formed ? ) Validation. ( DTD and XML Schema ) Allow programmatic access to the document’s.
21-Jun-15 SAX (Abbreviated). 2 XML Parsers SAX and DOM are standards for XML parsers-- program APIs to read and interpret XML files DOM is a W3C standard.
Java API for XML Processing (JAXP) CSE 4/586: Distributed Systems Department of Computer Science and Engineering University at Buffalo, New York Jia Zhao.
Fonts and colors Times New Roman “quotes” Trebuchet "quotes" yellow blue pink green violet.
26-Jun-15 SAX. SAX and DOM SAX and DOM are standards for XML parsers--program APIs to read and interpret XML files DOM is a W3C standard SAX is an ad-hoc.
CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start.
29-Jun-15 JAXB Java Architecture for XML Binding.
CS 898N – Advanced World Wide Web Technologies Lecture 22: Applying XML Chin-Chih Chang
Apache DOM Parser©zwzOctober 24, 2002 Wenzhong Zhao Department of Computer Science The University of Kentucky.
XML: Java Dr Andy Evans. Java and XML Couple of things we might want to do: Parse/write data as XML. Load and save objects as XML. We’ll mainly discuss.
Processing of structured documents Spring 2003, Part 5 Helena Ahonen-Myka.
Java Programming: I/O1 Java I/O Reference: java.sun.com/docs/books/tutorial/essential/io/
Networking Nasrullah. Input stream Most clients will use input streams that read data from the file system (FileInputStream), the network (getInputStream()/getInputStream()),
XML processing in ColdFusion MX Everything you wanted to know about XML, but were afraid to ask October 2006 Jaxfusion User Group.
Structured-Document Processing Languages Spring 2011 Course Review Repetitio mater studiorum est!
XML eXtensible Markup Language w3c standard Why? Store and transport data Easy data exchange Create more languages WSDL (Web Service Description Language)
SAX Parsing Presented by Clifford Lemoine CSC 436 Compiler Design.
Very Brief Introduction to Java I/O with Buffered Reader and Buffered Writer.
XML Parsers Overview  Types of parsers  Using XML parsers  SAX  DOM  DOM versus SAX  Products  Conclusion.
SAX. What is SAX SAX 1.0 was released on May 11, SAX is a common, event-based API for parsing XML documents Primarily a Java API but there implementations.
Beginning XML 4th Edition. Chapter 12: Simple API for XML (SAX)
Softsmith Infotech XML. Softsmith Infotech XML EXtensible Markup Language XML is a markup language much like HTML Designed to carry data, not to display.
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Java API for XML Processing (JAXP) Dr. Rebhi S. Baraka Advanced Topics in Information Technology (SICT 4310) Department of Computer.
SDPL Streaming API for XML1 3.4 Streaming API for XML (StAX) n Could we process XML documents more conveniently than with SAX, and yet more efficiently?
C# and Windows Programming XML Processing. 2 Contents Markup XML DTDs XML Parsers DOM.
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools.
Apache DOM Parser©zwzOctober 24, 2002 Wenzhong Zhao Department of Computer Science The University of Kentucky.
SAX2 and DOM2 Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
XML and SAX (A quick overview) ● What is XML? ● What are SAX and DOM? ● Using SAX.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
CS 157B: Database Management Systems II February 13 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.
When we create.rtf document apart from saving the actual info the tool saves additional info like start of a paragraph, bold, size of the font.. Etc. This.
What is XML? eXtensible Markup Language eXtensible Markup Language A subset of SGML (Standard Generalized Markup Language) A subset of SGML (Standard Generalized.
XML and Object Serialization. Structure of an XML Document Header Root Element Start Tags / End Tags Element Contents – Child Elements – Text – Both (mixed.
1 Introduction JAXP. Objectives  XML Parser  Parsing and Parsers  JAXP interfaces  Workshops 2.
Simple API for XML (SAX) Aug’10 – Dec ’10. Introduction to SAX Simple API for XML or SAX was developed as a standardized way to parse an XML document.
7-Mar-16 Simple API XML.  SAX and DOM are standards for XML parsers-- program APIs to read and interpret XML files  DOM is a W3C standard  SAX is an.
13-Mar-16 DOM. 2 Difference between SAX and DOM DOM reads the entire XML document into memory and stores it as a tree data structure SAX reads the XML.
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Chapter 7 Representing Web Data:
USING ANDROID WITH THE DOM. Slide 2 Lecture Summary DOM concepts SAX vs DOM parsers Parsing HTTP results The Android DOM implementation.
XML & JSON. Background XML and JSON are to standard, textual data formats for representing arbitrary data – XML stands for “eXtensible Markup Language”
1 Introduction SAX. Objectives 2  Simple API for XML  Parsing an XML Document  Parsing Contents  Parsing Attributes  Processing Instructions  Skipped.
21-Jun-16 Document Object Model DOM. SAX and DOM SAX and DOM are standards for XML parsers-- program APIs to read and interpret XML files DOM is a W3C.
Java API for XML Processing
XML 1.Introduction to XML 2.Document Type Definition (DTD) 3.XML Parser 4.Example: CGI Gateway to XML Middleware.
Apache Axis2 AXIOM - AXis Object Model. 2 Agenda Processing XML / Why AXIOM? AXIOM Architecture  Builders  Deferred Building  Caching  Factories 
XML Parsers Overview Types of parsers Using XML parsers SAX DOM
In this session, you will learn to:
Unit 4 Representing Web Data: XML
Java XML IS
Java/XML.
XML processing in ColdFusion MX
Chapter 7 Representing Web Data: XML
XML Parsers Overview Types of parsers Using XML parsers SAX DOM
Java API for XML Processing
A parser for XML Documents
Python and XML Styling and other issues XML
File Input and Output.
XML and Web Services (II/2546)
Presentation transcript:

28-Jun-15 StAX Streaming API for XML

XML parser comparisons DOM is Memory intensive Read-write Typically used for documents smaller than 10 MB SAX is Memory efficient Read only Typically used for documents larger than 10 MB StAX is Memory efficient Read-write Appropriate for documents of all sizes Easier to use than DOM or SAX Source: Murach’s Java SE 6

Creating a StAX reader Everything you need specifically for working with StAX is in javax.xml.stream import javax.xml.stream.* As with SAX and DOM, you first need to get a factory by calling a static method XMLInputFactory factory = XMLInputFactory.newInstance(); You can set some properties on this factory factory.setProperty("javax.xml.stream.isValidating", "true"); factory.setProperty("javax.xml.stream.isCoalescing", "true"); You will need to have some way to supply the XML, usually from an InputStream or a Reader FileReader fileReader = new FileReader(" somefile.xml"); This line could throw a FileNotFoundException You can then create a reader for the XML XMLStreamReader reader = factory.createXMLStreamReader(fileReader); This line could throw an XMLStreamException

Using the StAX parser A StaAX parser (reader) behaves similar to an Iterator The boolean hasNext() method tells you if there is another “event” to read The int next() method reads the next event However, what it returns is an int that tells what kind of event it was Some values are START_ELEMENT, END_ELEMENT, ATTRIBUTE, CHARACTERS (content), COMMENT, SPACE, END After doing next(), the parser has a “current element,” and can be asked questions about that element For example, getLocalName() returns the name of the current element It is important to remember that the parser only moves forward; it’s easy to go past the information you need It’s easier to parse a document if you know its structure A validating parser can check the structure against a DTD isValidating is a supported feature of Java’s current StAX parser

Using getLocalName() getLocalName() returns the tag name as a String Since you can’t use a switch statement with a String, your code will probably look something like this: String name = reader.getLocalName(); if (name.equals( someTag )) {... } else if (name.equals( someOtherTag )) {... } else if (name.equals( someOtherTag )) {... }...

Constants next() moves to the next event, and returns an int to tell what type it is START_DOCUMENT END_DOCUMENT START_ELEMENT END_ELEMENT ATTRIBUTE CHARACTERS COMMENT SPACE DTD PROCESSING_INSTRUCTION NAMESPACE CDATA ENTITY_REFERENCE T hese are all constants defined in the XMLStreamReader object

Methods There are numerous methods defined in the XMLStreamReader object; here are some of the most important: boolean hasNext() – true if there is another “event” int next() – gets next parsing event, return its type int nextTag() – gets next start or end element, return its type getEventType() – gets next parsing event, returns its type getLocalName() – gets the name of the current element or entity reference getAttributeCount() – gets the number of attributes of the current element getAttributeLocalName( index ) – gets the name of the current attribute getAttributeValue( index ) – gets the value of the current attribute getElementText() – returns the coalesced text of a START_ELEMENT ; after the call, the current event will be the corresponding END_ELEMENT getText() – returns the text value of a CHARACTERS, COMMENT, ENTITY_REFERENCE, CDATA, SPACE, or DTD

Creating a StAX writer Everything you need specifically for working with StAX is in javax.xml.stream import javax.xml.stream.* As with SAX and DOM, you first need to get a factory by calling a static method XMLOutputFactory factory = XMLOutputFactory.newInstance(); You will need to have somewhere to put the XML, usually to an OutputStream or a Writer FileWriter fileWriter = new FileWriter(" somefile.xml"); This line could throw an IOException You can then create a writer for the XML XMLStreamWriter writer = factory.createXMLStreamWriter(fileWriter); This line could throw an XMLStreamException

Methods There are numerous methods defined in the XMLStreamWriter object; here are some of the most important: writeStartDocument( version ) – writes the XML header writeStartElement( name ) – writes a start tag writeAttribute( name, value ) – writes an attribute writeCharacters( value ) – writes text, encoding & characters writeComment( value ) – writes a comment writeDTD( value ) – writes an (entire) DTD writeEndElement() – writes an end tag for the current start tag flush() – forces buffered output to be actually written close() – close the writer

Conclusions Advantages of StAX: Simpler to use than SAX, in part because it doesn’t use callbacks Can write XML files as well as read them Disadvantages of StAX Practically forces you to use an extended if-then-else to recognize what has been parsed As with SAX, you can only go forward Does not (currently) provide validation capabilities Comparison with DOM: StAX is much faster, more memory efficient, and somewhat simpler DOM lets you manipulate trees in memory

The End