Hi Ruben, Ruben Fragoso wrote:
> Hi Jorg > > thank you for comming back to me. I believe i miss lead you. my problem in > not in the test, the problem that am talking about is related to the fact > that the i cannot get the members of the Custom object to be populated, > the problem is inside the Unmarshal function, if i look at the example of > the Tutorial > > http://xstream.codehaus.org/converter-tutorial.html > > I dont seem to get it right, i have tried several things, some of them i > get an exception about missing class members, sometimes it gives the > exception about "only the Start_tag can have attributes" Remember that XStream is stream based. In your test you generate following XML: ================ %< ============== <MyCustomObject> <MyCustomObject widht="45" type="TypeThree" name="Three" diameter="78"/> <MyCustomObject widht="23" type="TypeOne" name="One" heigh="23"/> <MyCustomObject widht="23" type="TypeTwo" name="Two" speed="45"/> </MyCustomObject> ================ %< ============== I'll use now the pipe character to demonstrate, where the pointer in the stream is located, when your converter is called and how it advances (and make the empty element explicit for better demonstration): ================ %< ============== <MyCustomObject|> <MyCustomObject widht="45" type="TypeThree" name="Three" diameter="78"></MyCustomObject> <MyCustomObject widht="23" type="TypeOne" name="One" heigh="23"></MyCustomObject> ================ %< ============== Notice, that the pointer is in the parent tag. ================ %< ============== if ("MyCustomObject".equals(reader.getNodeName())) { ================ %< ============== You request the name of the parent element, but the pointer does not advance. Since your converter cannot control this name, it is normally "useless" to rely on its name. ================ %< ============== while (reader.hasMoreChildren()) { ================ %< ============== OK, you seem not to be interested in any attributes of the parent element (actually it does not have one anyway), so XStream will advance to the first child element: ================ %< ============== <MyCustomObject> <|MyCustomObject widht="45" type="TypeThree" name="Three" diameter="78"></MyCustomObject> <MyCustomObject widht="23" type="TypeOne" name="One" heigh="23"></MyCustomObject> ================ %< ============== MyBaseClass.Type type = MyBaseClass.Type.valueOf(reader .getAttribute("type")); ================ %< ============== OK, you're interested in the attributes of the current tag. However, you did not move into the XML tag, so you're lucky to read the attribute at this point, the behavior is more or less undefined. With the following command you process the XML tag itself: ================ %< ============== reader.moveDown(); ================ %< ============== Now it would have been the time to look for the attributes. ================ %< ============== <MyCustomObject> <MyCustomObject widht="45" type="TypeThree" name="Three" diameter="78"| ></MyCustomObject> <MyCustomObject widht="23" type="TypeOne" name="One" heigh="23"></MyCustomObject> ================ %< ============== The next call looks for the XML element's value and will therefore advance the pointer into the tag: ================ %< ============== if (!reader.getValue().isEmpty()) { ================ %< ============== <MyCustomObject> <MyCustomObject widht="45" type="TypeThree" name="Three" diameter="78">| </MyCustomObject> <MyCustomObject widht="23" type="TypeOne" name="One" heigh="23"></MyCustomObject> ================ %< ============== OK, the XML element has no value, so condition is not met. The next call will process the closing tag: ================ %< ============== reader.moveUp(); ================ %< ============== <MyCustomObject> <MyCustomObject widht="45" type="TypeThree" name="Three" diameter="78"></MyCustomObject>| <MyCustomObject widht="23" type="TypeOne" name="One" heigh="23"></MyCustomObject> ================ %< ============== We hit the loop condition and XStream will advance to the next child: ================ %< ============== while (reader.hasMoreChildren()) { ================ %< ============== <MyCustomObject> <MyCustomObject widht="45" type="TypeThree" name="Three" diameter="78"></MyCustomObject> <|MyCustomObject widht="23" type="TypeOne" name="One" heigh="23"></MyCustomObject> ================ %< ============== >From here we can continue above again. Since you did nothing with the created MyCustomObject instance, you return an empty instance. And since you do not use the return value of XStream's fromXML method, even this instance is ignored. - Jörg --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email
