Documents created with JDOM can have document type declarations and thus can be valid. JDOM does not offer a complete object model for DTDs, but it does allow you to point at an existing DTD or add an internal DTD subset to your documents. Example 14.2 is a simple document type definition (DTD) for the Fibonacci number documents we've been generating. ## Example 14.2 A Fibonacci DTD<!ELEMENT Fibonacci_Numbers (fibonacci*)> <!ELEMENT fibonacci (#PCDATA)> <!ATTLIST fibonacci index CDATA #IMPLIED> Let's assume that this is available at the relative URL fibonacci.dtd. Thus the following <!DOCTYPE Fibonacci_Numbers SYSTEM "fibonacci.dtd"> In JDOM, the DocType type = new DocType("Fibonacci_Numbers", "fibonacci.dtd"); You can either pass this Example 14.3 demonstrates a program that produces a completely valid document. However, JDOM does not provide any direct means to test the validity of a document, short of serializing it and passing the resulting stream through a validating parser. ## Example 14.3 A JDOM Program That Produces a Valid XML Documentimport org.jdom.*; import org.jdom.output.XMLOutputter; import java.math.BigInteger; import java.io.IOException; public class ValidFibonacci { public static void main(String[] args) { Element root = new Element("Fibonacci_Numbers"); DocType type = new DocType("Fibonacci_Numbers", "fibonacci.dtd"); Document doc = new Document(root, type); 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)); fibonacci.setText(low.toString()); root.addContent(fibonacci); BigInteger temp = high; high = high.add(low); low = temp; } // serialize with two-space indents and extra line breaks try { XMLOutputter serializer = new XMLOutputter(" ", true); serializer.output(doc, System.out); } catch (IOException e) { System.err.println(e); } } } Here is the output with the document type declaration in place: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE Fibonacci_Numbers SYSTEM "fibonacci.dtd"> <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> You also can specify a public ID for the external DTD subset. For example, if the public ID is DocType type = new DocType("Fibonacci_Numbers", "-//Elliotte Rusty Harold//Fibonacci Example//EN", "fibonacci.dtd"); You can also use the 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); The document produced includes an internal DTD subset and no system or public ID: <?xml version="1.0" encoding="UTF-8" <!DOCTYPE Fibonacci_Numbers [ <!ELEMENT Fibonacci_Numbers (fibonacci*)> <!ELEMENT fibonacci (#PCDATA)> <!ATTLIST fibonacci index CDATA #IMPLIED> ]> <Fibonacci_Numbers> <fibonacci index="1">1</fibonacci> ... Other programs and documents might specify both an internal DTD subset and a system and public ID for the external DTD subset. Note DTDs and document type declarations are most important for serialization. If the document is written onto a stream and read by some other program, then that other program may take advantage of the DTD for validation or application of default attribute values and so forth. However, JDOM itself doesn't pay a lot of attention to the DTD. In fact, as far as it's concerned the various parts are all just strings. It does not, for example, apply any default attribute values indicated by either the internal or external DTD subsets. If the initial JDOM |

Processing XML with Javaв„ў: A Guide to SAX, DOM, JDOM, JAXP, and TrAX

ISBN: 0201771861

EAN: 2147483647

EAN: 2147483647

Year: 2001

Pages: 191

Pages: 191

Authors: Elliotte Rusty Harold

Similar book on Amazon

- Chapter I e-Search: A Conceptual Framework of Online Consumer Behavior
- Chapter III Two Models of Online Patronage: Why Do Consumers Shop on the Internet?
- Chapter V Consumer Complaint Behavior in the Online Environment
- Chapter VI Web Site Quality and Usability in E-Commerce
- Chapter XVII Internet Markets and E-Loyalty

flylib.com © 2008-2017.

If you may any questions please contact us: flylib@qtcs.net

If you may any questions please contact us: flylib@qtcs.net