Presentation is loading. Please wait.

Presentation is loading. Please wait.

27-Jun-15 XOM. XOM Design Principles XOM (XML Object Model) is yet another XML parser, by textbook author Elliott Rusty Harold The goals of XOM, in order,

Similar presentations


Presentation on theme: "27-Jun-15 XOM. XOM Design Principles XOM (XML Object Model) is yet another XML parser, by textbook author Elliott Rusty Harold The goals of XOM, in order,"— Presentation transcript:

1 27-Jun-15 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 Node s 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, noChild; Element 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.appendChild(question); node1.appendChild(yesChild); node1.appendChild(noChild);

7 Creating an XOM document, II The portion of the document created so far, if printed, looks like: Does it have stripes? zebra horse

8 Creating an XOM document, III Here’s some additional tree creation, including an attribute: Element 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.appendChild("frog"); noChild = new Element("ifNo"); noChild.appendChild(node1); node2.appendChild(question); node2.appendChild(yesChild); node2.appendChild(noChild);

9 Creating an XOM document, IV Here’s the XML so far: Does it live in the water? frog Does it have stripes? zebra horse

10 Creating an XOM document, V Now to finish up: Element root = new Element("GameData"); root.appendChild(node2); prettyprint(root); The result is the same as earlier, with added 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.setMaxLength(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.getRootElement(), ""); } 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 < 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

16 Getting and installing XOM Go to http://www.xom.nuhttp://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 don’t know about

18 The End


Download ppt "27-Jun-15 XOM. XOM Design Principles XOM (XML Object Model) is yet another XML parser, by textbook author Elliott Rusty Harold The goals of XOM, in order,"

Similar presentations


Ads by Google