XOM - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

XOM

Description:

... same way you would any other JAR file. In Eclipse, this means Project ... Java Build Path Libraries Add External Jars.. Note: This is the only JAR file you need ... – PowerPoint PPT presentation

Number of Views:56
Avg rating:3.0/5.0
Slides: 19
Provided by: DaveMa56
Category:
Tags: xom | jars

less

Transcript and Presenter's Notes

Title: XOM


1
XOM
2
XOM Design Principles
  • XOM (XML Object Model) is yet another XML parser,
    by textbook author Elliott Rusty Harold
  • The goals of XOM, in order, are
  • Correctness
  • Only accepts well-formed XML documents
  • Only produces well-formed, round-trippable XML
    documents
  • Simplicity
  • Easy to use Make easy things easy, hard things
    possible
  • Easy to learn Minimum number of classes and
    methods uses Java idioms where they make sense
  • Performance
  • Fast enough Not as fast as SAX, because it uses
    a SAX parser
  • Small enough 2 - 3 times smaller than DOM

3
Node types
  • Node is an abstract class that extends Object
  • All the contents of an XML document are
    represented as Nodes
  • There are eight concrete subclasses
  • Element
  • Document
  • Text
  • Comment
  • Attribute
  • ProcessingInstruction
  • DocType
  • Namespace

4
ParentNode methods
  • A ParentNode is a Node that may have children
  • There are two subclasses Document and Element
  • Some of the methods are
  • void appendChild(Node child)
  • void insertChild(Node child, int position)
  • Node removeChild(Node child)
  • Node removeChild(int position)
  • int getChildCount()
  • Node getChild(int position)
  • int indexOf(Node child)

5
Element methods
  • Element(String name) // constructor
  • void appendChild(String text)
  • int getAttributeCount()
  • void addAttribute(Attribute attribute)
  • Attribute getAttribute(int index)
  • Attribute getAttribute(String name)
  • Elements getChildElements()
  • Elements is another class, with methods
  • Element get(int index)
  • int size()

6
Creating an XOM document, I
  • Element question, yesChild, noChildElement
    node1 new Element("item") question new
    Element("question") question.appendChild("Does
    it have stripes?") yesChild new
    Element("ifYes") yesChild.appendChild("zebra")
    noChild new Element("ifNo")
    noChild.appendChild("horse") node1.appendChi
    ld(question) node1.appendChild(yesChild) node1
    .appendChild(noChild)

7
Creating an XOM document, II
  • The portion of the document created so far, if
    printed, looks likeltquestiongtDoes it have
    stripes?lt/questiongtltifYesgtzebralt/ifYesgtltifNogthor
    selt/ifNogt

8
Creating an XOM document, III
  • Heres some additional tree creation, including
    an attributeElement node2 new
    Element("item")question new
    Element("question") question.appendChild("Does
    it live in the water?")yesChild new
    Element("ifYes") yesChild.addAttribute(new
    Attribute("color", "green")) yesChild.appendChil
    d("frog")noChild new Element("ifNo") noChild
    .appendChild(node1)node2.appendChild(question)
    node2.appendChild(yesChild)node2.appendChild(no
    Child)

9
Creating an XOM document, IV
  • Heres the XML so farltGameDatagt ltitemgt
    ltquestiongtDoes it live in the
    water?lt/questiongt ltifYes
    color"green"gtfroglt/ifYesgt ltifNogt
    ltitemgt ltquestiongtDoes it have
    stripes?lt/questiongt
    ltifYesgtzebralt/ifYesgt
    ltifNogthorselt/ifNogt lt/itemgt
    lt/ifNogt lt/itemgtlt/GameDatagt

10
Creating an XOM document, V
  • Now to finish upElement root new
    Element("GameData")root.appendChild(node2)pret
    typrint(root)
  • The result is the same as earlier, with
    lt?xml version"1.0" encoding"ISO-8859-1"?gtadded
    at the top

11
Printing the document
  • void prettyprint(Element root) Document doc
    new Document(root) try Serializer
    serializer new
    Serializer(System.out, "ISO-8859-1")
    serializer.setIndent(4) serializer.setMaxLengt
    h(64) serializer.write(doc) catch
    (IOException ex) System.err.println(ex)

12
Reading and parsing an XML file
  • File file chooseFile()try Builder builder
    new Builder() Document doc
    builder.build(file) // Done reading and
    parsing now print preorderPrint(doc.getRootEleme
    nt(), "")catch (ValidityException ex) ...
    catch (ParsingException ex) ... catch
    (IOException ex) ...

13
My preorderPrint method
  • private void preorderPrint(Element root, String
    indent) if (root.getChildCount() 0)
    System.out.print(indent root.getLocalName()
    ) System.out.println(" "
    root.getValue()) else
    System.out.println(indent root.getLocalName())
    Elements children root.getChildElements(
    ) for (int i 0 i lt children.size()
    i) preorderPrint(children.get(i),
    indent " ")

14
Making a validating XML parser
  • First, specify a DTD
  • Second, replace Builder builder new
    Builder() Document doc builder.build(file)
    with Builder builder new Builder(true)
    Document doc builder.build(file)

15
My DTD
  • lt!ELEMENT GameData (item)gtlt!ELEMENT item
    (question, ifYes, ifNo)gtlt!ELEMENT question
    (PCDATA)gtlt!ELEMENT ifYes (PCDATAitem)gtlt!ELEM
    ENT ifNo (PCDATAitem)gtlt!ATTLIST ifYes color
    CDATA "brown"gt

16
Getting and installing XOM
  • Go to http//www.xom.nu
  • Download either
  • The Minimal JAR file, xom-1.0.jar, or
  • The Complete distribution, zip format which
    contains source code, Javadoc documentation, and
    xom-1.0.jar (3.8MB)
  • Use the same way you would any other JAR file
  • In Eclipse, this means Project ? Properties... ?
    Java Build Path ? Libraries ? Add External Jars..
  • Note This is the only JAR file you need
  • Older documentation may say you also need Xerces,
    but a stripped-down version is now included in
    the XOM file

17
Comments
  • The XOM API looks large, but it really is simple
    you can do most of what you need with just a few
    classes
  • There are almost no convenience methods
  • Building a document could be a lot easier
  • You can easily write your own convenience methods
  • Problem For me, getChildCount() never returns
    zero
  • This is to be expected with a nonvalidating
    parser that assumes any whitespace could be a
    child
  • I think my problem is that there are some
    children that I dont know about

18
The End
Write a Comment
User Comments (0)
About PowerShow.com