Java WWW Week 10 Version 2.1 Mar 2008 Slide Java (JSP) and XML  Format of lecture: What is XML? A sample XML file… How to use.

Slides:



Advertisements
Similar presentations
CSCI N241: Fundamentals of Web Design Copyright ©2004 Department of Computer & Information Science Introducing XHTML: Module B: HTML to XHTML.
Advertisements

1/7 ITApplications XML Module Session 8: Introduction to Programming with XML.
XML: Extensible Markup Language
1 XSLT – eXtensible Stylesheet Language Transformations Modified Slides from Dr. Sagiv.
1 Extensible Markup Language: XML HTML: portable, widely supported protocol for describing how to format data XML: portable, widely supported protocol.
1 Introduction to XML. XML eXtensible implies that users define tag content Markup implies it is a coded document Language implies it is a metalanguage.
XML Parsing Using Java APIs AIP Independence project Fall 2010.
1 Extensible Markup Language: XML HTML: portable, widely supported protocol for describing how to format data XML: portable, widely supported protocol.
XML Introduction What is XML –XML is the eXtensible Markup Language –Became a W3C Recommendation in 1998 –Tag-based syntax, like HTML –You get to make.
XML(EXtensible Markup Language). XML XML stands for EXtensible Markup Language. XML is a markup language much like HTML. XML was designed to describe.
XML –Query Languages, Extracting from Relational Databases ADVANCED DATABASES Khawaja Mohiuddin Assistant Professor Department of Computer Sciences Bahria.
Introducing XHTML: Module B: HTML to XHTML. Goals Understand how XHTML evolved as a language for Web delivery Understand the importance of DTDs Understand.
Introducing HTML & XHTML:. Goals  Understand hyperlinking  Understand how tags are formed and used.  Understand HTML as a markup language  Understand.
Xpath Sources:
4/20/2017.
DHTML. What is DHTML?  DHTML is the combination of several built-in browser features in fourth generation browsers that enable a web page to be more.
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 9 JavaServer Pages (JSP) (Based on Møller.
XML eXtensible Markup Language by Darrell Payne. Experience Logicon / Sterling Federal C, C++, JavaScript/Jscript, Shell Script, Perl XML Training XML.
1 XML at a neighborhood university near you Innovation 2005 September 16, 2005 Kwok-Bun Yue University of Houston-Clear Lake.
Copyright © 2012 Accenture All Rights Reserved.Copyright © 2012 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are.
Introduction to XPath Transparency No. 1 Introduction to XPath API Cheng-Chia Chen.
XSLT for Data Manipulation By: April Fleming. What We Will Cover The What, Why, When, and How of XSLT What tools you will need to get started A sample.
School of Computing and Management Sciences © Sheffield Hallam University To understand the Oracle XML notes you need to have an understanding of all these.
XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.
Introduction to XML Eugenia Fernandez IUPUI. What is XML? From the World Wide Web Consortium (W3C) The Extensible Markup Language (XML) is the universal.
1 © Netskills Quality Internet Training, University of Newcastle Introducing XML © Netskills, Quality Internet Training University.
XML 1 Enterprise Applications CE00465-M XML. 2 Enterprise Applications CE00465-M XML Overview Extensible Mark-up Language (XML) is a meta-language that.
Chapter 27 The World Wide Web and XML. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.27-2 Topics in this Chapter The Web and the Internet.
Electronic Commerce COMP3210 Session 4: Designing, Building and Evaluating e-Commerce Initiatives – Part II Dr. Paul Walcott Department of Computer Science,
Intro to XML Originally Presented by Clifford Lemoine Modified by Box.
JSTL, XML and XSLT An introduction to JSP Standard Tag Library and XML/XSLT transformation for Web layout.
XML Extensible Markup Language
Copyrighted material John Tullis 10/17/2015 page 1 04/15/00 XML Part 3 John Tullis DePaul Instructor
XML 2nd EDITION Tutorial 1 Creating An Xml Document.
Date : 3/3/2010 Web Technology Solutions Class: Application Syndication: Parse and Publish RSS & XML Data.
WEB BASED DATA TRANSFORMATION USING XML, JAVA Group members: Darius Balarashti & Matt Smith.
XP 1 Creating an XML Document Developing an XML Document for the Jazz Warehouse XML Tutorial.
Web Technologies COMP6115 Session 4: Adding a Database to a Web Site Dr. Paul Walcott Department of Computer Science, Mathematics and Physics University.
Chapter 27 The World Wide Web and XML. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.27-2 Topics in this Chapter The Web and the Internet.
An Introduction to XML Paul Donohue May 8th 2002 Hotel Senator Zürich.
XML Design Goals 1.XML must be easily usable over the Internet 2.XML must support a wide variety of applications 3.XML must be compatible with SGML 4.It.
1 Tutorial 11 Creating an XML Document Developing a Document for a Cooking Web Site.
XML and Its Applications Ben Y. Zhao, CS294-7 Spring 1999.
XML and Database.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 4 1COMP9321, 15s2, Week.
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.
XML and Object Serialization. Structure of an XML Document Header Root Element Start Tags / End Tags Element Contents – Child Elements – Text – Both (mixed.
SCHOOL OF LIBRARY, ARCHIVE AND INFORMATION STUDIES Andy Dawson LIS1510 Library and Archives Automation Issues XML and extensible systems Andy Dawson School.
1 Introduction JAXP. Objectives  XML Parser  Parsing and Parsers  JAXP interfaces  Workshops 2.
Computing & Information Sciences Kansas State University Friday, 20 Oct 2006CIS 560: Database System Concepts Lecture 24 of 42 Friday, 20 October 2006.
©Silberschatz, Korth and Sudarshan10.1Database System Concepts W3C - The World Wide Web Consortium W3C - The World Wide Web Consortium.
C Copyright © 2011, Oracle and/or its affiliates. All rights reserved. Introduction to XML Standards.
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 1.Introduction to XML 2.Document Type Definition (DTD) 3.XML Parser 4.Example: CGI Gateway to XML Middleware.
XML Databases Presented By: Pardeep MT15042 Anurag Goel MT15006.
XML. Contents  Parsing an XML Document  Validating XML Documents.
XML BASICS and more…. What is XML? In common:  XML is a standard, simple, self-describing way of encoding both text and data so that content can be processed.
DHTML.
Unit 4 Representing Web Data: XML
WWW and HTTP King Fahd University of Petroleum & Minerals
Intro to XML.
XML QUESTIONS AND ANSWERS
{ XML Technologies } BY: DR. M’HAMED MATAOUI
XML in Web Technologies
Using Access and the Web
Creating an XML Document
WaysInJavaToParseXML
XML Problems and Solutions
XML document processing in Java using XPath and XSLT
Presentation transcript:

Java WWW Week 10 Version 2.1 Mar 2008 Slide Java (JSP) and XML  Format of lecture: What is XML? A sample XML file… How to use XML with JSP  Example code for parsing an XML file in JSP

Java WWW Week 10 Version 2.1 Mar 2008 Slide What is XML?  XML is principally concerned with the description and structure of data (content & presentation are separate)  Traditional methods of data storage and exchange employ a variety of schemes Electronically, these usually are of the form of simple text files (Comma Separated Values etc.) or binary files  Both methods have their own advantages and disadvantages CSV files contain data that can be easily read but lack a description of their own format Binary files contain data and a description of its data format (as a Word document does) but are proprietary schemes that require specific applications to read them

Java WWW Week 10 Version 2.1 Mar 2008 Slide What is XML?  XML stands for eXtensible Markup Language  Developed by World Wide Web Consortium (W3C)  Aims to provide the best of both worlds Stores data in an easy to read text format Also contains a description of the data  Open standard - looks similar to HTML (large user base) except the extensible nature of XML allows for the creation of user-defined tags  It is not a replacement for HTML (although it may eventually supplant it)

Java WWW Week 10 Version 2.1 Mar 2008 Slide What is XML?  XML offers a standard method for storing structured data  Language independence (English, Chinese etc.)  Hierarchical structure allows for simple and efficient querying/parsing of the document  Simple data interchange between applications and/or distributed objects

Java WWW Week 10 Version 2.1 Mar 2008 Slide What is XML?  Can improve upon and replace existing EDI (Electronic Document Interchange) solutions such as EDIFACT  Websites gain by having content and presentation separate A site could be developed purely in XML  Cost benefits No need for private EDI networks – the Internet used as exchange medium Reduced time to implement/reduced maintenance

Java WWW Week 10 Version 2.1 Mar 2008 Slide Illustrative Example Paper-based or Simple Text File Jonathan Westlake Staffordshire University Comma Separated Values Jonathan,Westlake,Staffordshire Binary – a string of 0s and 1s etc.

Java WWW Week 10 Version 2.1 Mar 2008 Slide Illustrative Example Simple ‘Freeform’ (i.e. without an XML schema) XML File Jonathan Westlake Staffordshire University

Java WWW Week 10 Version 2.1 Mar 2008 Slide How to use XML with JSP  XML is a big topic!  We are just going to look at one of the more fundamental aspects of XML  An XML parser simply checks that your XML document is syntactically correct (well-formed) and contains correctly formatted data (valid)  Once an XML document is parsed, the information it contains is accessible inside our web application  There are two ways of parsing an XML document – Simple API for XML (SAX) and Document Object Model (DOM) reference link

Java WWW Week 10 Version 2.1 Mar 2008 Slide SAX Parser  Event Driven : An event is triggered each time the parser encounters a beginning, or ending tag

Java WWW Week 10 Version 2.1 Mar 2008 Slide DOM Parser  A tree representing the document is built in memory.

Java WWW Week 10 Version 2.1 Mar 2008 Slide DOM Tree Structure contacts---contact | |---name | | | |---firstname | | └ ---Text | | | └ ---lastname | └ ---Text | |---workplace | └ ---Text | └ --- └ ---Text For our contacts example

Java WWW Week 10 Version 2.1 Mar 2008 Slide Standard DOM Parsing  Searching for a contact based on their address  Create a Document object  Load the XML file into the Document  Using iterations over the Document nodes, you can access any of the values or attributes that are stored in the XML file  When you find the record that contains the address that you are looking for, do something with the information

Standard DOM Parsing // Get a factory object (many ways to build a Document, this lets us choose) DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); // ensures the factory object returned is set to validate the XML data with the schema docBuilderFactory.setAttribute( " " docBuilderFactory.setValidating(true); docBuilderFactory.setNamespaceAware(true); // get the Document builder object that we will use to build the DOM tree in memory DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); // load the XML file into a new Document object Document doc = docBuilder.parse(new File(“myfile.xml”)); // does a bit of extra DOM processing (strips out empty nodes etc.) doc.normalize();

Standard DOM Parsing // get the whole node structure NodeList nodelist = getNodeList("/contacts", doc); for(int index = 0; index < nodelist.getLength(); index++) {Node node = nodelist.item(index); if(node.getNodeType(TEXT_NODE)) {if(node.getNodeName().equals(“firstname”)) firstname = node.getNodeValue(); if(node.getNodeName().equals(“lastname”)) lastname = node.getNodeValue(); if(node.getNodeName().equals(“workplace”)) workplace = node.getNodeValue(); if(node.getNodeName().equals(“ ”)) = node.getNodeValue(); { displayRecord(firstname, lastname, workplace, ); break; }

Java WWW Week 10 Version 2.1 Mar 2008 Slide Standard DOM Parsing Problems  The previous procedure is ok if you have a fairly ‘flat’ XML structure that does not contain many different node types  If you have many contacts stored then it may be very slow to iterate through the nodes until you find the contact you are looking for  For more complex XML documents, you can’t use a simple FOR loop to iterate  You end up with code that looks more like…

Java WWW Week 10 Version 2.1 Mar 2008 Slide Not Nice! Element root = doc.getDocumentElement(); Node configNode = root.getFirstChild(); NodeList childNodes = configNode.getChildNodes(); for (int childNum = 0; childNum < childNodes.getLength(); childNum++) { if ( childNodes.item(childNum).getNodeType() == Node.ELEMENT_NODE ) {Element child = (Element) childNodes.item( childNum ); if ( child.getTagName().equals( "header" ) ) {// Do something with the header System.out.print("Got a header!\n"); }

Java WWW Week 10 Version 2.1 Mar 2008 Slide XPath  XPath (XML Path Language) is a terse (short, non- XML) syntax for addressing portions of an XML document  A typical XPath expression is a Location Path consisting of a string of element or attribute qualifiers separated by forward slashes ("/"), similar in appearance to a file system path  E.g. this gets the field of the first contact //contact[1]/  NB would you expect to have seen //contact[0]/...?

Java WWW Week 10 Version 2.1 Mar 2008 Slide Using XPath  We can use the XPath syntax to retrieve any information we like from the Document  XPath is a new(ish) specification and initially it was only available via Java extensions  As of version 1.5 of the JDK, Java now natively supports XPath

Search Using XPath // create the XPath and initialize it XPath xPath = XPathFactory.newInstance().newXPath(); // now execute the XPath select statement to get the contact that matches the address NodeList nodes = nodelist, XPathConstants.NODESET); Node fNameNode = (NodeList) xPath.evaluate(//firstname’], nodes, XPathConstants.NODESET); String firstname = firstNameNode.getNodeValue(); Node lNameNode = (NodeList) xPath.evaluate(//lastname’], nodes, XPathConstants.NODESET); String lastname = lastNameNode.getNodeValue(); Node workNode = (NodeList) xPath.evaluate(//workplace’], nodes, XPathConstants.NODESET); String workplace = workPlaceNode.getNodeValue(); Node Node = (NodeList) xPath.evaluate(// ’], nodes, XPathConstants.NODESET); String = Node.getNodeValue();

Java WWW Week 10 Version 2.1 Mar 2008 Slide XMLHelper.java package xmlhelper; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; So JSP has access to a set of packages which include DOM and XPath

Java WWW Week 10 Version 2.1 Mar 2008 Slide

Java WWW Week 10 Version 2.1 Mar 2008 Slide getNodeListXPath() public static NodeList getNodeListXPath(String expression, Document target) throws XPathExpressionException { // create the XPath and initialize it XPath xPath = XPathFactory.newInstance().newXPath(); // now execute the XPath select statement NodeList nodeList = (NodeList) xPath.evaluate(expression, target, XPathConstants.NODESET); // return the resulting node return nodeList; }

Java WWW Week 10 Version 2.1 Mar 2008 Slide getBooleanXPath() public static boolean getBooleanXPath(String expression, Document target) throws XPathExpressionException { // create the XPath and initialize it XPath xPath = XPathFactory.newInstance().newXPath(); // now execute the XPath select statement Boolean nodeBoolean = (Boolean)xPath.evaluate(expression, target, XPathConstants.BOOLEAN); // return the resulting node return nodeBoolean.booleanValue(); }

Java WWW Week 10 Version 2.1 Mar 2008 Slide getNumberXPath() public static double getNumberXPath(String expression, Document target) throws XPathExpressionException { // create the XPath and initialize it XPath xPath = XPathFactory.newInstance().newXPath(); // now execute the XPath select statement Double nodeNumber = (Double)xPath.evaluate(expression, target, XPathConstants.NUMBER); // return the resulting node return nodeNumber.doubleValue(); }

Java WWW Week 10 Version 2.1 Mar 2008 Slide getStringXPath() public static String getStringXPath(String expression, Document target) throws XPathExpressionException { // create the XPath and initialize it XPath xPath = XPathFactory.newInstance().newXPath(); // now execute the XPath select statement String nodeText = (String)xPath.evaluate(expression, target, XPathConstants.STRING); // return the resulting node return nodeText; }

Java WWW Week 10 Version 2.1 Mar 2008 Slide Summary  XML is important as it offers: Neutral data exchange Can be built into a web application Can be searched for content using XPath  Java (and therefore JSP) can use XML and Xpath  Used widely in enterprise-scale information systems  No lecture next week but the first revision session in preparation of the module class test (short exam)