Java and XML - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Java and XML

Description:

contactlist owner='Yvan' contact name firstname Yvan ... now execute the XPath select statement to get the contact that matches the email address given ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 28
Provided by: jonathan76
Category:
Tags: xml | address | java | owner | search

less

Transcript and Presenter's Notes

Title: Java and XML


1
Java and XML
  • Format of lecture
  • What is XML?
  • A sample XML file
  • How to use XML with JSP.
  • A demonstration of parsing an XML file in JSP.

2
What is XML?
  • XML is principally concerned with the description
    and structure of data (content presentation are
    separate).
  • Traditional methods of data storage and exchange
    employ a variety of schemes.
  • Electronically, these usually are of the form of
    simple text files (Comma Separated Values etc.)
    or binary files.
  • Both methods have their own advantages and
    disadvantages.
  • CSV files contain data that can be easily read
    but lack a description of their own format.
  • Binary files contain data and a description of
    its data format (as a Word document does) but are
    proprietary schemes that require specific
    applications to read them.

3
What is XML?
  • XML stands for eXtensible Markup Language.
  • Developed by World Wide Web Consortium (W3C).
  • The aim of XML is to provide the best of both
    worlds a means to store data in an easy to read
    text format that also contains information that
    describes the format of the data itself.
  • Open standard - looks similar to HTML (large user
    base) except the extensible nature of XML allows
    for the creation of user-defined tags.
  • Is not to be thought of as purely a replacement
    for HTML (although it will eventually supplant
    it).

4
What is XML?
  • XML offers a standard method for storing
    structured data.
  • Language independence (English, Chinese etc.).
  • Hierarchical structure allows for simple and
    efficient querying/parsing of the document.
  • Simple data interchange between applications
    and/or distributed objects.
  • Can improve upon and replace existing EDI
    (Electronic Document Interchange) solutions.
  • Websites gain by having content and presentation
    separate.
  • Cost benefits
  • No need for private networks Internet used as
    exchange medium.
  • Reduced time to implement/reduced maintenance.

5
Illustrative Example
  • Paper-based or Simple Text File
  • Yvan Cartwright
  • Staffordshire University
  • y.j.f.cartwright_at_staffs.ac.uk
  • Comma Separated Values
  • Yvan,Cartwright,Staffordshire University,y.j.f.ca
    rtwright_at_staffs.ac.uk
  • Binary a string of 0s and 1s
  • 0101 1001 0111 0110 0110 0001 0110 1110 0010
    0000 etc.

6
Simple Illustrative Example
  • lt?xml version"1.0" encoding"UTF-8" ?gt
  • ltcontactlist owner"Yvan"gt
  • ltcontactgt
  • ltnamegt
  • ltfirstnamegtYvanlt/firstnamegt
  • ltlastnamegtCartwrightlt/lastnamegt
  • lt/namegt
  • ltworkplacegtStaffordshire Universitylt/workplacegt
  • ltemailgty.j.f.cartwright_at_staffs.ac.uklt/emailgt
  • lt/contactgt
  • lt/contactlistgt

7
How to use XML with JSP
  • XML is a big topic!
  • We are just going to look at one of the more
    fundamental aspects of XML.
  • The XML Parser.
  • An XML parser simply checks that your XML
    document is syntactically correct (well-formed)
    and contains correctly formatted data (valid).
  • Once an XML document is parsed, the information
    it contains is accessible inside our web
    application.
  • There are two ways of parsing an XML document
    SAX and DOM.

8
SAX Parser
  • Event Driven An event is triggered each time
    the parser encounters a beginning, or ending tag.

9
DOM Parser
  • A tree representing the document is built in
    memory.

10
Tree Structure
  • contacts---contact
  • ---name
  • ---firstname
  • ---Text
  • ---lastname
  • ---Text
  • ---workplace
  • ---Text
  • ---email
  • ---Text

11
Standard DOM Parsing
  • Searching for a contact based on their email
    address
  • Create a Document object.
  • Load the XML file into the Document.
  • Using iterations over the Document nodes, you can
    access any of the values or attributes that are
    stored in the XML file.
  • When you find the record that contains the email
    address that you are looking for, do something
    with the information.

12
Standard DOM Parsing
  • DocumentBuilderFactory docBuilderFactory
    DocumentBuilderFactory.newInstance()
  • DocumentBuilder docBuilder docBuilderFactory.new
    DocumentBuilder()
  • // load the XML file into a Document object
  • Document doc docBuilder.parse(new
    File(myfile.xml))
  • // what does this do?
  • doc.normalize()
  • // get the whole node structure
  • NodeList nodelist getNodeList("/contacts",
    doc)

13
Standard DOM Parsing
  • for(int index 0 index lt nodelist.getLength()
    index)
  • Node node nodelist.item(index)
  • if(node.getNodeType(TEXT_NODE))
  • if(node.getNodeName().equals(firstname))
    firstname node.getNodeValue()
  • if(node.getNodeName().equals(lastname))
    lastname node.getNodeValue()
  • if(node.getNodeName().equals(workplace))
    workplace node.getNodeValue()
  • if(node.getNodeName().equals(email)) email
    node.getNodeValue()
  • if(email.equals(y.j.f.cartwright_at_staffs.ac.uk)
    )
  • displayRecord(firstname, lastname,
    workplace, email)
  • break

14
Standard DOM Parsing Problems
  • The previous procedure is ok if you have a fairly
    flat XML structure that does not contain many
    different node types.
  • If you have many contacts stored then it may be
    very slow to iterate through the nodes until you
    find the contact you are looking for.
  • For more complex XML documents, you cant use a
    simple FOR loop to iterate.
  • You end up with code that looks more like

