Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intro to XML Originally Presented by Clifford Lemoine Modified by Box.

Similar presentations


Presentation on theme: "Intro to XML Originally Presented by Clifford Lemoine Modified by Box."— Presentation transcript:

1 Intro to XML Originally Presented by Clifford Lemoine Modified by Box

2 Introduction to XML Review of XML Review of XML What is different XML parsing? What is different XML parsing? Simple Example program Simple Example program Wrap-up Wrap-up References References

3 Quick XML Review XML – Wave of the future XML – Wave of the future Method of representing data Method of representing data Differs from HTML by storing and representing data instead of displaying or formatting data Differs from HTML by storing and representing data instead of displaying or formatting data Tags similar to HTML tags, only they are user- defined Tags similar to HTML tags, only they are user- defined Follows a small set of basic rules Follows a small set of basic rules Stored as a simple ASCII text file, so portability is insanely easy Stored as a simple ASCII text file, so portability is insanely easy

4 Quick XML Review Syntax Syntax Every XML document has a preamble Every XML document has a preamble An XML document may or may not have a DTD (Document Type Definition) or Schema An XML document may or may not have a DTD (Document Type Definition) or Schema

5 Quick XML Review Syntax cont. Syntax cont. Every element has a start and end tag, with optional attributes Every element has a start and end tag, with optional attributes … … If an element does not contain any data (or elements) nested within, the closing tag can be merged with the start tag like so: If an element does not contain any data (or elements) nested within, the closing tag can be merged with the start tag like so:

6 Quick XML Review Syntax cont. Syntax cont. Elements must be properly nested Elements must be properly nested The outermost element is called the root element The outermost element is called the root element An XML document that follows the basic syntax rules is called well-formed An XML document that follows the basic syntax rules is called well-formed An XML document that is well-formed and conforms to a DTD or Schema is called valid An XML document that is well-formed and conforms to a DTD or Schema is called valid Once again, XML documents do not always require a DTD or Schema, but they must be well-formed Once again, XML documents do not always require a DTD or Schema, but they must be well-formed

7 Quick XML Review Sample XML files Sample XML files Catalog.xml Catalog.xml

8 John Doe John Doe Title 1 Title 1 Phill Smith Phill Smith His One Book His One Book PC Mag PC Mag Second Headline Second Headline </catalog>

9 Let’s work in our project input Back to our original class diagram (network configuration) Back to our original class diagram (network configuration) Define your input Define your input xml\net.xml xml\net.xml xml\net.xml

10 What is XML Parser? A program or module that checks a well-formed syntax and provides a capability to manipulate XML data elements. A program or module that checks a well-formed syntax and provides a capability to manipulate XML data elements. Navigate thru the XML document (DOM or SAX) Navigate thru the XML document (DOM or SAX) extract or query data elements extract or query data elements Add/delete/modify data elements Add/delete/modify data elements

11 XML Parsing DOM (Document Object Model). DOM (Document Object Model). Reads the whole document and builds DOM tree. Reads the whole document and builds DOM tree. Simple API for XML = SAX Simple API for XML = SAX SAX is an event-based parsing method SAX is an event-based parsing method reads an XML document, firing (or calling) callback methods when certain events are found (e.g. elements, attributes, start/end tags, etc.) reads an XML document, firing (or calling) callback methods when certain events are found (e.g. elements, attributes, start/end tags, etc.) Pull parser (won’t talk more here) Pull parser (won’t talk more here)

12 DOM vs. SAX Parsing? Unlike DOM (Document Object Model), SAX does not store information in an internal tree structure Unlike DOM (Document Object Model), SAX does not store information in an internal tree structure Because of this, SAX is able to parse huge documents (think gigabytes) without having to allocate large amounts of system resources Because of this, SAX is able to parse huge documents (think gigabytes) without having to allocate large amounts of system resources Really great if the amount of data you’re looking to process is relatively large (no waste of memory on tree) Really great if the amount of data you’re looking to process is relatively large (no waste of memory on tree) If processing is built as a pipeline, you don’t have to wait for the data to be converted to an object; you can go to the next process once it clears the preceding callback method If processing is built as a pipeline, you don’t have to wait for the data to be converted to an object; you can go to the next process once it clears the preceding callback method

13 DOM vs. SAX Parsing? Most limitations are the programmer’s problem, not the API’s Most limitations are the programmer’s problem, not the API’s SAX does not allow random access to the file; it proceeds in a single pass, firing events as it goes SAX does not allow random access to the file; it proceeds in a single pass, firing events as it goes Makes it hard to implement cross-referencing in XML (ID and IDREF) as well as complex searching routines Makes it hard to implement cross-referencing in XML (ID and IDREF) as well as complex searching routines

14 XML Parser implementations Apache.org Xerces package at http://xml.apache.org/ Apache.org Xerces package at http://xml.apache.org/Xerces http://xml.apache.org/Xerces http://xml.apache.org/ JDOM.org jdom package www.jdom.org JDOM.org jdom package www.jdom.org

15 Simple Example Program WarReader.java WarReader.java Build document from xml file. Build document from xml file. SAXBuilder builder = new SAXBuilder(); SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(new File(filename)); Document doc = builder.build(new File(filename)); Get the root element (node) Get the root element (node) Element root = doc.getRootElement(); Element root = doc.getRootElement(); Get children of the root Get children of the root List servlets = root.getChildren("servlet"); List servlets = root.getChildren("servlet"); Iterate thru each child and extract more detailed info Iterate thru each child and extract more detailed info xml\WarReader.java xml\WarReader.java xml\WarReader.java

16 Using XML for your Final Term Project Each team spends 10 mins to come up with data structure and XML representation Each team spends 10 mins to come up with data structure and XML representation xml\NetReader.java xml\NetReader.java xml\NetReader.java Demonstration here Demonstration here

17 References Gittleman, Art. Advanced Java: Internet Applications (Second Edition). Scott Jones Publishers. El Granada, California. 2002. pp. 504-511. " JDOM Makes XML Easy"" JDOM Makes XML Easy" slides from JavaOne 2002, http://www.servlets.com/speaking/descriptions.html#jdom http://www.servlets.com/speaking/descriptions.html#jdom " JDOM Makes XML Easy" http://www.servlets.com/speaking/descriptions.html#jdom Janert, Phillip K. “Simple XML Parsing with SAX and DOM.” http://www.onjava.com/pub/a/onjava/2002/06/26/xml.html http://www.onjava.com/pub/a/onjava/2002/06/26/xml.html Published June 26, 2002. Accessed February 10, 2003. Wati, Anjini. “E-Catalog for a Small to Medium Enterprise.” http://ispg.csu.edu.au/subjects/itc594/reports/Tr-005.doc http://ispg.csu.edu.au/subjects/itc594/reports/Tr-005.doc Accessed February 10, 2003.


Download ppt "Intro to XML Originally Presented by Clifford Lemoine Modified by Box."

Similar presentations


Ads by Google