Data formats

50
Data formats Web Technology - 2ID60 28 November 2013 Katrien Verbert Natasha Stash George Fletcher

description

 

Transcript of Data formats

Page 1: Data formats

Data formats Web Technology - 2ID60 28 November 2013

Katrien Verbert Natasha Stash George Fletcher

Page 2: Data formats

Plan for today

•  Data formats: •  JSON •  XML •  Parsing XML

•  Example RESTful service that exchanges XML data •  Mini-project

PAGE 4 28/11/13

Page 3: Data formats

Data formats

•  JSON •  XML •  Parsing XML

PAGE 5 28/11/13

Page 4: Data formats

JSON

•  JSON: JavaScript Object Notation. •  JSON is syntax for storing and exchanging text

information. •  JSON is smaller than XML, and faster and easier to

parse.

PAGE 6 27/11/13

Page 5: Data formats

JSON

Maps to two universal structures

1.  An unordered collection of name value pairs: {"firstName”:"Nicole”,"lastName": "Kidman”}

2.  An ordered list of values ["Monday”, "Tuesday”, "Wednesday”]

PAGE 7 27/11/13 Source: Theresa Velden

Page 6: Data formats

JSON Syntax

PAGE 8 27/11/13 http://www.json.org/

Page 7: Data formats

JSON syntax

•  An object is an unordered set of name/value pairs •  The pairs are enclosed within braces, { } •  There is a colon between the name and the value •  Pairs are separated by commas •  Example: { "firstName":"John" , "lastName":"Doe" }

•  An array is an ordered collection of values •  The values are enclosed within brackets, [ ] •  Values are separated by commas •  Example: [ "html", �xml", "css" ]

Page 8: Data formats

JSON example: de-constructed

PAGE 10

27/11/13

Source: Theresa Velden

Page 9: Data formats

JSON example: de-constructed

PAGE 11

27/11/13

Source: Theresa Velden

Page 10: Data formats

JSON example: de-constructed

PAGE 12

27/11/13

Source: Theresa Velden

Page 11: Data formats

JSON example: de-constructed

Source: Theresa Velden PAGE 13

27/11/13

Page 12: Data formats

More information about JSON

•  http://www.w3schools.com/json/default.asp

PAGE 14 27/11/13

Page 13: Data formats

Overview

•  JSON •  XML •  parsing XML

PAGE 15 27/11/13

Page 14: Data formats

Introduction to XML

•  Why is XML important? •  simple open non-proprietary widely accepted data

exchange format •  XML is like HTML but

•  no fixed set of tags −  X = “extensible”

•  no fixed semantics (c.q. representation) of tags −  representation determined by separate ‘style sheet’ −  semantics determined by application

•  no fixed structure −  user-defined schemas

Page 15: Data formats

XML: running example

<?xml version="1.0"?> <Order>

<Date>2003/07/04</Date> <CustomerId>123</CustomerId> <CustomerName>Acme Alpha</CustomerName> <Item>

<ItemId> 987</ItemId> <ItemName>Coupler</ItemName> <Quantity>5</Quantity>

</Item> <Item>

<ItemId>654</ItemId> <ItemName>Connector</ItemName> <Quantity>3</Quantity>

</Item> </Order>

Page 16: Data formats

Elements of an XML Document

•  Global structure •  Mandatory first line <?xml version ="1.0"?>

•  A single root element <order> . . . </order>

•  Elements have a recursive structure •  Tags are chosen by author;

<item>, <itemId>, <itemName> •  Opening tag must have a matching closing tag <item></item>, <a><b></b></a>

Page 17: Data formats

Elements of an XML Document

•  The content of an element is a sequence of: −  Elements <item> … </item> −  Text Jan Vijs −  Processing Instructions <! . . . !> −  Comments <!– This is a comment --!>

•  Empty elements can be abbreviated: <item/> is shorthand for <item></item>

Page 18: Data formats

Elements of an XML Document

•  Elements can have attributes <Title Value="Student List"/> <PersonList Type="Student" Date="2004-12-12">

. . . </Personlist>

Attribute_name = “Value” Attribute name can only occur once Value is always quoted text (even numbers)

Page 19: Data formats

Elements of an XML Document

•  Text and elements can be freely mixed <Course ID=“2ID45”> The course <fullname>Database

Technology</fullname> is lectured by <title>dr.</title>

<fname>George</fname> <sname>Fletcher</sname>

</Course> •  The order between elements is considered important •  Order between attributes is not

Page 20: Data formats

Well-formedness

•  We call an XML-document well-formed iff •  it has one root element; •  elements are properly nested; •  any attribute can only occur once in a given opening

tag and its value must be quoted.

•  Check for instance at: http://www.w3schools.com/xml/xml_validator.asp

Page 21: Data formats

Overview

•  JSON •  XML •  parsing XML

PAGE 23 27/11/13

Page 22: Data formats

Parsing XML

•  Goal •  Read XML files into data structures in programming

languages

•  Possible strategies •  Parse into generic tree structure (DOM) •  Parse as sequence of events (SAX) •  Automatically parse to language-specific objects (JAXB)

Page 23: Data formats

DOM

•  A DOM document is an object containing all the information of an XML document

•  It is composed of a tree (DOM tree) of nodes

PAGE 25 27/11/13

Page 24: Data formats

DOM parsers

Different types of nodes Document node

Element node Text node Attribute node Processing instruction node …….

Page 25: Data formats

Example

PAGE 27 27/11/13 Source: Theresa Velden

Page 26: Data formats

Example DOM tree

PAGE 28 27/11/13 Source: Theresa Velden

Page 27: Data formats

Example DOM tree

PAGE 29 27/11/13 Source: Theresa Velden

Page 28: Data formats

DOM parsers

Page 29: Data formats

XML example

<?xml version="1.0"?> <Order>

<Date>2003/07/04</Date> <CustomerId>123</CustomerId> <CustomerName>Acme Alpha</CustomerName> <Item>

<ItemId> 987</ItemId> <ItemName>Coupler</ItemName> <Quantity>5</Quantity>

</Item> <Item>

<ItemId>654</ItemId> <ItemName>Connector</ItemName> <Quantity>3</Quantity>

</Item> </Order>

Page 30: Data formats

DOM tree example

order

item

_333445555

item

CustomerId

_123456789

item

_999887777

CustomerId

CustomerId

Example code fragment: Node order= doc.getFirstChild();

Page 31: Data formats

DOM tree example

order

item

_333445555

item

CustomerId

_123456789

item

_999887777

CustomerId

CustomerId

Example code fragment: Node order= doc.getFirstChild(); NodeList items= order.getChildNodes();

Page 32: Data formats

DOM tree example

order

item

_333445555

item

CustomerId

_123456789

item

_999887777

CustomerId

CustomerId

Example code fragment: NodeList items=

doc.getElementsByTagName("item")

Page 33: Data formats

Main features of DOM parsers

•  A DOM parser creates an internal structure in memory which is a DOM document object

•  Client applications get the information of the original XML document by invoking methods on this Document object or on other objects it contains

•  DOM parser is tree-based

Page 34: Data formats

Parsing XML in Java

•  Package javax.xml.parsers

•  Provides classes allowing the processing of XML documents.

•  SAX (Simple API for XML) •  DOM (Document Object Model)

Page 35: Data formats

Example: order list

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <order> <customerId>123</customerId> <customerName>Katrien Verbert</customerName> <date>12 February 2013</date> <item> <itemId>id1</itemId> <itemName>Iphone 5</itemName> <quantity>2</quantity> </item> <item> <itemId>id2</itemId> <itemName>Nokia Lumia 800</itemName> <quantity>1</quantity> </item> </order>

PAGE 37 27/11/13

Page 36: Data formats

Representing objects in Java

public class Item { String id; String name; String quantity; public Item(String id, String name, String quantity) { this.id = id; this.name = name; this.quantity = quantity; } public Item(){} + getters and setters

PAGE 38 27/11/13

Page 37: Data formats

Representing objects in Java

public class Order { private String date; private String customerId; private String customerName; private ArrayList<Item> items; public Order(String date, String customerId, String customerName) { this.date = date; this.customerId = customerId; this.customerName = customerName; items=new ArrayList<Item>(); } … }

PAGE 39 27/11/13

Page 38: Data formats

DOM parser example

public void parse(String fileName) throws Exception{ DocumentBuilderFactory factory =

DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(fileName); NodeList itemList = doc.getElementsByTagName("item"); for (int i = 0; i < itemList.getLength(); i++) { Node itemNode = itemList.item(i); Item item=getItem(itemNode); } }

PAGE 40 27/11/13

Page 39: Data formats

DOM tree example

order

item

_333445555

item

CustomerId

_123456789

item

_999887777

CustomerId

CustomerId

Example code fragment: NodeList items=

doc.getElementsByTagName("item")

Page 40: Data formats

DOM parser example

public Item getItem (Node n){ NodeList itemElements = n.getChildNodes(); Item item = new Item(); for (int j = 0; j < itemElements.getLength(); j++) { Node node=itemElements.item(j); if (node.getNodeName().equalsIgnoreCase("itemId")) item.setId(node.getTextContent()); else if (node.getNodeName().equalsIgnoreCase("itemName")) item.setName(node.getTextContent()); else if (node.getNodeName().equalsIgnoreCase("quantity")) item.setQuantity(node.getTextContent()); } return item; }

PAGE 42 27/11/13

Page 41: Data formats

JAXB

•  JAXB: Java API for XML Bindings

•  Defines an API for automatically representing XML schema as collections of Java classes.

Page 42: Data formats

Annotations markup

•  @XmlAttribute to designate a field as an attribute •  @XmlRootElement to designate the document root element. •  @XmlElement to designate a field as a node element •  @XmlElementWrapper to specify the element that encloses a

repeating series of elements

•  Note that you should specify only the getter method as @XmlAttribute or @XmlElement.

•  Jaxb oddly treats both the field and the getter method as independent entities

Page 43: Data formats

Order example

import javax.xml.bind.annotation.*; @XmlRootElement public class Item { @XmlElement private String itemId; @XmlElement private String ItemName; @XmlElement private int quantity; public Item() { } } }

Page 44: Data formats

Order example import javax.xml.bind.annotation.*; import java.util.*; @XmlRootElement public class Order { @XmlElement private String date; @XmlElement private String customerId; @XmlElement private String customerName; @XmlElement private List<Item> items; public Order() { this.items=new ArrayList<Item>(); } •  }

Page 45: Data formats

Marshalling

•  marshalling •  the process of producing an XML document from Java objects

•  unmarshalling •  the process of producing a content tree from an XML document

•  JAXB only allows you to unmarshal valid XML documents •  JAXB only allows you to marshal valid content trees into XML

Page 46: Data formats

Marshalling example

public String toXmlString(){ try{ JAXBContext context=JAXBContext.newInstance(Order.class); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); ByteArrayOutputStream b=new ByteArrayOutputStream(); m.marshal(this,b); return b.toString(); }catch (Exception e){ e.printStackTrace(); return null; } }

Page 47: Data formats

Unmarshalling example

public Order fromXmlString(String s){ try{ JAXBContext jaxbContext = JAXBContext.newInstance(Order.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

Order order = (Order) jaxbUnmarshaller.unmarshal(new StreamSource( new StringReader(s)));

return order; }catch (Exception e){ e.printStackTrace(); return null; } }

Page 48: Data formats

Test transformation

public static void main(String args[]){ Order o=new Order("1 March 2013", "123", "Katrien"); o.getItems().add(new Item("1", "iPhone 5", 2)); o.getItems().add(new Item("2", "Nokia Lumia 800", 2)); System.out.println(o.toXmlString()); }

Page 49: Data formats

Output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <order> <customerId>123</customerId> <customerName>Katrien Verbert</customerName> <date>12 February 2013</date> <items> <itemId>id1</itemId> <ItemName>Iphone 5</ItemName> <quantity>2</quantity> </items> <items> <itemId>id2</itemId> <ItemName>Nokia Lumia 800</ItemName> <quantity>1</quantity> </items> </order>