15
Not Nice!
  • Element root doc.getDocumentElement()
  • Node configNode root.getFirstChild()
  • NodeList childNodes configNode.getChildNodes()
  • for (int childNum 0 childNum lt
    childNodes.getLength() childNum)
  • if ( childNodes.item(childNum).getNodeType()
    Node.ELEMENT_NODE )
  • Element child (Element) childNodes.item(
    childNum )
  • if ( child.getTagName().equals( "header" ) )
  • // Do something with the header
    System.out.print("Got a header!\n")

16
XPath
  • XPath (XML Path Language) is a terse (non-XML)
    syntax for addressing portions of an XML
    document.
  • A typical XPath expression is a Location Path
    consisting of a string of element or attribute
    qualifiers separated by forward slashes ("/"),
    similar in appearance to a file system path.
  • E.g. this gets the firstname of the first contact
  • //contact1/name/firstname

17
Using XPath
  • Using the same code as per slide 12, we can then
    use the XPath syntax to retrieve any information
    we like from the Document.
  • XPath is a new(ish) specification and as such it
    was only available via Java extensions.
  • As of version 1.5 of the JDK, Java now natively
    supports XPath.

18
Search Using XPath
  • // create the XPath and initialize it
  • XPath xPath XPathFactory.newInstance().newXPath(
    )
  • // now execute the XPath select statement to get
    the contact that matches the email address given
  • NodeList nodes (NodeList) xPath.evaluate(//conta
    ctemaily.j.f.cartwright_at_staffs.ac.uk,
    nodelist, XPathConstants.NODESET)
  • Node firstNameNode (NodeList)
    xPath.evaluate(//firstname, nodes,
    XPathConstants.NODESET)
  • String firstname firstNameNode.getNodeValue()
  • Node lastNameNode (NodeList) xPath.evaluate(//l
    astname, nodes, XPathConstants.NODESET)
  • String lastname lastNameNode.getNodeValue()
  • Node workPlaceNode (NodeList)
    xPath.evaluate(//workplace, nodes,
    XPathConstants.NODESET)
  • String workplace workPlaceNode.getNodeValue()
  • Node emailNode (NodeList) xPath.evaluate(//emai
    l, nodes, XPathConstants.NODESET)
  • String email emailNode.getNodeValue()

19
XMLHelper2.java
  • package helper
  • import javax.xml.xpath.XPath
  • import javax.xml.xpath.XPathConstants
  • import javax.xml.xpath.XPathExpressionException
  • import javax.xml.xpath.XPathFactory
  • import org.w3c.dom.Document
  • import org.w3c.dom.Node
  • import org.w3c.dom.NodeList

20
Constructor
  • public class XMLHelper2
  • public XMLHelper2()
  • // bit blank isnt it

21
getNodeListXPath()
  • public NodeList getNodeListXPath(String
    expression, Document target) throws
    XPathExpressionException
  • // create the XPath and initialize it
  • XPath xPath XPathFactory.newInstance().newXPat
    h()
  • // now execute the XPath select statement
  • NodeList nodes (NodeList) xPath.evaluate(expre
    ssion, target, XPathConstants.NODESET)
  • // return the resulting node
  • return nodes

22
getNumberXPath()
  • public double getNumberXPath(String expression,
    Document target) throws XPathExpressionException
  • // create the XPath and initialize it
  • XPath xPath XPathFactory.newInstance().newXPat
    h()
  • // now execute the XPath select statement
  • Double nodeNumber (Double)xPath.evaluate(expre
    ssion, target, XPathConstants.NUMBER)
  • // return the resulting node
  • return nodeNumber.doubleValue()

23
getStringXPath()
  • public String getStringXPath(String expression,
    Document target) throws XPathExpressionException
  • // create the XPath and initialize it
  • XPath xPath XPathFactory.newInstance().newXPat
    h()
  • // now execute the XPath select statement
  • String nodeText (String)xPath.evaluate(express
    ion, target, XPathConstants.STRING)
  • // return the resulting node
  • return nodeText

24
getChildNodeText()
  • private String getChildNodeText(Node node)
  • String childNodeText node.getTextContent().tri
    m()
  • if(childNodeText.equals("")
    childNodeText.equals("\r")) childNodeText
    null
  • return childNodeText

25
getNodeValue()
  • private String getNodeValue(String expression,
    Document doc) throws Exception
  • String nodeValue null
  • NodeList nodelist
  • Node node
  • nodelist getNodeList(expression, doc)

26
getNodeValue()
  • for(int i 0 i lt nodelist.getLength() i)
  • node nodelist.item(i)
  • nodeValue getChildNodeText(node)
  • if(nodeValue ! null) break
  • return nodeValue

27
Using the XMLHelper class
  • XMLHelper2 xmlHelper new XMLHelper2()
  • DocumentBuilderFactory docBuilderFactory
    DocumentBuilderFactory.newInstance()
  • DocumentBuilder docBuilder docBuilderFactory.new
    DocumentBuilder()
  • // load the XML file into a Document object
  • Document doc docBuilder.parse(new
    File(//trentdev/wwwdatajsp/JWWW/yvan/contacts.xml
    ))
  • // what does this do?
  • doc.normalize()
  • // get the whole node structure
  • NodeList nodelist xmlHelper.getNodeListXPath("/c
    ontacts", doc)
Write a Comment
User Comments (0)
About PowerShow.com