1 Java and XML Modified from presentation by: Barry Burd Drew University Portions © 2002 Hungry Minds, Inc.

Slides:



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

Technische universität dortmund Service Computing Service Computing Prof. Dr. Ramin Yahyapour IT & Medien Centrum 22. Oktober 2009.
SDPL 2002Notes 3: XML Processor Interfaces1 3.3 JAXP: Java API for XML Processing n How can applications use XML processors? –A Java-based answer: through.
Internet Technologies 1 Lecture 1 Introduction.
Summer A-2000, Project Course-- Carnegie Mellon University 1 Financial Engineering Project Course.
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.
1 The Simple API for XML (SAX) Part I ©Copyright These slides are based on material from the upcoming book, “XML and Bioinformatics” (Springer-
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.
Tomcat Java and XML. Announcements  Final homework assigned Wednesday  Two week deadline  Will cover servlets + JAXP.
Parsing XML into programming languages JAXP, DOM, SAX, JDOM/DOM4J, Xerces, Xalan, JAXB.
Xerces The Apache XML Project Yvonne Yao. Introduction Set of libraries that provides functionalities to parse XML documents Set of libraries that provides.
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.
Summer A-2000, Project Course-- Carnegie Mellon University 1 Financial Engineering Project Course.
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.
JAX- Java APIs for XML by J. Pearce. Some XML Standards Basic –SAX (sequential access parser) –DOM (random access parser) –XSL (XSLT, XPATH) –DTD Schema.
17 Apr 2002 XML Programming: SAX Andy Clark. SAX Design Premise Generic method of creating XML parser, parsing documents, and receiving document information.
Chapter 24 XML. CHAPTER GOALS Understanding XML elements and attributes Understanding the concept of an XML parser Being able to read and write XML documents.
SDPL : (XML APIs) JAXP1 3.3 JAXP: Java API for XML Processing n How can applications use XML processors? –In Java: through JAXP –An overview of.
Xpath Sources:
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools Leonidas Fegaras.
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools Leonidas Fegaras.
17 Apr 2002 XML Programming: JAXP Andy Clark. Java API for XML Processing Standard Java API for loading, creating, accessing, and transforming XML documents.
The Joy of SAX (and DOM, and JDOM…) Bill MacCartney 11 October 2004.
SDPL 2003Notes 3: XML Processor Interfaces1 3. XML Processor APIs n How can applications manipulate structured documents? –An overview of document parser.
XML – Extensible Markup Language. Objectives To understand various ways in which XML can be used History of XML Syntax of XML Difference between HTML,
XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks.
Representing Web Data: XML CSI 3140 WWW Structures, Techniques and Standards.
Representing Web Data: XML CSI 3140 WWW Structures, Techniques and Standards.
Advanced Java Session 9 New York University School of Continuing and Professional Studies.
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools.
3/29/2001 O'Reilly Java Java API for XML Processing 1.1 What’s New Edwin Goei Engineer, Sun Microsystems.
EXtensible Markup Language (XML) James Atlas July 15, 2008.
SDPL 2002Notes 3: XML Processor Interfaces1 3. XML Processor APIs n How can applications manipulate structured documents? –An overview of document parser.
SDPL 20113: XML APIs and SAX1 3. XML Processor APIs n How can (Java) applications manipulate structured (XML) documents? –An overview of XML processor.
Extensible MarkUp Language. AGENDA  OVERVIEW OF XML  DATA TYPE DEFINITION LANGUAGE  XML SCHEMA  XML PARSERS 1) DOM PARSER 2) SAX PARSER 3) JAXB PARSER.
XML Processing in Java. Required tools Sun JDK 1.4, e.g.: JAXP (part of Java Web Services Developer Pack, already in Sun.
Java API for XML Processing (JAXP) Dr. Rebhi S. Baraka Advanced Topics in Information Technology (SICT 4310) Department of Computer.
Sheet 1XML Technology in E-Commerce 2001Lecture 3 XML Technology in E-Commerce Lecture 3 DOM and SAX.
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools.
Document Object Model DOM. Agenda l Introduction to DOM l Java API for XML Parsing (JAXP) l Installation and setup l Steps for DOM parsing l Example –Representing.
Java and XML. What is XML XML stands for eXtensible Markup Language. A markup language is used to provide information about a document. Tags are added.
© Marty Hall, Larry Brown Web core programming 1 Simple API for XML SAX.
XML and SAX (A quick overview) ● What is XML? ● What are SAX and DOM? ● Using SAX.
Schema Data Processing
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.
1 Introduction JAXP. Objectives  XML Parser  Parsing and Parsers  JAXP interfaces  Workshops 2.
SDPL 20063: XML Processor Interfaces1 3. XML Processor APIs n How can (Java) applications manipulate structured (XML) documents? –An overview of XML processor.
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.
SDPL 2001Notes 3: XML Processor Interfaces1 3. XML Processor APIs n How applications can manipulate structured documents? –An overview of document parser.
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Chapter 7 Representing Web Data:
1 Introduction SAX. Objectives 2  Simple API for XML  Parsing an XML Document  Parsing Contents  Parsing Attributes  Processing Instructions  Skipped.
Java API for XML Processing
Simple API for XML SAX. Agenda l Introduction to SAX l Installation and setup l Steps for SAX parsing l Defining a content handler l Examples Printing.
Parsing with SAX using Java Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
{ XML Technologies } BY: DR. M’HAMED MATAOUI
Unit 4 Representing Web Data: XML
Parsing XML into programming languages
Chapter 24 XML.
Java/XML.
Chapter 7 Representing Web Data: XML
Jagdish Gangolly State University of New York at Albany
Java API for XML Processing
DOM 8-Dec-18.
A parser for XML Documents
DOM 24-Feb-19.
XML document processing in Java using XPath and XSLT
SAX2 29-Jul-19.
Presentation transcript:

1 Java and XML Modified from presentation by: Barry Burd Drew University Portions © 2002 Hungry Minds, Inc.

2 Outline Review of XML Discussion of some Java XML APIs includingDiscussion of some Java XML APIs –SAX –DOM

3 Review of XML Hello Start tag End tag Processing instruction Characters (text)

4 Review of XML <?xml version="1.0" encod Hello Element Attribute name Attribute value

5 Review of XML... Empty Elements Entity references

6 Document Type Definition (DTD) <!ATTLIST note pitch CDATA #REQUIRED>...Etc.

7 A Doc that’s not well-formed Hello world! How are you? <!-- Oh, no! This start tag should be an end tag! --> Comment

8 An Invalid Document Hello world! <!-- No Question element in the DTD --> How are you?

9 A valid document Hello world! How are you?

10 Outline Review of XML Discussion of some Java XML APIs –What APIs are availableWhat APIs are available –SAXSAX –DOMDOM –Validation, namespaces, etc.Validation, namespaces, etc. –Creating a new XML document using DOMCreating a new XML document using DOM –Using XSLUsing XSL

11 Some of the Java APIs JAXP (Java API for XML Processing) –SAX (Simple API for XML) –DOM (Document Object Model) –JAXP comes standard with J2SE 1.4 –It is installed on cerebro if you use the path /usr/local/linux-jdk1.3.1/bin

12 SAX Event driven Deals with start tags, end tags, etc. No inherent notion of an element No inherent notion of nesting No in-memory copy of the whole document

13 import javax.xml.parsers.*; import org.xml.sax.*; import java.io.*; class CallSAX { static public void main(String[] args) throws SAXException, ParserConfigurationException, IOException { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); XMLReader xmlReader = saxParser.getXMLReader(); xmlReader.setContentHandler (new MyContentHandler()); //...(more) Calling SAX

14 import javax.xml.parsers.*; import org.xml.sax.*; import java.io.*; class CallSAX { static public void main(String[] args) throws SAXException, ParserConfigurationException, IOException { //...Stuff from previous slide xmlReader.parse (new File(“weather.xml"). toURL().toString()); } Calling SAX

15 A Content Handler import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.Attributes; class MyContentHandler extends DefaultHandler { public void startDocument() { System.out.println ("Starting the document."); } // More...

16 A Content Handler (cont’d) public void startElement(String uri, String localName, String qualName, Attributes attribs) { System.out.print("Start tag: "); System.out.println(qualName); for (int i=0; i<attribs.getLength(); i++) { System.out.print("Attribute: "); System.out.print(attribs.getQName(i)); System.out.print(" = "); System.out.println(attribs.getValue(i)); }

17 A Content Handler (cont’d) public void characters (char[] charArray, int start, int length) { String charString = new String(charArray, start, length); charString = charString.replaceAll("\n", "[cr]"); charString = charString.replaceAll(" ", "[blank]"); System.out.print(length + " characters: "); System.out.println(charString); }

18 A Content Handler (cont’d) public void endElement(String uri, String localName, String qualName) { System.out.print("End tag: "); System.out.println(qualName); } public void endDocument() { System.out.println("Ending the document."); }

19 A Sample Document White Plains NY Sat Jul am EDT

20 The Output Starting the document. Start tag: WeatherReport 1 characters: $ Start tag: City 12 characters: White Plains End tag: City 1 characters: $ Start tag: State 2 characters: NY End tag: State 1 characters: $ Start tag: Date 15 characters: Sat Jul End tag: Date 1 characters: $ Start tag: Time 9 characters: 11 am EDT End tag: Time 1 characters: $ Start tag: CurrTemp Attribute: unit = Farenheit 2 characters: 70 End tag: CurrTemp 1 characters: $ Start tag: High Attribute: unit = Farenheit 2 characters: 82 End tag: High 1 characters: $ Start tag: Low Attribute: unit = Farenheit 2 characters: 62 End tag: Low 1 characters: $ End tag: WeatherReport Ending the document.

21 DOM Not event driven Creates an in-memory copy of the document Deals with document nodes Nodes are nested Elements are nodes, but so are attributes, text, processing instructions, comments, the entire document itself No inherent notion of a tag

22 Calling DOM import javax.xml.parsers.*; import org.xml.sax.SAXException; import java.io.*; import org.w3c.dom.Document; public class CallDOM { public static void main(String args[]) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc; // More... }

23 Calling DOM import javax.xml.parsers.*; import org.xml.sax.SAXException; import java.io.*; import org.w3c.dom.Document; public class CallDOM { public static void main(String args[]) throws ParserConfigurationException, SAXException, IOException { //... Stuff from previous slide if (args.length == 1) { doc = builder.parse (new File(args[0]).toURL().toString()); new MyTreeTraverser (doc); } else System.out.println ("Usage: java CallDOM file-name.xml"); }

24 class MyTreeTraverser { String indent=""; MyTreeTraverser (Node node) { traverse(node); } void traverse(Node node){ displayName(node); displayValue(node); if (node.getNodeType() == Node.ELEMENT_NODE) displayAttributes(node); indent=indent.concat(" "); displayChildren(node); indent=indent.substring(0,indent.length()-3); } // …stuff void displayChildren(Node node) { Node child = node.getFirstChild(); while (child != null) { traverse(child); child = child.getNextSibling(); } A Tree Traverser

25 The Output Name: #document Value: null Name: WeatherReport Value: null Name: #text Value: Name: City Value: null Name: #text Value: White Plains Name: #text Value: Name: State Value: null Name: #text Value: NY Name: #text Value: Name: Date Value: null Name: #text Value: Sat Jul Name: #text Value: Name: Time Value: null Name: #text Value: 11 am EDT Name: #text Value: Name: CurrTemp Value: null Attribute: Unit = Farenheit Name: #text Value: 70 Name: #text Value: Name: High Value: null Attribute: Unit = Farenheit Name: #text Value: 82 Name: #text Value: Name: Low Value: null Attribute: Unit = Farenheit Name: #text Value: 62 Name: #text Value:

26 Setting an Error Handler In SAX: xmlReader.setErrorHandler new MyErrorHandler()); In DOM builder.setErrorHandler (new MyErrorHandler());

27 An Error Handler import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.SAXParseException; class MyErrorHandler extends DefaultHandler { public void error(SAXParseException e) { System.out.println("Error:"); showSpecifics(e); System.out.println(); } public void showSpecifics(SAXParseException e) { System.out.println(e.getMessage()); System.out.println (" Line " + e.getLineNumber()); System.out.println (" Column " + e.getColumnNumber()); System.out.println (" Document " + e.getSystemId()); }

28 import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.SAXParseException; class MyErrorHandler extends DefaultHandler { public void warning(SAXParseException e) { System.out.println("Warning:"); showSpecifics(e); System.out.println(); } public void fatalError(SAXParseException e) { System.out.println("Fatal error:"); showSpecifics(e); System.out.println(); } //... } An Error Handler (cont’d)

29 Turning on DTD Validation // must do this before using the factory factory.setValidating(true);

30 The Output Error: Attribute value for "Unit" is #REQUIRED. Line 9 Column -1 Document file:/usr/local/www/data/csci380/02s/examples/xml/DOMvalid/weather2.xml Name: #document Value: null Name: WeatherReport Value: null Name: WeatherReport Value: null …

31 About This Book Conventions Used in This Book A "Hello" Example Doing Validation A "Hello" Example Creating a new document Using XSL

32 <xsl:stylesheet xmlns:xsl= version="1.0"> Part <xsl:value-of <xsl:value-of An XSL Stylesheet

33     <xsl:if <xsl:value-of <xsl:value-of #160;       •     Appendix <xsl:value-of <xsl:value-of An XSL Stylesheet (cont’d)

34 JavaAndXMLforDummies.htm Java and XML For Dummies Introduction • About This Book • Conventions Used in This Book Part I: The Big Picture Chapter 1: SAX... Etc.

35 Java and XML For Dummies Introduction About This Book Conventions Used in This Book Part I: The Big Picture Chapter 1: SAX A "Hello" Example Doing Validation Chapter 2: DOM A "Hello" Example Creating a new document Appendixes Appendix A: Things to Remember about Java Appendix B: Things to Remember about XML

36 Applying XSL with Java Code import javax.xml.transform.TransformerFactory; import javax.xml.transform.Transformer; import javax.xml.transform.stream.StreamSource; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerConfigurationException; import java.io.FileOutputStream; import java.io.FileNotFoundException; import java.io.IOException; More...

37 Applying XSL with Java Code (cont’d) public class MyTransform { public static void main(String[] args) throws TransformerException, TransformerConfigurationException, FileNotFoundException, IOException { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer (new StreamSource("JavaAndXMLforDummies.xsl")); transformer.transform (new StreamSource("JavaAndXMLforDummies.xml"), new StreamResult (new FileOutputStream("JavaAndXMLforDummies.htm"))); }

38 The Usual Scenario... Client Browser http GET request for JavaAndXMLforDummies.jsp HTTP server Servlet container.xml file.xsl file.jsp file.htm file response