/* * (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 javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; /** * A sample DOM application. This program illustrates how to * create a DOM tree without an XML source. */ public class DomBuilder { /** Prints the specified node, recursively. */ public void printDomTree(Node node) { int type = node.getNodeType(); switch (type) { // print the document element case Node.DOCUMENT_NODE: { System.out.println(""); printDomTree(((Document)node).getDocumentElement()); break; } // print element and any attributes case Node.ELEMENT_NODE: { System.out.print("<"); System.out.print(node.getNodeName()); if (node.hasAttributes()) { NamedNodeMap attrs = node.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) printDomTree(attrs.item(i)); } System.out.print(">"); if (node.hasChildNodes()) { NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) printDomTree(children.item(i)); } break; } // Print attribute nodes case Node.ATTRIBUTE_NODE: { System.out.print(" " + node.getNodeName() + "=\""); if (node.hasChildNodes()) { NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) printDomTree(children.item(i)); } System.out.print("\""); break; } // handle entity reference nodes case Node.ENTITY_REFERENCE_NODE: { System.out.print("&"); System.out.print(node.getNodeName()); System.out.print(";"); break; } // print cdata sections case Node.CDATA_SECTION_NODE: { System.out.print(""); break; } // print text case Node.TEXT_NODE: { System.out.print(node.getNodeValue()); break; } // print comment case Node.COMMENT_NODE: { System.out.print(""); break; } // print processing instruction case Node.PROCESSING_INSTRUCTION_NODE: { System.out.print(""); break; } } if (type == Node.ELEMENT_NODE) { System.out.print("'); } } // printDomTree(Node) /** Main program entry point. */ public static void main(String argv[]) { if (argv.length == 1 && argv[0].equals("-help")) { System.out.println("\nUsage: java DomBuilder"); System.out.println("\nBuilds a DOM tree with DOM method calls, " + "then prints it."); System.exit(1); } try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = dbf.newDocumentBuilder(); Document doc = docBuilder.getDOMImplementation(). createDocument("", "sonnet", null); Element root = doc.getDocumentElement(); root.setAttribute("type", "Shakespearean"); Element author = doc.createElement("author"); Element lastName = doc.createElement("last-name"); lastName.appendChild(doc.createTextNode("Shakespeare")); author.appendChild(lastName); Element firstName = doc.createElement("first-name"); firstName.appendChild(doc.createTextNode("William")); author.appendChild(firstName); Element nationality = doc.createElement("nationality"); nationality.appendChild(doc.createTextNode("British")); author.appendChild(nationality); Element yearOfBirth = doc.createElement("year-of-birth"); yearOfBirth.appendChild(doc.createTextNode("1564")); author.appendChild(yearOfBirth); Element yearOfDeath = doc.createElement("year-of-death"); yearOfDeath.appendChild(doc.createTextNode("1616")); author.appendChild(yearOfDeath); root.appendChild(author); Element title = doc.createElement("title"); title.appendChild(doc.createTextNode("Sonnet 130")); root.appendChild(title); Element lines = doc.createElement("lines"); Element line01 = doc.createElement("line"); line01.appendChild(doc.createTextNode ("My mistress' eyes are nothing like the sun,")); lines.appendChild(line01); Element line02 = doc.createElement("line"); line02.appendChild(doc.createTextNode ("Coral is far more red than her lips red.")); lines.appendChild(line02); Element line03 = doc.createElement("line"); line03.appendChild(doc.createTextNode ("If snow be white, why then her breasts are dun,")); lines.appendChild(line03); Element line04 = doc.createElement("line"); line04.appendChild(doc.createTextNode ("If hairs be wires, black wires grow on her head.")); lines.appendChild(line04); Element line05 = doc.createElement("line"); line05.appendChild(doc.createTextNode ("I have seen roses damasked, red and white,")); lines.appendChild(line05); Element line06 = doc.createElement("line"); line06.appendChild(doc.createTextNode ("But no such roses see I in her cheeks.")); lines.appendChild(line06); Element line07 = doc.createElement("line"); line07.appendChild(doc.createTextNode ("And in some perfumes is there more delight")); lines.appendChild(line07); Element line08 = doc.createElement("line"); line08.appendChild(doc.createTextNode ("Than in the breath that from my mistress reeks.")); lines.appendChild(line08); Element line09 = doc.createElement("line"); line09.appendChild(doc.createTextNode ("I love to hear her speak, yet well I know")); lines.appendChild(line09); Element line10 = doc.createElement("line"); line10.appendChild(doc.createTextNode ("That music hath a far more pleasing sound.")); lines.appendChild(line10); Element line11 = doc.createElement("line"); line11.appendChild(doc.createTextNode ("I grant I never saw a goddess go,")); lines.appendChild(line11); Element line12 = doc.createElement("line"); line12.appendChild(doc.createTextNode ("My mistress when she walks, treads on the ground.")); lines.appendChild(line12); Element line13 = doc.createElement("line"); line13.appendChild(doc.createTextNode ("And yet, by Heaven, I think my love as rare")); lines.appendChild(line13); Element line14 = doc.createElement("line"); line14.appendChild(doc.createTextNode ("As any she belied with false compare.")); lines.appendChild(line14); root.appendChild(lines); DomBuilder db = new DomBuilder(); db.printDomTree(doc); } catch (Exception e) { System.err.println(e); } } }