Note that there are some explanatory texts on larger screens.

plurals
  1. POIntercepting Xstream while parsing XML
    primarykey
    data
    text
    <p>Suppose I have a simple Java class like this:</p> <pre><code>public class User { String firstName; String lastName; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } } </code></pre> <p>Now, suppose I want to parse the following XML:</p> <pre><code>&lt;user&gt; &lt;firstName&gt;Homer&lt;/firstName&gt; &lt;lastName&gt;Simpson&lt;/lastName&gt; &lt;/user&gt; </code></pre> <p>I can do this with no problems in XStream like so:</p> <pre><code>User homer = (User) xstream.fromXML(xml); </code></pre> <p>Ok, all good so far, but here's my problem.</p> <p>Suppose I have the following XML that I want to parse:</p> <pre><code>&lt;user&gt; &lt;fullName&gt;Homer Simpson&lt;/fullName&gt; &lt;/user&gt; </code></pre> <p>How can I convert this XML into the <strong>same</strong> User object using XStream? </p> <p>I'd like a way to implement some kind of callback so that when XStream parses the fullName field, I can split the string in two and manually set the first name and last name fields on the user object. Is this possible?</p> <p>Note that I'm not asking how to split the string in two (that's the easy part), I want to know how to intercept the XML parsing so XStream doesn't try to reflectively set the fullName field on the User object (which obviously doesn't exist).</p> <p>I looked at the converters that XStream provides but couldn't figure out how to use it for this purpose.</p> <p>Any help would be appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload