WaysInJavaToParseXML

Slides:



Advertisements
Similar presentations
Technische universität dortmund Service Computing Service Computing Prof. Dr. Ramin Yahyapour IT & Medien Centrum 22. Oktober 2009.
Advertisements

The Semantic Web. The Web Today Designed for Human to read Cannot express meaning Architecture: URL –Decentralized: Link structure Language: html.
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.
Lecture 4 Java Interfaces (review of inheritance and abstract classes) The XML DOM Java Examples Homework 3.
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.
Xerces The Apache XML Project Yvonne Yao. Introduction Set of libraries that provides functionalities to parse XML documents Set of libraries that provides.
1 Processing XML with Java CS , Spring 2008/9.
Apache DOM Parser©zwzOctober 24, 2002 Wenzhong Zhao Department of Computer Science The University of Kentucky.
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.
Xpath Sources:
XML Android Club Agenda XML JSON XML eXtensible Markup Language Used for: data transfer PHP -> Java.
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.
XML Anisha K J Jerrin Thomas. Outline  Introduction  Structure of an XML Page  Well-formed & Valid XML Documents  DTD – Elements, Attributes, Entities.
1 XML at a neighborhood university near you Innovation 2005 September 16, 2005 Kwok-Bun Yue University of Houston-Clear Lake.
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools.
Parsing with DOM using MSXML Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
DOM Robin Burke ECT 360. Outline XHTML in Schema JavaScript DOM (MSXML) Loading/Parsing Transforming parameter passing DOM operations extracting data.
Intro to XML Originally Presented by Clifford Lemoine Modified by Box.
The Main Routine In these notes I call it DataImporter. It contains: A main routine the creates an instance of IndexerData, the root of our data model.
XML Processing in Java. Required tools Sun JDK 1.4, e.g.: JAXP (part of Java Web Services Developer Pack, already in Sun.
SDPL 2002Notes 3.2: Document Object Model1 3.2 Document Object Model (DOM) n How to provide uniform access to structured documents in diverse applications.
Sheet 1XML Technology in E-Commerce 2001Lecture 3 XML Technology in E-Commerce Lecture 3 DOM and SAX.
DOM Programming The Document Object Model standardises  what an application can see of the XML data  how it can access it An XML structure is a tree.
XML Refresher Course Bálint Joó School of Physics University of Edinburgh May 02, 2003.
1 Dr Alexiei Dingli XML Technologies SAX and DOM.
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.
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 eXtensible Markup Language. XML A method of defining a format for exchanging documents and data. –Allows one to define a dialect of XML –A library.
XML and SAX (A quick overview) ● What is XML? ● What are SAX and DOM? ● Using SAX.
XML. DCS – SWC 2 Data vs. Information We often use the terms data and information interchangeably More precisely, data is some ”value” of a certain type,
Schema Data Processing
1 JAXP & XPATH. Objectives 2  XPath  JAXP Processing of XPath  Workshops.
CS 157B: Database Management Systems II February 13 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.
 defined as Extensible Markup Language (XML) is a set of rules for encoding documents  Defines structure and data.
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.
Web services. DOM parsing and SOAP.. Summary. ● Exercise: SAX-Based checkInvoice(), ● push parsing, ● event-based parsing, ● traversal order is depth-first.
Java Web 应用开发: J2EE 和 Tomcat 蔡 剑, Ph.D.. 本讲内容 Web 层技术 (III) Custom Tags JSP and XML JSTL.
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.
1 Validation SAX-DOM. Objectives 2  Schema Validation Framework  XML Validation After Transformation  Workshops.
USING ANDROID WITH THE DOM. Slide 2 Lecture Summary DOM concepts SAX vs DOM parsers Parsing HTTP results The Android DOM implementation.
XML DOM Week 11 Web site:
XML & JSON. Background XML and JSON are to standard, textual data formats for representing arbitrary data – XML stands for “eXtensible Markup Language”
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.
USING ANDROID WITH THE INTERNET. Slide 2 Lecture Summary Getting network permissions Working with the HTTP protocol Sending HTTP requests Getting results.
XML. Contents  Parsing an XML Document  Validating XML Documents.
{ XML Technologies } BY: DR. M’HAMED MATAOUI
XML Parsers Overview Types of parsers Using XML parsers SAX DOM
In this session, you will learn to:
Chapter 24 XML.
Java/XML.
{ XML Technologies } BY: DR. M’HAMED MATAOUI
More DOM 13-Nov-18.
Week 11 Web site: XML DOM Week 11 Web site:
DOM Document Object Model.
XML Parsers Overview Types of parsers Using XML parsers SAX DOM
More Sample XML By Sadia Anjum.
DOM 8-Dec-18.
WaysInJavaToParseXML
More DOM.
DOM 24-Feb-19.
CS 240 – Advanced Programming Concepts
XML document processing in Java using XPath and XSLT
XML Parsers.
XML Programming in Java
Presentation transcript:

WaysInJavaToParseXML Prof: Dr. Shu-Ching Chen TA: Sheng Guan

Parse XML with Java DOM API How to do this ? The Java DOM API for XML parsing is intended for working with XML as an object graph in memory - a "Document Object Model (DOM)". The parser traverses the XML file and creates the corresponding DOM objects. DOM objects are linked together in a tree structure.

Object model ( DOM Tree)

Steps: 1. Creating A Java DOM XML Parser 2. Parsing XML with a Java DOM Parser 3.Java DOM: Get the Document Object

XML file and corresponding DOM structure <book> <title>Fun Software</title> <author>Jakob Jenkov</author> <ISBN>0123456789</ISBN> </book>

DOM - 3 pieces of XML 1. Elements (sometimes called tags) 2. Attributes 3. The data (also called values) that the elements and attributes describe

Step 1: Creating a Java DOM XML parser Creating a Java DOM XML parser is done using the javax.xml.parsers.DocumentBuilderFactory class. Here is an example: DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = null; try { builder = builderFactory.newDocumentBuilder(); } catch (ParserConfigurationException e) { e.printStackTrace(); }

Step 2: Parsing an XML file into a DOM tree try { Document document = builder.parse( new FileInputStream("/path/to/your/file.xml")); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { }

Step 3: Java DOM: Get the Document Object You are now ready to traverse the Document instance you have received from the DocumentBuilder; The DOM Document object represents an XML document. When you parse an XML file using a Java DOM parser, you get back a Document object.

The DOM Document Element The two most commonly used features of DOM are: 1.Accessing Child Elements of an Element 2.Accessing Attributes of an Element A DOM object contains a lot of different nodes connected in a tree-like structure. At the top is the Document object. The Document object has a single root element, which is returned by calling getDocumentElement(): Element rootElement = document.getDocumentElement();

DOM Elements, Child Elements, and the Node Interface The root element has children. You get the children of an element like this: NodeList nodes = element.getChildNodes(); for(int i=0; i<nodes.getLength(); i++){ Node node = nodes.item(i); if(node instanceof Element){ //a child element to process Element child = (Element) node; String attribute = child.getAttribute("width"); } http://www3.ntu.edu.sg/home/ehchua/programming/sql/relational_database_design.html

DOM Element Attributes The getChildNodes() method returns a NodeList object, which is a list of Node elements. The Node interface is a superinterface for all of the different node types in DOM The Element interface extends Node and you can access the attributes of an element via the Element interface: String attrValue = element.getAttribute("attrName"); http://www3.ntu.edu.sg/home/ehchua/programming/sql/relational_database_design.html

Examine sub-elements //returns a list of subelements of specified name getElementsByTagName("subelementName"); //returns a list of all child nodes getChildNodes(); A whole complete example is in the below link: https://www.tutorialspoint.com/java_xml/java_dom_ parse_document.htm http://www3.ntu.edu.sg/home/ehchua/programming/sql/relational_database_design.html

Reference https://sanaulla.info/2013/05/23/parsing-xml- using-dom-sax-and-stax-parser-in-java/ https://docs.oracle.com/cd/B28359_01/appdev.1 11/b28394/adx_j_parser.html http://tutorials.jenkov.com/java-xml/dom.html https://www.tutorialspoint.com/java_xml/java_dom _parse_document.htm Drafting out the sample input forms, queries and reports, often helps.