<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Here comes the Sun &#187; xml</title>
	<atom:link href="http://sunng.info/blog/tag/xml/feed/" rel="self" type="application/rss+xml" />
	<link>http://sunng.info/blog</link>
	<description>47% users on this site use *nix</description>
	<lastBuildDate>Sat, 04 Feb 2012 13:08:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Suppressing simple-xml&#039;s class attribute</title>
		<link>http://sunng.info/blog/2010/02/suppressing-simple-xmls-class-attribute/</link>
		<comments>http://sunng.info/blog/2010/02/suppressing-simple-xmls-class-attribute/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 07:07:12 +0000</pubDate>
		<dc:creator>sunng</dc:creator>
				<category><![CDATA[手艺]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://sunng.info/blog/?p=452</guid>
		<description><![CDATA[Simple-xml is an object-xml serialization and de-serialization framework. It&#8217;s featured by annotation-driven, light-weight and self-contained. In simple-xml, pojos that will be serialized are annotated with @Root, @Element, @Attribute or @Text. The problem is, when handling with inheritance, if the actual &#8230; <a href="http://sunng.info/blog/2010/02/suppressing-simple-xmls-class-attribute/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://simple.sourceforge.net" target="_blank">Simple-xml</a> is an object-xml serialization and de-serialization framework. It&#8217;s featured by annotation-driven, light-weight and self-contained.</p>
<p>In simple-xml, pojos that will be serialized are annotated with @Root, @Element, @Attribute or @Text. The problem is, when handling with inheritance, if the actual type is different from declared type, simple-xml will add a &#8220;class&#8221; attribute for de-serialization consideration. That is, the parser will use this attribute to determine the schema of the xml element. However, this is of no useful when we do not parse xml by simple-xml. And there is also risk that the class attribute exposes our program&#8217;s internal structure.</p>
<p>Take following code as example:</p>
<pre class="brush:java">@Root
public class Response{
    @Element
    private Object entry;

    // getter and setter ...
}

public class OrderItem{
    @Element
    private String name;

    // getter and setter ...
}</pre>
<p>When you generate xml from classes above, you will have the entry element of response with an attribute &#8220;class&#8221;, which value is the qualified class name, like &#8220;package.OrderItem&#8221;.</p>
<p>This is done by simple-xml&#8217;s Strategy interface. By default, the Persister uses TreeStrategy which has an implementation of setElement like:</p>
<pre class="brush:java">/**
 * This is used to attach a attribute to the provided element
 * that is used to identify the class. The attribute name is
 * "class" and has the value of the fully qualified class
 * name for the object provided. This will only be invoked
 * if the object class is different from the field class.
 *
 * @param type this is the declared class for the field used
 * @param value this is the instance variable being serialized
 * @param node this is the element used to represent the value
 * @param map this is used to maintain contextual information
 *
 * @return this returns true if serialization is complete
 */
public boolean setElement(Type type, Object value, NodeMap node, Map map){
    Class actual = value.getClass();
    Class expect = type.getType();
    Class real = actual;
    if(actual.isArray()) {
        real = setArray(expect, value, node);
    }
    if(actual != expect) {
        node.put(label, real.getName());
    }
    return false;
}</pre>
<p>This is where class attributed. So to suppress the attribute, we simply override this method by inherit TreeStrategy. I take a inline class for convenience here.</p>
<pre class="brush:java">Serializer s = new Persister(new TreeStrategy(){
    @Override
    public boolean setElement(Type type, Object value, NodeMap node, Map map){
        return false;
    }
});</pre>
<p>Now it works, however, simple-xml won&#8217;t be able to deserialize xml generated by this modified strategy.</p>
]]></content:encoded>
			<wfw:commentRss>http://sunng.info/blog/2010/02/suppressing-simple-xmls-class-attribute/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

