1 57 58 package dom.serialize; 59 import java.io.FileInputStream ; 60 import java.io.ObjectInputStream ; 61 62 import org.enhydra.apache.xerces.dom.DocumentImpl; 63 import org.enhydra.apache.xerces.dom.NodeImpl; 64 import org.w3c.dom.Node ; 65 import org.w3c.dom.NodeList ; 66 67 import dom.DOMWriter; 68 69 70 80 public class TestSerializeDOMIn { 81 82 public TestSerializeDOMIn() { 83 } 84 85 86 92 public DocumentImpl deserializeDOM( String nameSerializedFile ){ 93 ObjectInputStream in = null; 94 DocumentImpl doc = null; 95 try { 96 97 FileInputStream fileIn = new FileInputStream ( nameSerializedFile ); 98 in = new ObjectInputStream (fileIn); 99 doc = (DocumentImpl) in.readObject(); } catch ( Exception ex ) { 101 ex.printStackTrace(); 102 } 103 return doc; 104 } 105 106 107 public static void main( String argv[] ){ 108 if ( argv.length != 2 ) { 109 System.out.println("Error - Usage: java TestSerializeDOMIn yourFile.ser elementName" ); 110 System.exit(1); 111 } 112 113 String xmlFilename = argv[0]; 114 115 TestSerializeDOMIn tst = new TestSerializeDOMIn(); 116 DocumentImpl doc = tst.deserializeDOM( xmlFilename ); 117 118 NodeList nl = doc.getElementsByTagName( argv[1]); 119 120 121 int length = nl.getLength(); 122 123 if ( length == 0 ) 124 System.out.println(argv[1] + ": is not in the document!"); 125 126 NodeImpl node = null; 127 for ( int i = 0;i<length;i++ ){ 128 node = (NodeImpl) nl.item(i); 129 Node childOfElement = node.getFirstChild(); 130 if ( childOfElement != null ){ 131 System.out.println( node.getNodeName() + ": " + 132 childOfElement.getNodeValue() ); 133 } 134 } 135 try { 136 DOMWriter prettyWriter = new DOMWriter( false ); 137 System.out.println( "Here is the whole Document" ); 138 prettyWriter.print( doc.getDocumentElement() ); 139 } catch( Exception ex ){ 140 } 141 } 142 } 143 | Popular Tags |