Consuming eXtensible Markup Language (XML) feeds

  • View
    212

  • Download
    0

Embed Size (px)

Transcript

  • Consuming eXtensible Markup Language (XML) feeds

  • What is XML?XMLStands for eXtensibleMarkupLanguage

    Is designed to transport and store data with focus on what data isAs opposed to HTML that was designed to display data with focus on how data looks

    Tags are not predefinedThe tags used in HTML are predefined

    HTML docs use tags defined in HTML standard

    Does not do anythingCreated to structure, store, and transport information

    is a Software and hardware-independent tool

    For carrying information

    Tove Jani Reminder Don't forget me this weekend!

  • How can XML be used?Separate data from HTMLDisplaying dynamic data in your HTML document

    Simplify data sharing/transportXML is stored in plain text format =>Software and hardware-independent data sharing

    Greatly reducing complexity of data transportBetween incompatible applications

  • XML treeXML documentsForm a tree structure Starting at root and branching to leaves

    Example XML document: Tove Jani Reminder Don't forget me this weekend!

  • Tree representation of an XML doc: Example Everyday Italian Giada De Laurentiis 2005 30.00 Harry Potter J K. Rowling 2005 29.99 Learning XML Erik T. Ray 2003 39.95

  • XML elementsAn element can containOther elements

    Text

    Attributes

    Or a mix of the above Harry Potter J K. Rowling 2005 29.99 Learning XML Erik T. Ray 2003 39.95

  • Well-formed XML docsThe syntax rulesXML docs must have a root element

    XML elements must have a closing tag

    XML tags are case sensitive

    XML elements must be properly nested

    XML attributes must be quotedThis text is bold and italicThis is incorrect Tove Jani

  • Document Object Model (DOM)DOMIs a tree structure where each node Contains one of the components of an XML structure

    The two most common nodes areElement nodes and text nodes

    Provides an API for processing XML filesInstantiate the Factory

    Create a document builder

    Get a parser and parse the file

  • DOM Nodes

    NodenodeNamenodeValueAttributesAttrName of attributeValue of attributenullCDATASection#cdata-sectionContent of the CDATA sectionnullComment#commentContent of the commentnullDocument#documentnullnullDocumentFragment#documentFragmentnullnullDocumentTypeDocument Type namenullnullElementTag namenullnullEntityEntity namenullnullEntityReferenceName of entity referencednullnullNotationNotation namenullnullProcessingInstructionTargetEntire content excluding the targetnullText#textContent of the text nodenull

  • Classes for Processing XML filesDocumentRepresents the entire XML documentProviding primary access to the documents data

    MethodsgetElementsByTagName(String tagname)Returns a NodeList of all Nodes with a given tag name

    NodeRepresents a single node in the document treegetNodeName()/getNodeValue() returnThe name/value as a string of the node depending on its typegetFirstChild()/getLastChild()/getChildNodes()

  • Classes for Processing XML files (continued)NodeListOrdered collection of nodes, whereItems accessible via an integral indexMethodsitem(int index)Returns the Node at index.

    NamedNodeMapCollection of nodes that can be accessed by name

    MethodsNode item(int index) Node getNamedItem(String name)

  • Classes for Processing XML files (contd) ElementRepresents an element in XML thatMay have attributes associated with themHas methods to retrieve attributes by name or by valueString getAttribute(String name)Retrieves an attribute name by nameAttr getAttributeNode(String name)Retrieves an attribute node by nameAttrRepresents an attribute in an Element objectString getName()String getValue()

  • JDOM

  • What is JDOM?JDOMis a third-party java libraryfor accessing, manipulating and outputting XML data

    has an easy-to-use APIHTML documentation for API can found at:http://www.jdom.org/docs/apidocs/index.html

    is lightweight and fastAs compared to the DOM parsing library offered by Java

    Can be downloaded from:http://www.jdom.org/downloads/index.html

  • JDOM: exampleimport java.io.File; import java.io.IOException;import java.util.List; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; public class ReadXMLFile { public static void main(String[] args) { SAXBuilder builder = new SAXBuilder(); File xmlFile = new File(staff.xml"); try { Document document = (Document) builder.build(xmlFile); Element rootNode = document.getRootElement(); List list = rootNode.getChildren("staff"); for (int i = 0; i < list.size(); i++) { Element node = (Element) list.get(i); System.out.println("First Name : " + node.getChildText("firstname")); System.out.println("Last Name : " + node.getChildText("lastname")); System.out.println("Nick Name : " + node.getChildText("nickname")); System.out.println("Salary : " + node.getChildText("salary")); } } catch (IOException io) { System.out.println(io.getMessage()); } catch (JDOMException jdomex) { System.out.println(jdomex.getMessage()); } } }

    *********