XStream Interview Questions & Answers

  1. Question 1. Why Does Xstream Not Respect The Encoding In The Xml Declaration?

    Answer :

    XStream architecture is based on IO Readers and Writers, while the XML declaration is the responsibility of XML parsers. All HierarchicalStreamDriver implementations respect the encoding since version 1.3, but only if you provide an InputStream. If XStream consumes a Reader you have to initialize the reader with the appropriate encoding yourself, since it is now the reader’s task to perform the encoding and no XML parser can change the encoding of a Reader and any encoding definition in the XML header will be ignored.

  2. Question 2. Why Does Xstream Not Write An Xml Declaration?

    Answer :

    XStream is designed to write XML snippets, so you can embed its output into an existing stream or string. You can write the XML declaration yourself into the Writer before using it to call XStream.toXML(writer).

  3. File Maker Interview Questions

  4. Question 3. Why Does Xstream Not Write Xml In Utf-8?

    Answer :

    XStream does no character encoding by itself, it relies on the configuration of the underlying XML writer. By default it uses its own PrettyPrintWriter which writes into the default encoding of the current locale. To write UTF-8 you have to provide a Writer with the appropriate encoding yourself.

  5. Question 4. Why Do Field Names Suddenly Have Double Underscores In The Generated Xml?

    Answer :

    XStream maps Java class names and field names to XML tags or attributes. Unfortunately this mapping cannot be 1:1, since some characters used for identifiers in Java are invalid in XML names. Therefore XStream uses an XmlFriendlyNameCoder to replace these characters with a replacement. By default this NameCoder uses an underscore as escape character and has therefore to escape the underscore itself also. You may provide a different configured instance of the XmlFriendlyNameCoder or a complete different implementation like the NoNameCoder to prevent name coding at all. However it is your responsibility then to ensure, that the resulting names are valid for XML.

  6. File Maker Tutorial

  7. Question 5. Xstream Failed To Unmarshal My Given Xml And I Do Not Know Why?

    Answer :

    By default XStream is written for persistence i.e. it will read the XML it can write. If you have to transform a given XML into an object graph, you should go the other way round. Use XStream to transfer your objects into XML. If the written XML matches your schema, XStream is also able to read it. This way is much easier, since you can spot the differences in the XML much more easy than to interpret the exceptions XStream will throw if it cannot match the XML into your objects.

  8. J2EE Interview Questions

  9. Question 6. Why Is My Element Not Written As Xml Attribute Although I Have Configured It?

    Answer :

    You can only write types as attributes that are represented as a single String value and are handled therefore by SingleValueConverter implementations. If your type is handled by a Converter implementation, the configuration of XStream to write an attribute (using XStream.useAttributeFor() or @XStreamAsAttribute) is simply ignored.

  10. Question 7. Why Are Whitespace Characters Wrong In My Attribute Values After Deserialization?

    Answer :

    This is part of the XML specification and a required functionality for any XML parser called attribute value normalization. It cannot be influenced by XStream. A compliant XML parser will replace by default real tab, carriage return and line feed characters with normal spaces. If you want to keep these characters you will have to encode them with entities.

  11. J2EE Tutorial
    Core Java Interview Questions

  12. Question 8. Why Does Xstream Not Have Any Namespace Support?

    Answer :

    Not every XML parser supports namespaces and not every XML parser that supports namespaces can be configured within XStream to use those. Basically namespaces must be supported individually for the different XML parsers and the only support for namespaces that has currently been implemented in XStream is for the StAX paser. Therefore use and configure the StaxDriver of XStream to use namespaces.

  13. Question 9. My Xml Contains Xpath Expressions In The References, But They Seem Not To Work?

    Answer :

    XStream generates only XPath compliant expressions. These have a very limited syntax and they are the only ones that can be interpreted at deserialization again, since XStream does not use an XPath interpreter. Therefore there is no support for attribute selectors, qualified element access with axis names or functions. For real XPath support you will have to implement your own MarshallingStrategy.

  14. Android Interview Questions

  15. Question 10. Does Xstream Support Entities?

    Answer :

    Entity support is completely dependent on the XML parser. XStream uses by default the Xpp3 parser that does not support entities at all (like the kXML2 parser). Other parsers support entities, but they might have been turned off to avoid XXE vulnerability. To enable the entities again, you have to overload the individual method of the HierarchicalStreamDriver implementation that generated the parser factory.

  16. Core Java Tutorial

  17. Question 11. Why Are There Two Json Driver Implementations?

    Answer :

    As always, first for historical reasons! Main difference is that the JettisonMappedXmlDriver is a thin wrapper around Jettison in combination with the StaxDriver, while the JsonHierarchicalStreamDriver uses an own more flexible implementation, but can only be used to generate JSON, deserialization is not implemented.

  18. JavaServer Faces (JSF) Interview Questions

  19. Question 12. Which Versions Of Jettison Are Supported?

    Answer :

    Users of Java 5 or higher can use Jettison 1.2, users of Java 1.4.2 have to use Jettison 1.0.1. Jettison 1.1 nor Jettison 1.3 or higher is supported.

  20. File Maker Interview Questions

  21. Question 13. Why Is It Not Possible To Deserialize A Json String Starting With An Array?

    Answer :

    XStream’s implementation to deserialize JSON is based on Jettison and StAX. Jettison implements a XMLStreamReader of StaX and transforms the processed JSON virtually into XML first. However, if the JSON string starts with an array it is not possible for Jettison to create a valid root element, since it has no name.

  22. Android Tutorial

  23. Question 14. Xstream Fails To Unmarshal My Json String And I Do Not Know Why?

    Answer :

    Deserialization of JSON is currently done by Jettison, that transforms the JSON string into a StAX stream. XStream itself does nothing know about the JSON format here. If your JSON string reaches some kind of complexity and you do not know how to design your Java objects and configure XStream to match those, you should have a look at the intermediate XML that is processed by XStream in the end. This might help to identify the problematic spots. Also consider then marshalling your Java objects into XML first. You can use following code to generate the XML:

    String json = “{“string”: “foo”}”;
    HierarchicalStreamDriver driver = new JettisonMappedXmlDriver();
    StringReader reader = new StringReader(json);
    HierarchicalStreamReader hsr = driver.createReader(reader);
    StringWriter writer = new StringWriter();
    new HierarchicalStreamCopier().copy(hsr, new PrettyPrintWriter(writer));
    writer.close();
    System.out.println(writer.toString());

  24. Question 15. What Limitations Has Xstream’s Json Support?

    Answer :

    JSON represents a very simple data model for easy data transfer. Especially it has no equivalent for XML attributes. Those are written with a leading “@” character, but this is not always possible without violating the syntax (e.g. for array types). Those may silently dropped (and makes it therefore difficult to implement deserialization). References are another issue in the serialized object graph, since JSON has no possibility to express such a construct. You should therefore always set the NO_REFERENCES mode of XStream. Additionally you cannot use implicit collections, since the properties in a JSON object must have unique names.

  25. Java Swing Interview Questions

  26. Question 16. Why Are My Long Values Incorrect In Javascript?

    Answer :

    JavaScript does not know about integer values. All numbers are represented with double precition floats using 64 bits (IEEE 754). These types cannot represent technically the complete value range of 64-bit integers like Java’s Long. With the JsonWriter you have the possibility since XStream 1.4.5 to set IEEE_754_MODE to force any long value that is not representable as JavaScript number to be written as string value in JSON. With the Jettison-based JettisonMappedXmlDriver you may either set a different TypeConverter or force the default converter to write integer values out of the range of 32-bit always as string setting the system property jettison.mapped.typeconverter.enforce_32bit_integer to true (not available for Jettison 1.0.1 and Java 1.4).

  27. JavaServer Faces (JSF) Tutorial

  28. Question 17. Why Are There Invalid Characters In My Json Representation?

    Answer :

    The JSON spec requires any JSON string to be in UTF-8 encoding. However, XStream ensures this only if you provide an InputStream or an OutputStream. If you provide a Reader or Writer you have to ensure this requirement on your own.

  29. Javascript Objects Interview Questions

  30. Question 18. Why Does Xstream Not Convert An Java.beans.eventhandler?

    Answer :

    Since XStream version 1.4.7 it does no longer handle an EventHandler automatically. Such an instance can be used to initiate calls on arbitrary instances at deserialization time e.g. ProcessBuilder.start() You can register a ReflectionConverter instance explicitly for the EventHandler if you need support for such instances.

  31. J2EE Interview Questions

  32. Question 19. Is Xstream Xxe Vulnerable (cve-2016-3674)?

    Answer :

    XStream does not contain an own XML parser, therefore it depends on the parser selected with the HierarchicalStreamDriver if the current XStream instance is XXE vulnerable at deserialization time. However, XStream tries to deactivate the processing of external entities by default. Status for the different supported XML parsers.

  33. Java Swing Tutorial

  34. Question 20. How Does Xstream Compare To Java.beans.xmlencoder?

    Answer :

    XStream is designed for serializing objects using internal fields, whereas XMLEncoder is designed for serializing JavaBeans using public API methods (typically in the form of getXXX(), setXXX(), addXXX() and removeXXX() methods.

  35. Javascript Advanced Interview Questions

  36. Question 21. How Does Xstream Compare To Jaxb (java Api For Xml Binding)?

    Answer :

    JAXB is a Java binding tool. It generates Java code from a schema and you are able to transform from those classes into XML matching the processed schema and back. Note, that you cannot use your own objects, you have to use what is generated.

  37. Question 22. Is Xstream Thread Safe?

    Answer :

    Yes. Once the XStream instance has been created and configured, it may be shared across multiple threads allowing objects to be serialized/deserialized concurrently (unless you enable the auto-detection to process annotations on-the-fly). Actually the creation and initialization of XStream is quite expensive, therefore it is recommended to keep the XStream instance itself. If you absolutely have to rely on annotation processing on the fly, you will have to use separate XStream instances for each thread – either by using every time a new instance or by a shared pool.

  38. Javascript Objects Tutorial

  39. Question 23. How Much Memory Does Xstream Consume?

    Answer :

    This cannot be answered in general, but following topics have impact on the memory:

    1. XML parser technology in use: You should use a streaming parser like Xpp3 or StAX. DOM-based parsers process the complete XML and create their document model in memory before the first converter of XStream is called.
    2. Your object model: Is it necessary to keep the complete object graph in memory at once? As alternative you might use object streams or write custom converters that can load and save objects of your object model on the fly without adding them to the object graph physically. As example see the implementation of the XmlArrayList in combination with the FileStreamStrategy from XStream’s persistence package to keep parts of the object graph separate.
    3. References: By default XStream supports references to the same object in an object graph. This implies that XStream keeps track of all serialized and deserialized objects internally. These references are kept with WeakReferences, so that the memory can be freed as soon as nobody references these objects anymore.
    4. XML values: Any tag and attribute value that is converted into a Java String in the object graph will use by default the same String instance unless it exceeds 38 characters (length of a UUID string representation).
    5. XStream caches: To increase performance XStream caches quite a lot like classes, converters to use, aliasing, tag names. All those caches make usage of WeakReferences or will exist only while marshalling one object graph resp. unmarshalling one input stream.
  40. JavaMail API Interview Questions

  41. Question 24. Can The Performance Of Xstream Be Increased?

    Answer :

    XStream is a generalizing library, it inspects and handles your types on the fly. Therefore it will normally be slower than a piece of optimized Java code generated out of a schema. However, it is possible to increase the performance anyway:

    • Write custom converters for those of your types that occur very often in your XML.
    • Keep a configured XStream instance for multiple usage. Creation and initialization is quite expensive compared to the overhead of XStream when calling marshall or unmarshal.
    • Use Xpp3 or StAX parsers.

    Note, you should never try to optimize code for performance simply because you believe that you have detected a bottle neck. Always use proper tools like a profiler to verify where your hotspots are and whether your optimization was really successful or not.

  42. Core Java Interview Questions

  43. Question 25. Is Xstream A Data Binding Tool?

    Answer :

    No. It is a serialization tool.

  44. Javascript Advanced Tutorial

  45. Question 26. Can Xstream Generate Classes From Xsd?

    Answer :

    No. For this kind of work data binding tools such as JAXB or XMLBeans are appropriate.

  46. Java collections framework Interview Questions

  47. Question 27. Why Is There No Saxreader?

    Answer :

    XStream works on a stream-based parser model, while SAX is event-based. The stream based model implies, that the caller consumes the individual tokens from the XML parser on demand, while in an event-based model the parser controls the application flow on its own and will use callbacks to support client processing. The different architecture makes it therefore impossible for XStream to use an event-driven XML parser.

  48. Android Interview Questions