1 / 67

JDOM

JDOM. Notes from Rusty Harold’s Processing XML with JAVA http://www.cafeconleche.org/books/xmljava/chapters/ch14.html. What’s wrong with DOM? . DOM needed to be downward/backward compatible to poorly designed object modules in older browsers

Gabriel
Télécharger la présentation

JDOM

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JDOM Notes from Rusty Harold’s Processing XML with JAVA http://www.cafeconleche.org/books/xmljava/chapters/ch14.html

  2. What’s wrong with DOM? • DOM needed to be downward/backward compatible to poorly designed object modules in older browsers • DOM was designed by committee trying to reconcile differences between Netscape, MS IE, and so on and to develop something minimally acceptable to all parties • DOM is a cross language API defined in IDL, limited to features of languages like js and Vb – not fully OO. • DOM needs to work for XHTLM, but also malformed HTML.

  3. JDOM is java • JDOM was created by Jason Hunter and Brett McLaughlin of O’Reilly to fix the problems they saw with DOM. • Interfaces (like the Node interface of DOM) are replaced by concrete classes. • Things work the way java programmers expect them to work. (java naming conventions are used, for example). • JDOM is to DOM as Java is to C++.

  4. JDOM is open-source • JDOM uses the apache licensing. • It is a tree-based API for creating, outputing, manipulating, and serializing XML documents. • XML is represented as a tree composed of elements, attributes, comments, processing instructions, text nodes, CDATA sections, etc.

  5. JDOM uses java conventions and class libraries • JDOM classes have equals(), toString() and hashCode() methods. • They implement Clonable amnd Serializable interfaces. • Children of elements are stored in java.util.List

  6. More… • Like DOM and unlike SAX, JDOM can build a new XML tree in memory. Data can come from a database, or anywhere. • JDOM checks data for well-formedness • JDOM unlike SAX can modify a document and you can serialize it back out to output or elsewhere.

  7. Creating XML with JDOM • Harold follows an example of fibonaci numbers. To create the JDOM of <fibonacci/> you would write Element element = new Element(“fibonacci”); • (DOM requires a document builder factory to make a document, an implementation and then you can use the doc to create an element.) • To add text to <fibonacci/> you would use, eg., Element element = new Element(“fibonacci”); Element.setText(“8”);

  8. More… • For the element above, you could set its index (attribute) to 6 by adding the codeelement.setAttribute(“index”,”6”); • You can add more to an element (child elements, comments, etc) using the addContent() method:

  9. An example • To create <sequence> <number>3</number> <number>3</number> </sequence> • You need three elements (two number elements and a sequence element)

  10. Example continued Element element=new Element(“sequence”); Element first=new Element(“number”); Element second=new Element(“number”); First.setText(“3”); Second.setText(“5”); Element.addContent(first); Element.addContent(second); • What this really produces is this element: <sequence><number>3</number><number>5</number></sequence>

  11. Example continued • White space is significant in XML and thus significant in JDOM. If you want the nicely indented element, you also need to add some strings containing the appropriate white space like this: Element element = new Element("sequence"); Element firstNumber = new Element("number"); Element secondNumber = new Element("number"); firstNumber.setText("3"); secondNumber.setText("5"); element.addContent("\n "); element.addContent(firstNumber); element.addContent("\n "); element.addContent(secondNumber); element.addContent("\n"); • If you only care about the extra white space when the document is serialized, you can ask an XMLOutputter to insert it for you.

  12. Creating XML Documents with JDOM <?xml version="1.0"?> <GREETING> Hello JDOM! </GREETING> • Since all documents should have root elements, we’ll need to create the root GREETING element first, then use that element to create the document: • Element root = new Element("GREETING"); root.setText("Hello JDOM!"); Document doc = new Document(root); • Note • Initially the Element object is not associated with any Document. It is free-standing. This contrasts with DOM where all nodes are always part of some document. JDOM allows nodes to stand on their own if that’s useful. However, JDOM does not allow a node to be part of two documents at once. Before an Element can be transferred into a new Document it must first be detached from its old document using its detach() method.

  13. Note • Initially the Element object is not associated with any Document. It is free-standing. This contrasts with DOM where all nodes are always part of some document. JDOM allows nodes to stand on their own if that’s useful. However, JDOM does not allow a node to be part of two documents at once. Before an Element can be transferred into a new Document it must first be detached from its old document using its detach() method.

  14. Writing XML Documents with JDOM • Once you’ve created a document, you’re likely to want to serialize it to a network socket, a file, a string, or some other stream. JDOM’s org.jdom.output.XMLOutputter class does this in a standard way. You can create an XMLOutputter object with a no-args constructor and then write a document onto an OutputStream with its output() method. For example, this code fragment writes the Document object named doc onto System.out. • XMLOutputter outputter = new XMLOutputter(); • try { outputter.output(doc, System.out); } catch (IOException e) { System.err.println(e); } • Besides streams you can also output a document onto a java.io.Writer. However, it’s recommended that you use an OutputStream because it’s generally not possible to determine the underlying encoding of a Writer and set the encoding declaration accordingly.

  15. Writing XML fragments • Besides documents, XMLOutputter can write elements, attributes, CDATA sections, and all the other JDOM node classes. For example, this code fragment writes an empty element named Greeting onto System.out: XMLOutputter outputter = new XMLOutputter(); try { Element element = new Element("Greeting"); outputter.output(element, System.out); } catch (IOException e) { System.err.println(e); }

  16. Or write to a String • This may occasionally be useful; but if you write anything other than a single Document or Element onto a stream, the result probably won’t be a well-formed XML document. • Finally, instead of writing onto a stream or writer, you can use the outputString() methods to store an XML document or node in a String. This is often useful when passing XML data through non-XML aware systems. For example, this code fragment stores an empty element named Greeting in the String variable named hello: XMLOutputter outputter = new XMLOutputter(); Element element = new Element("Greeting"); String hello = outputter.outputString(element);

  17. A JDOM program that produces an XML document containing Fibonacci numbers import org.jdom.*; import org.jdom.output.XMLOutputter; import java.math.BigInteger; import java.io.IOException; public class FibonacciJDOM { public static void main(String[] args) { Element root = new Element("Fibonacci_Numbers"); BigInteger low = BigInteger.ONE; BigInteger high = BigInteger.ONE; for (int i = 1; i <= 5; i++) { Element fibonacci = new Element("fibonacci"); fibonacci.setAttribute("index", String.valueOf(i)); root.addContent(fibonacci); BigInteger temp = high; high = high.add(low); low = temp; } Document doc = new Document(root); // serialize it onto System.out try { XMLOutputter serializer = new XMLOutputter(); serializer.output(doc, System.out); } catch (IOException e) { System.err.println(e); } } }

  18. The output is as follows: • D:\books\XMLJAVA\examples\14>java FibonacciJDOM <?xml version="1.0" encoding="UTF-8"?> <Fibonacci_Numbers><fibonacci index="1">1</fibonacci><fibonacci index="2">1</fibonacci><fibonacci index="3">2</fibonacci> <fibonacci index="4">3</fibonacci><fibonacci index="5">5 </fibonacci></Fibonacci_Numbers>

  19. Whitespace and cr can be set • You can also specify the amount of indenting to use and whether or not to add line breaks as arguments to the XMLOutputter() constructor like this: XMLOutputter serializer = new XMLOutputter(" ", true); serializer.output(doc, System.out);

  20. A database example…writing xml from java public static void convert(List data, OutputStream out) throws IOException { Writer wout = new OutputStreamWriter(out, "UTF8"); wout.write("<?xml version=\"1.0\"?>\r\n"); wout.write("<Budget>\r\n"); Iterator records = data.iterator(); while (records.hasNext()) { wout.write(" <LineItem>\r\n"); Map record = (Map) records.next(); Set fields = record.entrySet(); Iterator entries = fields.iterator(); while (entries.hasNext()) { Map.Entry entry = (Map.Entry) entries.next(); String name = (String) entry.getKey(); String value = (String) entry.getValue(); /* some of the values contain ampersands and less than // signs that must be escaped */ value = escapeText(value); wout.write(" <" + name + ">"); wout.write(value); wout.write("</" + name + ">\r\n"); } wout.write(" </LineItem>\r\n"); } wout.write("</Budget>\r\n"); wout.flush(); }

  21. JDOM can make this method quite a bit simpler public static void convert(List data, OutputStream out) throws IOException { Element budget = new Element("Budget"); Iterator records = data.iterator(); while (records.hasNext()) { Element lineItem = new Element("LineItem"); budget.addContent(lineItem); Map record = (Map) records.next(); Set fields = record.entrySet(); Iterator entries = fields.iterator(); while (entries.hasNext()) { Map.Entry entry = (Map.Entry) entries.next(); String name = (String) entry.getKey(); String value = (String) entry.getValue(); Element category = new Element(name); category.setText(value); lineItem.addContent(category); } } Document doc = new Document(budget); XMLOutputter outputter = new XMLOutputter(" ", true); outputter.output(doc, out); out.flush(); }

  22. Handling DocType for the XML Element root = new Element("Fibonacci_Numbers"); DocType type = new DocType("Fibonacci_Numbers", "fibonacci.dtd"); Document doc = new Document(root, type);

  23. You can also use the setInternalSubset() method to provide an internal DTD subset. • As with all internal DTD subsets, this can be instead of or in addition to the external DTD subset identified by the public ID and the system ID. For example, this code fragment uses an internal DTD subset instead of an external DTD subset. Element root = new Element("Fibonacci_Numbers"); DocType type = new DocType("Fibonacci_Numbers"); String dtd = "<!ELEMENT Fibonacci_Numbers (fibonacci*)>\n"; dtd += "<!ELEMENT fibonacci (#PCDATA)>\n"; dtd += "<!ATTLIST fibonacci index CDATA #IMPLIED>\n"; type.setInternalSubset(dtd); Document doc = new Document(root, type);

  24. Reading XML with JDOM • The rough outline for working with an existing XML document using JDOM is as follows: • Construct an org.jdom.input.SAXBuilder object using a simple no-args constructor • Invoke the builder’s build() method to build a Document object from a Reader, InputStream, URL, File, or a String containing a system ID. • If there’s a problem reading the document, an IOException is thrown. If there’s a problem building the document, a JDOMException is thrown. • Otherwise, navigate the document using the methods of the Document class, the Element class, and the other JDOM classes.

  25. Recall, JDOM uses the SAX parser • The SAXBuilder class represents the underlying XML parser. Parsing a document from a URL is straightforward. Just create a SAXBuilder object with the no-args constructor and pass the string form of the URL to its build() method. This returns a JDOM Document object. For example, • SAXBuilder parser = new SAXBuilder(); Document doc = parser.build("http://www.cafeconleche.org/"); // work with the document...

  26. A JDOM program that checks XML documents for well-formedness import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; import java.io.IOException; public class JDOMChecker { public static void main(String[] args) { if (args.length == 0) { System.out.println("Usage: java JDOMChecker URL"); return; } SAXBuilder builder = new SAXBuilder(); /* command line should offer URIs or file names */ try { builder.build(args[0]); /* If there are no well-formedness errors, then no exception is thrown */ System.out.println(args[0] + " is well-formed."); } // indicates a well-formedness error catch (JDOMException e) { System.out.println(args[0] + " is not well-formed."); System.out.println(e.getMessage()); } catch (IOException e) { System.out.println("Could not check " + args[0]); System.out.println(" because " + e.getMessage()); } } }

  27. A JDOM program that validates XML documents import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; import java.io.IOException; public class JDOMValidator { public static void main(String[] args) { if (args.length == 0) { System.out.println("Usage: java JDOMValidator URL"); return; } SAXBuilder builder = new SAXBuilder(true); // ^^^^ // Turn on validation // command line should offer URIs or file names try { builder.build(args[0]); // If there are no well-formedness or validity errors, // then no exception is thrown. System.out.println(args[0] + " is valid."); } // indicates a well-formedness or validity error catch (JDOMException e) { System.out.println(args[0] + " is not valid."); System.out.println(e.getMessage()); } catch (IOException e) { System.out.println("Could not check " + args[0]); System.out.println(" because " + e.getMessage()); } } }

  28. Note • JDOM does not currently distinguish between validity and well-formedness errors.

  29. Navigating JDOM Trees • Once you’ve parsed a document and formed a Document object, you’ll probably want to search it to select out those parts of it your program is interested in. In JDOM, most navigation takes place through the methods of the Element class. The complete children of each Element are available as a java.util.List returned by the getContent() method. Just the child elements of each Element are available in a java.util.List returned by the getChildren() method. (Yes, the terminology is a little confusing here. This is a case where JDOM is marching out of step with the rest of the XML world. JDOM uses the word children to refer only to child elements.)

  30. remarks • Because JDOM uses the Java Collections API to manage the tree, it is simultaneously too polymorphic (everything’s an object and must be cast to the right type before you can use it) and not polymorphic enough (there’s no useful generic interface or superclass for navigation such as DOM’s Node class.) Consequently, you’re going to find yourself doing numerous tests with instanceof and casting to the determined type. This is far and away my least favorite part of JDOM’s design. Furthermore, there’s no standard traversal API as there is in DOM to help you avoid reinventing the wheel every time you need to walk a tree or iterate a document. There is a Filter interface that can simplify some of the polymorphism and casting issues a little, but it still won’t let you walk more than one level down the tree at a time.

  31. namespaces • I omitted coverage of namespace inclusion here

  32. A JDOM program that lists the elements used in a document import org.jdom.*; import org.jdom.input.SAXBuilder; import java.io.IOException; import java.util.*; public class ElementLister { public static void main(String[] args) { if (args.length == 0) { System.out.println("Usage: java ElementLister URL"); return; } SAXBuilder builder = new SAXBuilder(); try { Document doc = builder.build(args[0]); Element root = doc.getRootElement(); listChildren(root, 0); } // indicates a well-formedness error catch (JDOMException e) { System.out.println(args[0] + " is not well-formed."); System.out.println(e.getMessage()); } catch (IOException e) { System.out.println(e); } }

  33. Continuedhttp://www.cafeconleche.org/books/xmljava/chapters/ch14s08.htmlContinuedhttp://www.cafeconleche.org/books/xmljava/chapters/ch14s08.html • public static void listChildren(Element current, int depth) { printSpaces(depth); System.out.println(current.getName()); List children = current.getChildren(); Iterator iterator = children.iterator(); while (iterator.hasNext()) { Element child = (Element) iterator.next(); listChildren(child, depth+1); } } private static void printSpaces(int n) { for (int i = 0; i < n; i++) { System.out.print(' '); } } }

  34. Output (when run on pastry.xml from previous ppt) C:\PROGRA~1\JAVA\JDK15~1.0_0\BIN>java ElementLister pastry.xml donuts jelly lemon lemon glazed C:\PROGRA~1\JAVA\JDK15~1.0_0\BIN>

  35. Running JDOM • You’ll have to go find JDOM and download the zip file • You’ll have to put the jdom.jar in a directory called org • You’ll have to put directory org in your classpath even if it is in the java/bin directory

  36. A JDOM program that lists the nodes used in a document • Code in notes • Output from NodeLister on pastry.xml C:\PROGRA~1\JAVA\JDK15~1.0_0\BIN>java NodeLister pastry.xml Document Comment Comment Unexpected type: class org.jdom.DocType Element: donuts Text Element: jelly Text Text Element: lemon Text Text Element: lemon Text Text Element: glazed Text Text

  37. Getting attributes and namespaces • The only pieces that are missing here are the attributes and namespaces associated with each element. These are not included by either getContent() or getChildren(). If you want them, you have to ask for them explicitly using the getAttributes(), getNamespace(), getAdditionalNamespaces(), and related methods of the Element class.

  38. Talking to DOM Programs: About JDOM and DOM Elements • A JDOM Element is not a DOM Element. Each has methods and interfaces the other does not have. You can’t pass a JDOM Element to a method expecting a DOM Element, and so on. • The same is true for JDOM documents and DOM documents. (as well as their corresponding attribute classes/interfaces, processing instruction classes/interfaces and so on).

  39. You can convert a DOM doc into a JDOM doc • DOMBuilder builder =new DOMBuilder(); • Org.jdom.Document jdomDocument = builder.build(domDocument); • //now work with the jdom doc • Going the other wy, the org.jdom.output.DOMOutputter class produces DOM docs from JDOM doc (objects).

  40. You can convert a JDOM doc into a DOM doc • DOMOutputter converter = new DOMOutputter(); • Org.w3c.dom.Document domdoc=converter.output(jdomDoc); • //now work with the DOM doc • The documents are not connected or related after translation has occurred.

  41. Talking to SAX Programs • JDOM works very well with SAX parsers. SAX is an almost ideal event model for building a JDOM tree; and when the tree is complete, JDOM makes it easy to walk the tree, firing off SAX events as you go. Since SAX is so fast and memory-efficient, SAX doesn’t add a lot of extra overhead to JDOM programs.

  42. Configuring SAXBuilder • When reading a file or stream through a SAX parser, you can set various properties on the parser including the ErrorHandler, DTDHandler, EntityResolver, and any custom features or properties that are supported by the underlying SAX XMLReader. SAXBuilder includes several methods that just delegate these configurations to the underlying XMLReader: • public void setErrorHandler(ErrorHandler errorHandler);public void setEntityResolver(EntityResolver entityResolver);public void setDTDHandler(DTDHandler dtdHandler);public void setIgnoringElementContentWhitespace(boolean ignoreWhitespace);public void setFeature(String name, boolean value);public void setProperty(String name, Object value);

  43. Schema validation • For example, suppose you want to schema validate documents before using them. This requires three additional steps beyond the norm: • Explicitly pick a parser class that is known to be able to schema validate such as org.apache.xerces.parsers.SAXParser (Most parsers can’t schema validate.) • Install a SAX ErrorHandler that reports validity errors. • Set the SAX feature that turns on schema validation to true. Which feature this is depends on which parser you picked in step 1. In Xerces, it’s http://apache.org/xml/features/validation/schema and you also need to turn validation on using the standard SAX feature http://xml.org/sax/features/validation

  44. A JDOM program that schema validates documents import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; import java.io.IOException; public class JDOMSchemaValidator { public static void main(String[] args) { if (args.length == 0) { System.out.println("Usage: java JDOMSchemaValidator URL"); return; } SAXBuilder builder = new SAXBuilder( "org.apache.xerces.parsers.SAXParser"); builder.setValidation(true); builder.setErrorHandler(new BestSAXChecker()); // ^^^^^^^^^^^^^^ // From Chapter 7 // turn on schema support builder.setFeature("http://apache.org/xml/features/validation/schema", true); // command line should offer URIs or file names try { builder.build(args[0]); } // indicates a well-formedness error catch (JDOMException e) { System.out.println(args[0] + " is not well-formed."); System.out.println(e.getMessage()); } catch (IOException e) { System.out.println("Could not check " + args[0]); System.out.println(" because " + e.getMessage()); } } }

  45. A SAX program that reports all problems found in an XML document • in notes…of next slide • from chpt 7 of Harold’s text • Program generates line numbered errors

  46. I had to remove the string from the builder constructor to get this to work • import org.xml.sax.*; import org.xml.sax.helpers.XMLReaderFactory; import java.io.IOException; public class BestSAXChecker implements ErrorHandler { public void warning(SAXParseException exception) { System.out.println("Warning: " + exception.getMessage()); System.out.println(" at line " + exception.getLineNumber() + ", column " + exception.getColumnNumber()); System.out.println(" in entity " + exception.getSystemId()); } public void error(SAXParseException exception) { System.out.println("Error: " + exception.getMessage()); System.out.println(" at line " + exception.getLineNumber() + ", column " + exception.getColumnNumber()); System.out.println(" in entity " + exception.getSystemId()); } public void fatalError(SAXParseException exception) { System.out.println("Fatal Error: " + exception.getMessage()); System.out.println(" at line " + exception.getLineNumber() + ", column " + exception.getColumnNumber()); System.out.println(" in entity " + exception.getSystemId()); } public static void main(String[] args) { if (args.length <= 0) { System.out.println("Usage: java BestSAXChecker URL"); return; } String document = args[0]; try { XMLReader parser = XMLReaderFactory.createXMLReader(); ErrorHandler handler = new BestSAXChecker(); parser.setErrorHandler(handler); parser.parse(document); // If the document isn't well-formed, an exception has // already been thrown and this has been skipped. System.out.println(document + " is well-formed."); } catch (SAXParseException e) { System.out.print(document + " is not well-formed at "); System.out.println("Line " + e.getLineNumber() + ", column " + e.getColumnNumber() ); System.out.println(" in entity " + e.getSystemId()); } catch (SAXException e) { System.out.println("Could not check document because " + e.getMessage()); } catch (IOException e) { System.out.println( "Due to an IOException, the parser could not check " + document ); } } }

  47. Got this to validate Author.xml with Author.xsd from Peltzer • C:\PROGRA~1\JAVA\JDK15~1.0_0\BIN>java JDOMSchemaValidator Author2.xml

  48. Author.xml <Author xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\Author.xsd"> <Name>Dwight Peltzer</Name> <Address>PO Box 555</Address> <City>Oyster Bay</City> <State>NY</State> <Zip>11771</Zip> </Author>

  49. String representations The JDOM toString() methods produce strings that look like these: [Document:  No DOCTYPE declaration, Root is [Element: <html  [Namespace: http://www.w3.org/1999/xhtml]/>]][Element: <html [Namespace: http://www.w3.org/1999/xhtml]/>][Attribute: xml:lang="en"][Text:][Attribute: type="text/css"][Attribute: rel="stylesheet"][Text: Latest Version: ][Element: <a [Namespace: http://www.w3.org/1999/xhtml]/>][Attribute: href="http://www.rddl.org/"][Text: June 16, 2002]

  50. Author.xsd <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="Author"> <xs:annotation> <xs:documentation> </xs:documentation> </xs:annotation> <xs:complexType> <xs:sequence> <xs:element name="Name" type="xs:string"/> <xs:element name="Address" type="xs:string"/> <xs:element name="City" type="xs:string"/> <xs:element name="State" type="xs:string"/> <xs:element name="Zip" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>

More Related