GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing x-stream.github.io — XStream - Object Streams Tutorial Object Streams TutorialXStream provides alternative implementations of This is useful when processing large sets of objects, as only one needs to be in memory at a time. Obviously you should use also a stream-based XML parser reading the XML. A DOM-based XML parser will process the complete XML and build the object model before XStream is able to to handle the first element. Using the Object StreamsThe interface to the object streaming capabilities of XStream is through the standard
ExampleTo serialize a stream of objects to XML: ObjectOutputStream out = xstream.createObjectOutputStream(someWriter);
out.writeObject(new Person("Joe", "Walnes"));
out.writeObject(new Person("Someone", "Else"));
out.writeObject("hello");
out.writeInt(12345);
out.close();The resulting XML: <object-stream>
<com.blah.Person>
<firstname>Joe</firstname>
<lastname>Walnes</lastname>
</com.blah.Person>
<com.blah.Person>
<firstname>Someone</firstname>
<lastname>Else</lastname>
</com.blah.Person>
<string>hello</string>
<int>123</int>
</object-stream>To deserialze the stream of objects from the XML: ObjectInputStream in = xstream.createObjectInputStream(someReader); Person a = (Person)in.readObject(); Person b = (Person)in.readObject(); String c = (String)in.readObject(); int d = in.readInt(); ConsiderationsRoot nodeBecause an XML document can only have a single root node, all the serialized elements must be wrapped in an
additional root node. This root node defaults to This can be changed by using the overloaded method:
Close the ObjectOutputStreamRemember to call Detecting the end of the ObjectInputStreamWhen there are no more objects left to read in the stream, ReferencesNormally XStream will not know about references between the different objects that are written individually in the ObjectStream. Nevertheless there is an example in the acceptance tests (MultipleObjectsInOneStreamTest) where such a functionality is realized with the help of a custom MarshallingStrategy. Note, that this implementation is not for general use, since it will ignore some parameters at second invocation, but they do not matter in the demonstrated use case. Additionally those references prevent the objects from being garbage collected, which is a bit counter-productive for the use case of ObjectStreams as described above. So you have to know what you do! Evaluating XStreamUsing XStreamTutorials
Developing XStream |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |