Download Distributed Software Development XML: Structure and Parsing | CS 682 and more Study notes Software Engineering in PDF only on Docsity!
Distributed Software Development XML: Structure and Parsing^ Chris BrooksDepartment of Computer ScienceUniversity of San Francisco
Department of Computer Science — University of San Francisco – p. 1/
??
5-2:^ Outline^ •^ About XML^ •^ Structuring XML documents^ •^ Using CSS to display XML^ •^ Parsing with DOM^ •^ Parsing with SAX
Department of Computer Science — University of San Francisco – p. 2/
??
5-3:^ XML^ •^ XML is a language for describing data^ ◦^ Really more of a meta-language^ •^ XML itself provides metadata^ ◦^ Data types, relations between data objects, etc.^ •^ Designed to be read, created, and consumed by programs.
Department of Computer Science — University of San Fra
5-4:^ Advantages of XML^ •^ Well-defined, easy-to-manipulate structure^ •^ Human-readable^ •^ Extensible^ •^ Metadata can be included directly with data^ •^ Widely used
Department of Computer Science — University of San Francisco – p. 4/
??
5-5:^ Things to note^ •^ An XML document has two components:^ ◦^ tags (metadata)^ ◦^ content (data)^ •^ Metadata serves to help an application make sense of the data.
Department of Computer Science — University of San Francisco – p. 5/
??
5-6:^ Example ^ J.R.R.^
Tolkien^
The Lord of^ the^ Rings^ Fellowship^ of^ The^ Ring^ ^ The^ Two^ Towers ^ Return^ of the^ King^
^ 14.95^ ^ Ballantine ^0345340426 Department of Computer Science — University of San Fra
5-7:^ XML documents as trees^ •^ An XML document can also be represented as a tree.^ •^ This makes XML very easy to parse.^ •^ The outermost element is the root element, and elementscontained within it are children of that element.^ •^ Content is stored at the leaves^ •^ What would the tree for our Tolkien example look like?
Department of Computer Science — University of San Francisco – p. 7/
??
5-8:^ Outline^ •^ About XML^ •^ Stucturing XML documents^ •^ Using CSS to display XML^ •^ Parsing with DOM^ •^ Parsing with SAX
Department of Computer Science — University of San Francisco – p. 8/
??
5-9:^ Elements^ •^ XML requires that every starting tag have a correspondingclosing tag.^ •^ Everything between a starting tag and a closing tag is called an^ element^ •^ For example, Return of The King is anelement^ •^ So is everything between and ^ •^ As is everything between and .^ •^ This means that elements must be nested.
Department of Computer Science — University of San Fra
5-10:^ Tags and elements^ •^ Tags form the boundaries of elements, and give processinginstructions to parsers.^ ◦^ Empty elements:
^
All information is contained in the tag. ◦ Container elements:
^ 14.95 ◦^ Comments:^ <!-- here’s
a^ comment --> ◦^ Declaration:^ <!ENTITY jrrt
’’J.R.R.^ Tolkien’’> This provides a way to define variables or constants in asingle location. ◦ Entity reference:^
&jrrt
^ Department of Computer Science — University of San Francisco – p. 10/
??
5-11:^ Attributes and Values^ •^ You can also specify that an element has
attributes
-^ These attributes can take on
values
-^ This is helpful when you want to specify that an object belongsto one of a few types.^ <book^ genre="fantasy"
size="large">^ ...
Department of Computer Science — University of San Francisco – p. 11/
??
5-12:^ Attributes vs. Sub-elements^ •^ We could rewrite the example above using subelementsinstead of attributes.^ •^ When to use one over the other is largely stylistic.^ ◦^ Can always transform one into the other^ •^ If a feature can only take on one of a few values, an attributemight make more sense.^ •^ If we expect to extend the number of genres, a subelement ispreferable.^ •^ Also, order is preserved for subelements^ ◦^ Semantically, attribute/value pairs are treated as adictionary.^ •^ So, a list of authors should be done as subelements
Department of Computer Science — University of San Fran
5-19:^ Entities^ •^ We could then use our entity definitions later in the documentby prepending a ’&’ to them^ ^ ...^
the^ Author^ of^ The^
Lord^ of^ the^ Rings^
is^ &jrrt;^ he invented^ a^ grammar
and^ semantics^ for
Elvish,^ which^ can
be^ found^ at
&elvish-key;
Department of Computer Science — University of San Francisco – p. 19/
??
5-20:^ Outline^ •^ About XML^ •^ Stucturing XML documents^ •^ Using CSS to display XML^ •^ Parsing with DOM^ •^ Parsing with SAX
Department of Computer Science — University of San Francisco – p. 20/
??
5-21:^ Using CSS to display XML^ •^ CSS can also be used to display XML documents.^ •^ Control is limited to laying out a complete XML document.^ •^ If we want filtering or sorting, we’ll need to use XSLT.
Department of Computer Science — University of San Fran
5-22:^ An example^ •^ Let’s say we have an XML-based CD database:^ •^ We can use CSS to display it in a web browser.^ •^ (see separate examples)
Department of Computer Science — University of San Francisco – p. 22/
??
5-23:^ Outline^ •^ About XML^ •^ Stucturing XML documents^ •^ Validating XML with schema^ •^ Using CSS to display XML^ •^ Parsing with DOM^ •^ Parsing with SAX
Department of Computer Science — University of San Francisco – p. 23/
??
5-24:^ Parsing XML^ •^ XML also has the advantage of being easy for programs toparse and construct.^ •^ There are two different approaches to parsing and manipulatingXML.^ •^ SAX: Simple API for XML^ ◦^ Event-driven parser^ ◦^ User defines actions to take when an element is foundduring parsing.
Department of Computer Science — University of San Fran
5-25:^ Parsing XML^ •^ DOM: Document Object Model^ ◦^ Tree parser: Entire document is instantiated in memory as atree.^ ◦^ Nice for random-access applications^ ◦^ Large documents may consume a large amount of memory^ •^ Most languages provide support for both. We’ll start with DOM.
Department of Computer Science — University of San Francisco – p. 25/
??
5-26:^ Libraries^ •^ The DOM model is specified in a language independent way.^ •^ Implementations then follow this specification.^ ◦^ This means that they all work very similarly.^ •^ Java^ ◦^ javax.xml.parsers built into Java 1.5^ ◦^ Apache’s Xerces parser provides support for both SAX andDOM.^ •^ Xerces also has C++ and Perl implementations^ ◦^ JDOM is also a popular tool for parsing and creating XML inJava.^ •^ Python^ ◦^ Built-in support for SAX, DOM, and minidom
Department of Computer Science — University of San Francisco – p. 26/
??
5-27:^ Libraries^ •^ Perl^ ◦^ LibXML provides SAX and DOM functionality.^ •^ C#^ ◦^ .NET has built-in support for SAX and DOM
Department of Computer Science — University of San Fran
5-28:^ Parsing a document in Python^ •^ Example:^ from^ xml.dom^ import
minidom doc=^ minidom.parse(’library.xml’) • Reads in and parses a document • creates a Document object. • toxml() show the XML version.
Department of Computer Science — University of San Francisco – p. 28/
??
5-29:^ Traversing the tree^ •^ childNodes, firstChild, lastChild, parentNode^ •^ childNodes can have childNodes.^ •^ Leaves are text nodes,^ ◦^ Respond to ’data’, which gives up the data they store.^ •^ This is useful if you need to process an entire document, butannoying if you’re searching.
Department of Computer Science — University of San Francisco – p. 29/
??
5-30:^ Finding specific elements^ •^ getElementsByTagName finds all elements according to name:^ eltlist^ =^ doc.getElementsByTagName(’key’)^ •^ Can search at any node
Department of Computer Science — University of San Fran
5-37:^ Using SAX within Python import^ xml.saxfrom^ xml.sax.handler
import^ * class^ CDHandler(ContentHandler)
: def^ init(self)
:self.books = [];self.buffer = ’’self.inTitle = False def^ startElement(self,
name,^ attrs)^ : if^ name^ ==^ ’title’
:self.inTitle = True def^ endElement(self,
name)^ :if name == ’title’^ :self.inTitle = Falseprint self.bufferself.buffer = ’’ def^ characters(self,
ch)^ :if self.inTitle :self.buffer += ch
Department of Computer Science — University of San Francisco – p. 37/
??
5-38:^ Using SAX within Python^ •^ To use this, we then register the handler with a SAX parser.^ parser^ =^ xml.sax.make_parser()handler^ =^ CDHandler()parser.setContentHandler(handler)parser.parse(’cdcat.xml’)
Department of Computer Science — University of San Francisco – p. 38/
??
5-39:^ SAX comments^ •^ You must keep track of ’where you are’ yourself.^ ◦^ No access to the enclosing context^ ◦^ It’s hard with SAX to, for example, print the correspondingartist for each title node.^ •^ SAX has more modest memory requirements than DOM^ ◦^ Nodes are discarded after parsing^ •^ More flexible recovery from parsing errors.^ •^ Use the parser that best fits your needs.
Department of Computer Science — University of San Fran