开发者

SAXParseException while trying to unmarshal

I am pretty new to JAXB. my experience with it was pretty good but now i am having some problem with the unmarshaling.

My class implements the follwing interface:

public interface Attribute {

    public String getAttrName();
    public void setAttrName(String s);
    public String getAttrValue();
    public void setAttrValue(String s);

    public Object getPrincipal();
    public void setPrincipal(Object o);
}

Where getPricipal returns in this case a User class.

I marshal the object like this:

Marshaller m = context.createMarshaller();
JAXBContext context = JAXBContext.newInstance(AttributeImpl.class, UserImpl.class, RoleImpl.class);
Marshaller m = context.createMarshaller();
m.marshal(attribute, sw);

And it generates the following XML

<profileAttribute>
    <attrName>KEY2</attrName>
    <attrValue>2_VALUE2</attrValue>
    <principal xsi:type="userImpl">
        <enabled>true</enabled>
        <externallyDefined>false</externallyDefined>
        <fullName>NAME</fullName>
        <password>PASSWORD</password>
        <prev开发者_如何学JAVAiousPasswordChangeTime>2011-10-05T11:16:44.960-07:00
        </previousPasswordChangeTime>
        <roles xsi:type="roleImpl">
            <externallyDefined>false</externallyDefined>
            <roleName>ROLE_USER</roleName>
        </roles>
        <roles xsi:type="roleImpl">
            <externallyDefined>false</externallyDefined>
            <roleName>ROLE_ADMINISTRATOR</roleName>
        </roles>
        <username>jasperadmin</username>
    </principal>
</profileAttribute>

My problem is when i am trying unmarshal the same XML with the follwing code

JAXBContext jc = JAXBContext.newInstance( AttributeImpl.class, UserImpl.class, RoleImpl.class );
Unmarshaller u = jc.createUnmarshaller();
ProfileAttribute pa = (ProfileAttribute) u.unmarshal(req.getInputStream()) ;

I am getting an Exception. javax.xml.bind.UnmarshalException with linked exception: [org.xml.sax.SAXParseException: The prefix "xsi" for attribute "xsi:type" associated with an element type "principal" is not bound

  1. Any advice on what i am doing wrong would be appreciated.
  2. Another question is how do i exlude a member from being marshaled?

Thanks


Any advice on what i am doing wrong would be appreciated.

It appears as though there is a bug in the implementation (Metro, MOXy, JaxMe, etc) of JAXB that you are using. From the document you have provided there should have been a namespace declaration for the xsi prefix included.

<profileAttribute xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    ....
</profileAttribute>

Which JAXB implementation and version are you using?

The following article is related to your use case, and you may find it helpful:

  • http://blog.bdoughan.com/2011/05/jaxb-and-interface-fronted-models.html

Another question is how do i exclude a member from being marshaled?

You can use the @XmlTransient annotation on a field/property to prevent that field/property from being marshalled.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