110 likes | 223 Views
Java API for XML Processing (JAXP). CSE 4/586: Distributed Systems Department of Computer Science and Engineering University at Buffalo, New York Jia Zhao ( jiazhao@buffalo.edu ). XML Basics.
E N D
Java API for XML Processing (JAXP) CSE 4/586: Distributed Systems Department of Computer Science and Engineering University at Buffalo, New York Jia Zhao (jiazhao@buffalo.edu)
XML Basics • eXtensible Markup Language (XML) –Its primary purpose is to facilitate the sharing of structured data across different information systems, particularly via the Internet. • XML is a generic framework for storing any amount of text or any data whose structure can be represented as a tree: • Root element:the document has exactly one root element • Elements: The text enclosed by the root tags may contain an arbitrary number of XML elements • E.g. <nameattribute="value">content</name>
XML Basics • XML requires that elements be properly nested • A comment is enclosed between "<!--" and "-->". • XML reserves certain characters. To express these characters in your document, use the following: & & ampersand < < less than > > greater than ' ‘ apostrophe " “ quotation mark
Schema Definition • The schema of XML files is usually defined using Languages such as: • Document Type Definition (.dtd): The oldest schema format for XML • W3C XML Schema Language (.xsd): the successor of DTDs .
JAXP • Allows to parse your data as a stream of events(SAX) or to build an object representation(DOM) of it. • Simple API for XML (SAX) : event-driven serial-access mechanism that does element-by-element processing. The application registers to receive events as entities are encountered within the source document • Document Object Model (DOM) : interface-oriented mechanism that allows for navigation of the entire document as if it were a tree of "Node" objects. The entire document is read into memory as a tree structure for random access by the calling application
JAXP- StAX • Streaming API for XML (StAX) :a median between SAX and DOM for Java. The application moves the cursor forward - 'pulling' the information from the parser as it needs • The XMLStreamReader interface in the StAX cursor API lets you read XML streams or documents in a forward direction only, one item in the infoset at a time.
JAXP- StAX • The following methods are available for pulling data from the stream or skipping unwanted events: • Get the value of an attribute • Read XML content • Determine whether an element has content or is empty • Get indexed access to a collection of attributes • Get indexed access to a collection of namespaces • Get the name of the current event (if applicable) • Get the content of the current event (if applicable)
Weather.com XML feed & WeatherParser • The StAX tutorial which is part of the J2EE tutorialhttp://java.sun.com/javaee/5/docs/tutorial/doc/bnbem.html#bnbewhttp://www.ibm.com/developerworks/xml/library/x-tipstx/ • The weather.com SDK http://download.weather.com/web/xml/sdk.zip • The weather.com DTD http://www.weather.com/documentation/xml/weather.dtd
Weather.com XML feed & WeatherParser • Sample XML Feed • http://xoap.weather.com/weather/local/14214?cc=&prod=xoap&par=1012383874&key=49ead562833bfce4 • Register with weather.com at http://www.weather.com/services/xmloap.html and get your own partner ID and license key.