Java API for XML Processing (JAXP) CSE 4/586: Distributed Systems Department of Computer Science and Engineering University at Buffalo, New York Jia Zhao
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. content XML Basics
XML requires that elements be properly nested A comment is enclosed between " ". 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
SAX Parsing
DOM Parsing
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 tutorial em.html#bnbew x-tipstx/ em.html#bnbew x-tipstx/ The weather.com SDK The weather.com DTD
Weather.com XML feed & WeatherParser Sample XML Feed c=&prod=xoap&par= &key=49ead bfce4 c=&prod=xoap&par= &key=49ead bfce4 Register with weather.com at and get your own partner ID and license key.