Generating XML. Crash course on generating XML

Similar documents
Choosing output format. Dynamically choosing format

Front-end / Back-end. How does your application communicate with services?

Apache Xerces is a Java-based processor that provides standard interfaces and implementations for DOM, SAX and StAX XML parsing API standards.

Java and XML. XML documents consist of Elements. Each element will contains other elements and will have Attributes. For example:

Chapter 4. Result and Discussion

The concept of DTD. DTD(Document Type Definition) Why we need DTD

CSC System Development with Java Working with XML

Copyright 2005, by Object Computing, Inc. (OCI). All rights reserved. Database to Web

Understanding DOM. Presented by developerworks, your source for great tutorials ibm.com/developerworks

JAXP: Beyond XML Processing


Java Programming with XSLT

CS193j, Stanford Handout #29 XML. Suppose you have a bunch of dots (x,y pairs) you need to represent in a program for processing.

Introduction to Database Systems. Fundamental Concepts

XML. Technical Talk. by Svetlana Slavova. CMPT 842, Feb

XML. XML Syntax. An example of XML:

XML An API Persepctive. Context. Overview

extensible Markup Language (XML) Announcements Sara Sprenkle August 1, 2006 August 1, 2006 Assignment 6 due Thursday Project 2 due next Wednesday

M359 Block5 - Lecture12 Eng/ Waleed Omar

Chapter 13 XML: Extensible Markup Language

Introduction to XML. An Example XML Document. The following is a very simple XML document.

.. Cal Poly CPE/CSC 366: Database Modeling, Design and Implementation Alexander Dekhtyar..

XML Extensible Markup Language

markup language carry data define your own tags self-descriptive W3C Recommendation

Copyright 2007 Ramez Elmasri and Shamkant B. Navathe. Slide 27-1

9.3.5 Accessing the Vote Service

Introduction Syntax and Usage XML Databases Java Tutorial XML. November 5, 2008 XML

CSC Web Technologies, Spring Web Data Exchange Formats

Document Type Definitions

Web architectures Laurea Specialistica in Informatica Università di Trento. DOM architecture

2009 Martin v. Löwis. Data-centric XML. XML Syntax

An Introduction to XML

JDeveloper. Read Lotus Notes Data via URL Part 2

XML: and related technologies

Technical University of Braunschweig. Institute of Operating Systems and Networks

A POLICY-BASED DIALOGUE SYSTEM FOR PHYSICAL ACCESS CONTROL

SDMX self-learning package No. 6 Student book. XML Based Technologies Used in SDMX

XML for Java Developers G Session 3 - Main Theme XML Information Modeling (Part I) Dr. Jean-Claude Franchitti

Extensible Markup Language (XML) Hamid Zarrabi-Zadeh Web Programming Fall 2013

Marco Ronchetti - Java XML parsing J0 1

XML Tutorial. NOTE: This course is for basic concepts of XML in line with our existing Android Studio project.

Part II: Semistructured Data

XML: A Language for Metadata Tags. The Database's Advantage. Differences Between Tables and Databases

HUB Web Service API. IPPC ephyto HUB v1.5. Public - FAO/IPPC

XPath Basics. Mikael Fernandus Simalango

Introduction to XML. SSE 3200 Web-based Services. Michigan Technological University Nilufer Onder

5/17/2009. The Database's Advantage. XML: A Language for Metadata Tags. An Example from Tahiti. An Example from Tahiti (cont'd)

HUB Web Service API. IPPC ephyto HUB v1.7. Public - FAO/IPPC

THE ASIAN SCHOOL. Class-10. ch9. answer scheme. A. Check the Right Answer (1*2) 1. What is the correct way of describing XML data? a.

XML Overview, part 1

XML 2 APPLICATION. Chapter SYS-ED/ COMPUTER EDUCATION TECHNIQUES, INC.

COMP9321 Web Application Engineering. Extensible Markup Language (XML)

XML Structures. Web Programming. Uta Priss ZELL, Ostfalia University. XML Introduction Syntax: well-formed Semantics: validity Issues

Introduction to XML. Chapter 133

COMP9321 Web Application Engineering

XML for Java Developers G Session 3 - Main Theme XML Information Modeling (Part I) Dr. Jean-Claude Franchitti

Oracle Fusion Middleware

Grid Computing. What is XML. Tags, elements, and attributes. Valid and well formed XML. Grid Computing Fall 2006 Paul A.

Solutions. a. Yes b. No c. Cannot be determined without the DTD. d. Schema. 9. Explain the term extensible. 10. What is an attribute?

XML. Jonathan Geisler. April 18, 2008

References between Mapping Programs in SAP-XI/PI as of Release 7.0 and 7.1

CLASS DISCUSSION AND NOTES

Outline. XML vs. HTML and Well Formed vs. Valid. XML Overview. CSC309 Tutorial --XML 4. Edward Xia

Information Systems. XML Essentials. Nikolaj Popov

XML 2 APPLICATION. Chapter SYS-ED/ COMPUTER EDUCATION TECHNIQUES, INC.

XML in the Development of Component Systems. The Document Object Model

The Xlint Project * 1 Motivation. 2 XML Parsing Techniques

XML. Objectives. Duration. Audience. Pre-Requisites

Introduction to XML Zdeněk Žabokrtský, Rudolf Rosa

What is XML? XML is designed to transport and store data.

Introduction to XML. XML: basic elements

XML: Extensible Markup Language

