/* * (C) Copyright IBM Corp. 2004. All rights reserved. * * US Government Users Restricted Rights Use, duplication or * disclosure restricted by GSA ADP Schedule Contract with IBM Corp. * * The program is provided "as is" without any warranty express or * implied, including the warranty of non-infringement and the implied * warranties of merchantibility and fitness for a particular purpose. * IBM will not be liable for any damages suffered by you as a result * of using the Program. In no event will IBM be liable for any * special, indirect or consequential damages or lost profits even if * IBM has been advised of the possibility of their occurrence. IBM * will not be liable for any third party claims against you. */ import java.io.IOException; import java.util.Vector; import org.jdom.DocType; import org.jdom.Document; import org.jdom.Element; import org.jdom.output.XMLOutputter; public class JdomBuilder { public static void main(String[] argv) { if (argv.length == 1 && argv[0].equals("-help")) { System.out.println("\nUsage: java JdomBuilder"); System.out.println("\nUses the JDOM API to create a Document " + "object without an XML source file."); System.exit(1); } JdomBuilder jb = new JdomBuilder(); jb.buildDocument(); } public void buildDocument() { Element root = new Element("sonnet"); root.setAttribute("type", "Shakespearean"); Vector author = new Vector(); author.add(new Element("last-name").addContent("Shakespeare")); author.add(new Element("first-name").addContent("William")); author.add(new Element("nationality").addContent("British")); author.add(new Element("year-of-birth").addContent("1564")); author.add(new Element("year-of-death").addContent("1616")); root.addContent(new Element("author").setContent(author)); root.addContent(new Element("title").addContent("Sonnet 130")); Vector lines = new Vector(); lines.add(new Element("line"). addContent("My mistress' eyes are nothing like the sun,")); lines.add(new Element("line"). addContent("Coral is far more red than her lips red.")); lines.add(new Element("line"). addContent("If snow be white, why then her breasts are dun,")); lines.add(new Element("line"). addContent("If hairs be wires, black wires grow on her head.")); lines.add(new Element("line"). addContent("I have seen roses damasked, red and white,")); lines.add(new Element("line"). addContent("But no such roses see I in her cheeks.")); lines.add(new Element("line"). addContent("And in some perfumes is there more delight")); lines.add(new Element("line"). addContent("Than in the breath that from my mistress reeks.")); lines.add(new Element("line"). addContent("I love to hear her speak, yet well I know")); lines.add(new Element("line"). addContent("That music hath a far more pleasing sound.")); lines.add(new Element("line"). addContent("I grant I never saw a goddess go,")); lines.add(new Element("line"). addContent("My mistress when she walks, treads on the ground.")); lines.add(new Element("line"). addContent("And yet, by Heaven, I think my love as rare")); lines.add(new Element("line"). addContent("As any she belied with false compare.")); root.addContent(new Element("lines").setContent(lines)); Document doc = new Document(root, new DocType("sonnet", "sonnet.dtd")); try { XMLOutputter xo = new XMLOutputter(" ", true); xo.output(doc, System.out); } catch (IOException ioe) { System.err.println("IO Exception: " + ioe); } } }