java - Jaxb XML Attribute -
i doing marshalling of java object xml using jaxb . had requirement create thing
<link rel="self" href="test" />
how can done? annotations should use.
any appriciated
java class
public class item { private string title; private int price; private string productlink; private string rel; public string gettitle() { return title; } public void settitle(string title) { this.title = title; } public int getprice() { return price; } public void setprice(int price) { this.price = price; } @xmlpath("link/@href") public string getproductlink() { return productlink; } public void setproductlink(string productlink) { this.productlink = productlink; }
you can create link
class annotated @xmlrootelement
properties (rel
, href
) annotated @xmlattribute
.
the following tutorial acquainted jaxb (jsr-222):
option #1 - using eclipselink jaxb (moxy) jaxb provider
using @xmlpath
extension in eclipselink jaxb (moxy) following:
@xmlpath("link[@rel='self']/@href") public string getproductlink() { return productlink; }
for more information
option #2 - using jaxb provider
you use xmladapter
@xmlelement(name="link") @xmljavatypeadapter(linkadapter.class) public string getproductlink() { return productlink; }
linkadapter
import javax.xml.bind.annotation.*; import javax.xml.bind.annotation.adapters.xmladapter; public class linkadapter extends xmladapter<linkadapter.link, string>{ public static class link { @xmlattribute public string rel = "self"; @xmlattribute public string href; } @override public string unmarshal(link v) throws exception { return v.href; } @override public link marshal(string v) throws exception { link link = new link(); link.href = v; return link; } }
Comments
Post a Comment