0

I am doing marshalling of java object to XML using JAXB . I had requirement to create some thing like

<link rel="self" href="test" />

How this can be done? what annotations should i use.

Any help will be greatly 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;
    }
6
  • This is not valid XML. Commented May 8, 2013 at 19:50
  • 1
    @AbdullahShoaib - looks valid to me Commented May 8, 2013 at 19:56
  • did you try reading a jaxb tutorial? Commented May 8, 2013 at 19:57
  • @ jtahlborn thanks for the suggestion, i need it urgently thats why i posted Commented May 8, 2013 at 20:00
  • sorry, the rest of the world isn't going to do your job for you, no matter how "urgent" it is for you. Commented May 8, 2013 at 20:01

1 Answer 1

1

You can create a Link class annotated with @XmlRootElement with to properties (rel and href) that are annotated with @XmlAttribute.

The following tutorial will help get you acquainted with JAXB (JSR-222):


OPTION #1 - Using EclipseLink JAXB (MOXy) as your JAXB Provider

Using the @XmlPath extension in EclipseLink JAXB (MOXy) you could do the following:

@XmlPath("link[@rel='self']/@href")
public String getProductLink() {
    return productLink;
}

For More Information


OPTION #2 - Using an JAXB Provider

You could use an 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;
    }

}
Sign up to request clarification or add additional context in comments.

3 Comments

thank you very much for your kind response, isn't possible some thing like @XmlPath("link/@href"). I did'nt understand how to give 'rel' as well to the same path
@user964147 - There are many ways to map this. Could you update your question to include the class structure you are trying to map this XML to?
Thanks a lot fro your response

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.