The main Topics in this lecture are:

Structured documents

Chapter 2 XML, XML Schema, XSLT, and XPath

Intro to XML. Borrowed, with author s permission, from:

INTERNET PROGRAMMING XML

W3C XML XML Overview

Döcu Content IBM Notes Domino, DB2 Oracle JDeveloper, WebLogic

Document Object Model (DOM) Java API for XML Parsing (JAXP) DOM Advantages & Disadvantage &6&7XWRULDO (GZDUG;LD

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI

languages for describing grammar and vocabularies of other languages element: data surrounded by markup that describes it

Semistructured data, XML, DTDs

Time: 3 hours. Full Marks: 70. The figures in the margin indicate full marks. Answer from all the Groups as directed. Group A.

Java EE 7: Back-end Server Application Development 4-2

6.1 Understand Relational Database Management Systems

The Semi-Structured Data Model. csc343, Introduction to Databases Diane Horton originally based on slides by Jeff Ullman Fall 2017

All About <xml> CS193D, 2/22/06

SDN Community Contribution

User Interaction: XML and JSON

XML: Managing with the Java Platform

Overview. Introduction. Introduction XML XML. Lecture 16 Introduction to XML. Boriana Koleva Room: C54

extensible Markup Language (XML) Basic Concepts

XML. Marie Dubremetz Uppsala, April 2014

Outline. XML DOCTYPE External - SYSTEM. XML DOCTYPE Internal DTD &6&7XWRULDO ;0/ (GZDUG;LD

XML APIs. Web Data Management and Distribution. Serge Abiteboul Philippe Rigaux Marie-Christine Rousset Pierre Senellart

XML for Android Developers. partially adapted from XML Tutorial by W3Schools

Part 2: XML and Data Management Chapter 6: Overview of XML

AIM. 10 September

Copyright 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Chapter 7 XML

Transcription:

Generating XML Crash course on generating XML

What is XML? XML is a markup language using tags (entities surrounded in < and > ). XML stands for extensible Markup Language. Goals: simplicity, generality and usability Pros: Machine and human readable, easy for machines to validate and check Read: http://www.w3schools.com/xml/ https://en.wikipedia.org/wiki/xml

XML data integrity XML can be checked for validity and wellformedness Wellformed: Adheres to the XML syntax Validity: Adheres to some schema (DTD)

Syntax (overview) Declaration: <?xml version="1.0" encoding="utf-8"?> Followed by elements: <STUDENT> start tag <NAME>Donald Duck</NAME> child element end tag Donald Duck is content, while the tags are markup!

Tags may have attributes <STUDENT studentid="2"> <NAME>Beata Bengtsson</NAME> studentid is an attribute whose value is "2"

The top-level tag is sometimes called root element <?xml version="1.0" encoding="utf-8" standalone="no"?> <STUDENTS> root element! <STUDENT id="1"> <NAME>Anders Andersson</NAME> <STUDENT id="2"> <NAME>Beata Bengtsson</NAME> <STUDENT id="3"> <NAME>Charlie Christensen</NAME> <STUDENT id="4"> <NAME>Dick Dale</NAME> <STUDENT id="5"> <NAME>Edward Eriksson</NAME> </STUDENTS>

Creating an XML document from Java (finally!) Sh*tload of imports import javax.xml.parsers.documentbuilder; import javax.xml.parsers.parserconfigurationexception; import javax.xml.parsers.documentbuilderfactory; import javax.xml.transform.transformer; import javax.xml.transform.transformerexception; import javax.xml.transform.transformerfactory; import javax.xml.transform.outputkeys; import javax.xml.transform.dom.domsource; import javax.xml.transform.stream.streamresult; import org.w3c.dom.document; import org.w3c.dom.element; import org.w3c.dom.domexception;

Creating the Document DocumentBuilderFactory docfactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docbuilder = docfactory.newdocumentbuilder(); Document doc = docbuilder.newdocument();

Creating the root element Element rootelement = doc.createelement("students"); doc.appendchild(rootelement); Will generate (when we create it): <?xml version="1.0" encoding="utf-8"?> <STUDENTS> </STUDENTS>

Adding child elements // A treemap with <ID,NAME> for(string id : map.keyset()){ Element student = doc.createelement("student"); student.setattribute("id", id); Element name = doc.createelement("name"); name.appendchild(doc.createtextnode(map.get(id))); student.appendchild(name); rootelement.appendchild(student); }

Transformers - robots in disguise TransformerFactory transformerfactory = TransformerFactory. newinstance(); Transformer transformer = transformerfactory.newtransformer(); transformer.setoutputproperty(outputkeys.indent, "yes"); Transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File("Student.xml")); transformer.transform(source, result);

The resulting file <?xml version="1.0" encoding="utf-8" standalone="no"?> <STUDENTS> <STUDENT id="1"> <NAME>Anders Andersson</NAME> <STUDENT id="2"> <NAME>Beata Bengtsson</NAME> <STUDENT id="3"> <NAME>Charlie Christensen</NAME> <STUDENT id="4"> <NAME>Dick Dale</NAME> <STUDENT id="5"> <NAME>Edward Eriksson</NAME> </STUDENTS>

For reference - The TreeMap with the students Map<String,String> students = new TreeMap<String,String>(); students.put("1", "Anders Andersson"); students.put("2","beata Bengtsson"); students.put("3","charlie Christensen"); students.put("4","dick Dale"); students.put("5","edward Eriksson");