1 57 58 package dom.serialize; 59 import java.io.FileOutputStream ; 60 import java.io.ObjectOutputStream ; 61 62 import org.enhydra.apache.xerces.dom.DocumentImpl; 63 import org.enhydra.apache.xerces.parsers.DOMParser; 64 import org.w3c.dom.Document ; 65 66 import dom.DOMWriter; 67 68 69 79 80 81 public class TestSerializeDOMOut 82 { 83 84 public TestSerializeDOMOut(){ 85 } 86 87 93 public void serializeDOM( Document doc, String nameSerializedFile ){ 94 try { 95 ObjectOutputStream out = 96 new ObjectOutputStream ( new FileOutputStream ( nameSerializedFile ) ); 97 out.writeObject(doc); 98 out.close(); 99 100 } catch ( Exception ex ) { 101 ex.printStackTrace(); 102 } 103 } 104 105 106 public static void main (String [] argv) 107 { 108 109 if ( argv.length != 1 ) { 110 System.out.println("Error - Usage: java TestOut yourFile.xml" ); 111 System.exit(1); 112 } 113 114 String xmlFilename = argv[0]; 115 116 117 try { 118 DOMParser parser = new DOMParser(); 119 120 parser.parse( xmlFilename ); 121 122 DocumentImpl doc = (DocumentImpl) parser.getDocument(); 123 124 int indexOfextension = xmlFilename.indexOf("." ); 125 126 127 128 String nameOfSerializedFile = null; 129 130 if ( indexOfextension == -1 ) { 131 nameOfSerializedFile = xmlFilename +".ser" ; 132 } else { 133 nameOfSerializedFile = 134 xmlFilename.substring(0,indexOfextension) + ".ser"; 135 } 136 137 System.out.println( "Writing Serialize DOM to file = " + nameOfSerializedFile ); 138 139 140 FileOutputStream fileOut = new FileOutputStream ( nameOfSerializedFile ); 141 142 143 TestSerializeDOMOut tstOut = new TestSerializeDOMOut(); 144 145 tstOut.serializeDOM( doc, nameOfSerializedFile ); 146 147 148 System.out.println( "Reading Serialize DOM from " + nameOfSerializedFile ); 149 150 151 TestSerializeDOMIn tstIn = new TestSerializeDOMIn(); 152 doc = tstIn.deserializeDOM( nameOfSerializedFile ); 153 154 DOMWriter prettyWriter = new DOMWriter( false ); 155 System.out.println( "Here is the whole Document" ); 156 prettyWriter.print( doc.getDocumentElement() ); 157 } catch ( Exception ex ){ 158 ex.printStackTrace(); 159 } 160 } 161 } 162 163 | Popular Tags |