关于jaxb转换为xml的问题,高分求帮助
展开全部
既然你不想定义result这个类,那你service中的属性都可以不用定义了,直接使用XmlJavaTypeAdapter这样大的注解去marshal、unmarshal对象xml就行了。一般使用jaxb,如果属性为某个类库中定义的类,而需要转换为xml时,通过XmlJavaTypeAdapter来进行特殊处理。如插件开发中,元素属性为:org.eclipse.draw2d.geometry.Rectangle(矩形)时,通过XmlJavaTypeAdapter转换,如下所示:
import javax.xml.bind.annotation.adapters.XmlAdapter;
import org.eclipse.draw2d.geometry.Rectangle;
public class LayoutXmlAdapter extends XmlAdapter<String, Rectangle{@Overridepublic String marshal(Rectangle v) throws Exception{if(v == null) return "";
return v.x + "," + v.y + "," + v.width + "," + v.height;}@Overridepublic Rectangle unmarshal(String v) throws Exception{if(v != null && v.trim().length() 0){String[] ps = v.split(",");
if(ps.length ==4){int x = Integer.parseInt(ps[0]);
int y = Integer.parseInt(ps[1]);
int width = Integer.parseInt(ps[2]);
int height = Integer.parseInt(ps[3]);
return new Rectangle(x, y, width, height);}}return new Rectangle(0, 0, 0, 0);}}如果你非要不定义result类,直接转换为:那你可以使用XmlJavaTypeAdapter注解。
import javax.xml.bind.annotation.adapters.XmlAdapter;
import org.eclipse.draw2d.geometry.Rectangle;
public class LayoutXmlAdapter extends XmlAdapter<String, Rectangle{@Overridepublic String marshal(Rectangle v) throws Exception{if(v == null) return "";
return v.x + "," + v.y + "," + v.width + "," + v.height;}@Overridepublic Rectangle unmarshal(String v) throws Exception{if(v != null && v.trim().length() 0){String[] ps = v.split(",");
if(ps.length ==4){int x = Integer.parseInt(ps[0]);
int y = Integer.parseInt(ps[1]);
int width = Integer.parseInt(ps[2]);
int height = Integer.parseInt(ps[3]);
return new Rectangle(x, y, width, height);}}return new Rectangle(0, 0, 0, 0);}}如果你非要不定义result类,直接转换为:那你可以使用XmlJavaTypeAdapter注解。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询