WaysInJavaToParseXML

Slides:



Advertisements
Similar presentations
XPathAPI XPathAPI HOME : API : j/apidocs/ Lib.
Advertisements

Technische universität dortmund Service Computing Service Computing Prof. Dr. Ramin Yahyapour IT & Medien Centrum 22. Oktober 2009.
1/7 ITApplications XML Module Session 8: Introduction to Programming with XML.
Class Scope class Student { private: string id; string firstName, lastName; float gpa; public: void Read() { cin >> id >> firstName >> lastName >> gpa;
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
The Semantic Web. The Web Today Designed for Human to read Cannot express meaning Architecture: URL –Decentralized: Link structure Language: html.
XML Parsers By Chongbing Liu. XML Parsers  What is a XML parser?  DOM and SAX parser API  Xerces-J parsers overview  Work with XML parsers (example)
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.
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.
1 Processing XML with Java Representation and Management of Data on the Internet A comprehensive tutorial about XML processing with JavaXML processing.
1 Processing XML with Java CS , Spring 2008/9.
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.
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.
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.
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.
1 XML at a neighborhood university near you Innovation 2005 September 16, 2005 Kwok-Bun Yue University of Houston-Clear Lake.
XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks.
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.
Introduction to XPath Transparency No. 1 Introduction to XPath API Cheng-Chia Chen.
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools.
1 Java and XML Modified from presentation by: Barry Burd Drew University Portions © 2002 Hungry Minds, Inc.
SDPLNotes 3.3: DOM Examples1 3.3 DOM Examples 1. DOM processing in a Web browser –a toy "XML database browser" –with JavaScript and MS Internet Explorer.
File Management Android Club Agenda Working with files Networking XML JSON.
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.
Java API for XML Processing (JAXP) Dr. Rebhi S. Baraka Advanced Topics in Information Technology (SICT 4310) Department of Computer.
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.
Consuming eXtensible Markup Language (XML) feeds.
1 4/13/01 CSE 121/131 Programming Spring 2001 Lecture Notes 7  A. Sahuguet & V.Tannen.
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.
SAX2 and DOM2 Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
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.
XML and Object Serialization. Structure of an XML Document Header Root Element Start Tags / End Tags Element Contents – Child Elements – Text – Both (mixed.
Java Web 应用开发: J2EE 和 Tomcat 蔡 剑, Ph.D.. 本讲内容 Web 层技术 (III) Custom Tags JSP and XML JSTL.
Document Object Model.  The XML DOM (Document Object Model) defines a standard way for accessing and manipulating XML documents.  The DOM presents an.
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.
1 Validation SAX-DOM. Objectives 2  Schema Validation Framework  XML Validation After Transformation  Workshops.
Chapter 26 XML. Chapter Goals Understanding XML elements and attributes Understanding the concept of an XML parser Being able to read and write XML documents.
USING ANDROID WITH THE DOM. Slide 2 Lecture Summary DOM concepts SAX vs DOM parsers Parsing HTTP results The Android DOM implementation.
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. Contents  Parsing an XML Document  Validating XML Documents.
{ XML Technologies } BY: DR. M’HAMED MATAOUI
Chapter 24 XML.
Java/XML.
{ XML Technologies } BY: DR. M’HAMED MATAOUI
Prof: Dr. Shu-Ching Chen TA: Sheng Guan
DOM Document Object Model.
XML Parsers By Chongbing Liu.
Computing Adjusted Quiz Total Score
Jagdish Gangolly State University of New York at Albany
DOM 8-Dec-18.
class PrintOnetoTen { public static void main(String args[]) {
A parser for XML Documents
DOM 24-Feb-19.
XML document processing in Java using XPath and XSLT
CPSC 233 Tutorial 13 March 11/12th, 2015.
WaysInJavaToParseXML
Presentation transcript:

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

Parse XML Purpose : populating the XML content into objects and then adding the objects to a list.

Sample XML <employees> <employee id="111"> <firstName>Rakesh</firstName> <lastName>Mishra</lastName> <location>Bangalore</location> </employee> <employee id="112"> <firstName>John</firstName> <lastName>Davis</lastName> <location>Chennai</location> <employee id="113"> ………………………………………………….

Using DOM Parser (1) The DOM Parser loads the complete XML content into a Tree structure. We iterate through the Node and NodeList to get the content of the XML.

Using DOM Parser (2) public class DOMParserDemo { public static void main(String[] args) throws Exception { //Get the DOM Builder Factory DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //Get the DOM Builder DocumentBuilder builder = factory.newDocumentBuilder();

Using DOM Parser (3) //Load and Parse the XML document //document contains the complete XML as a Tree. Document document = builder.parse( ClassLoader.getSystemResourceAsStream("xml/empl oyee.xml")); List<Employee> empList = new ArrayList<>(); //Iterating through the nodes and extracting the data.

Using DOM Parser (4) NodeList nodeList = document.getDocumentElement().getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { //We have encountered an <employee> tag. Node node = nodeList.item(i); if (node instanceof Element) { Employee emp = new Employee(); emp.id = node.getAttributes(). getNamedItem("id").getNodeValue();

Using DOM Parser (5) NodeList childNodes = node.getChildNodes(); for (int j = 0; j < childNodes.getLength(); j++) { Node cNode = childNodes.item(j); //Identifying the child tag of employee encountered. if (cNode instanceof Element) { String content = cNode.getLastChild(). getTextContent().trim(); switch (cNode.getNodeName()) {

Using DOM Parser (6) case "firstName": emp.firstName = content; break; case "lastName": emp.lastName = content; case "location": emp.location = content; } http://www3.ntu.edu.sg/home/ehchua/programming/sql/relational_database_design.html

Using DOM Parser (7) } empList.add(emp); //Printing the Employee list populated. for (Employee emp : empList) { System.out.println(emp); http://www3.ntu.edu.sg/home/ehchua/programming/sql/relational_database_design.html

Using DOM Parser (8) } class Employee{ String id; String firstName; String lastName; String location; @Override public String toString() { return firstName+" "+lastName+"("+id+")"+location; http://www3.ntu.edu.sg/home/ehchua/programming/sql/relational_database_design.html

Using DOM Parser (9) } The output for the above will be: Rakesh Mishra(111)Bangalore John Davis(112)Chennai Rajesh Sharma(113)Pune http://www3.ntu.edu.sg/home/ehchua/programming/sql/relational_database_design.html

Other main parsers SAX Parser StAX Parser You can find sample codes in reference link !!! 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/ Drafting out the sample input forms, queries and reports, often helps.