Web Services Basic Training - PowerPoint PPT Presentation

tyson
web services basic training n.
Skip this Video
Loading SlideShow in 5 Seconds..
Web Services Basic Training PowerPoint Presentation
Download Presentation
Web Services Basic Training

play fullscreen
1 / 18
Download Presentation
Web Services Basic Training
98 Views
Download Presentation

Web Services Basic Training

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Web Services Basic Training Srinivas Kandula Vijayan Srinivasan

  2. Agenda • Environment Setup • XML • Namespaces • DTD / Schema • JAXP • DOM • SAX • JAXB • Marshaling • Un-Marshaling • XSD -> Java Utilities • Web Services • WSDL • SOAP • REST

  3. Environment Setup • Compiler • JDK 1.6 • IDE • Eclipse Java EE – Helios • Additional Plugins • SVN Plugin • Subclipse • Maven Plugin • M2Eclipse Core • M2Eclipse Extras • Example Source Code • Available at Google Code

  4. XML What is XML? • XML stands for EXtensible Markup Language • XML is a markup language much like HTML • XML was designed to carry data, not to display data • XML is a W3C Recommendation Pros • Platform and system independent • XML tags are not predefined. You must define your own tags • XML is designed to be self-descriptive • Validations are possible DTD and XML Schema • Many Built-In Parsers are available in almost all popular languages Cons • Self-descriptive nature results a bigger payload compare to its competitive formats • JSON • YAML

  5. Example XML <bookstore>   <book category="COOKING">    <title lang="en">Everyday Italian</title>    <author>Giada De Laurentiis</author>    <year>2005</year>    <price>30.00</price>  </book> <book category="WEB">    <title lang="en">Learning XML</title>    <author>Erik T. Ray</author>    <year>2003</year>    <price>39.95</price>  </book> </bookstore>

  6. DTD • DTD • The purpose of a DTD (Document Type Definition) is to define the legal building blocks of an XML document. • A DTD defines the document structure with a list of legal elements and attributes. <!DOCTYPE bookstore[ <!ELEMENT bookstore(book*)> <!ELEMENT book(title, author, year, price)><!ELEMENT title(#PCDATA)><!ELEMENT author(#PCDATA)><!ELEMENT year(#PCDATA)><!ELEMENT price(#PCDATA)> <!ATTLIST book category CDATA #REQUIRED><!ATTLIST title lang CDATA #IMPLIED> ]>

  7. JAXP • Java API for XML Processing • JAXP enables applications to parse, transform, validate and query XML documents using an API • JAXP provides API which is independent of a particular XML processor implementation • Parser can be changed without recompiling the code using set of system properties • SAX • DOM • XSD • XSL • XPATH

  8. SAX • SAX stand for Simple API for XML • SAX is an event-driven, serial-access mechanism for accessing/processing XML documents • Parser triggers the Callback’s: startElement(), characters(), endElement() etc., while parsing the xml document. • Steps involved in Parsing XML file using SAX • Create a SAX Parser instance • Register callback implementation and implement the callbacks • Fastest and least memory-intensive • This is used only if we want to read data and have the application act on it

  9. Sample SAX import java.io.File; import javax.xml.parsers.FactoryConfigurationError; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; publicclass TestSAXParsing { publicstaticvoid main(String[] args) { try { // Get SAX Parser Factory SAXParserFactory factory = SAXParserFactory.newInstance(); // Turn on validation, and turn off namespaces factory.setValidating(true); factory.setNamespaceAware(false); SAXParser parser = factory.newSAXParser(); parser.parse(new File("books.xml"), new MyHandler()); } catch (ParserConfigurationException e) { System.out.println("The underlying parser does not support " + " the requested features."); } catch (FactoryConfigurationError e) { System.out.println("Error occurred obtaining SAX Parser Factory."); } catch (Exception e) { e.printStackTrace(); } } }

  10. MyHandler import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; publicclass MyHandler extends DefaultHandler { publicvoid startDocument() throws SAXException { } publicvoid endDocument() throws SAXException { } publicvoid startElement(String namespaceURI, String localName, String qName, Attributes attributes) { System.out.print(qName + ":"); } publicvoid endElement(String name) throws SAXException { System.out.println(); } publicvoid characters(char buf[], int offset, int len) throws SAXException { String s = new String(buf, offset, len); System.out.print(s); } }

  11. DOM • DOM stands for Document Object Model • A DOM for XML is an object model that exposes the contents of an XML document • Behind the scene it uses SAX Parser to construct the DOM Object • Mainly used when you want to manipulate the XML content during runtime • DOM can be memory intensive when it comes to very large XML document

  12. Sample DOM publicclassDOMParser { publicstaticvoid main(String[] args) { Document document=parse("books.xml"); Node node=document.getDocumentElement(); print(node,0); } privatestaticvoid print(Node node, int level) { if(node.getNodeType()==3){ return; } for(inti=0;i<level;i++){ System.out.print("\t"); } System.out.println(node.getNodeName()); if(node.hasChildNodes()){ NodeListnodeList=node.getChildNodes(); for(inti=0;i<nodeList.getLength();i++){ print(nodeList.item(i),level+1); } } } }

  13. Sample DOM publicstatic Document parse(String fileName) { Document document = null; // Initiate DocumentBuilderFactory DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); // To get a validating parser factory.setValidating(false); // To get one that understands namespaces factory.setNamespaceAware(true); try { // Get DocumentBuilder DocumentBuilder builder = factory.newDocumentBuilder(); // Parse and load into memory the Document document = builder.parse( new File(fileName)); return document; } catch (SAXParseException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } returnnull; }

  14. JAXB • Framework for processing XML documents • Mandates Schema for XML document • Convert Java objects to XML and Vice versa. • Generate the Java type information (Classes) using Binding compiler (xjc) for accessing elements, attributes and other content in a typesafe way. • JAXWS uses JAXB internally for message parsing

  15. JAXB Binding Process Steps 1 2 4 3

  16. Advantages of JAXB over JAXP • JAXB allows Java developers to access and process XML data without having to know XML or XML processing. • For example, there's no need to create or use a SAX parser or write callback methods. • JAXB presents the XML document to the program in a Java format.

  17. Examples • JAXB2 • Create Java Classes from XSD • Unmarshal • Marshal • Castor • Create Java Classes from XSD • Unmarshal • Marshal

  18. Comparisons • JAXB • Standard from Sun • No Reflection • Better performance • Castor • Nonstandard • Uses reflection • Can be used for any POJO • Better Memory Management