当前位置: 首页 > 面试题库 >

JAXB xsi:type子类解组不起作用

商经业
2023-03-14
问题内容

我遵循此经常引用的博客文章中有关使用xsi:type的说明:

http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-using-
xsitype.html

基本上我有这个:

public abstract class ContactInfo {
}

public class Address extends ContactInfo {

    private String street;

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }
}

@XmlRootElement
public class Customer {

    private ContactInfo contactInfo;

    public ContactInfo getContactInfo() {
        return contactInfo;
    }

    public void setContactInfo(ContactInfo contactInfo) {
        this.contactInfo = contactInfo;
    }
}

而这个测试:

@Test
public void contactTestCase() throws JAXBException, ParserConfigurationException, IOException, SAXException {
    Customer customer = new Customer();
    Address address = new Address();
    address.setStreet("1 A Street");
    customer.setContactInfo(address);

    JAXBContext jc = JAXBContext.newInstance(Customer.class, Address.class, PhoneNumber.class);
    StringWriter writer = new StringWriter();
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(customer, writer);
    String s = writer.toString();
    System.out.append(s);

    StringInputStream sis = new StringInputStream(s);
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = documentBuilderFactory.newDocumentBuilder();
    Document doc = db.parse(sis);

    Unmarshaller um = jc.createUnmarshaller();
    JAXBElement result = um.unmarshal(doc, Customer.class);
    Customer f = (Customer) result.getValue();

    writer = new StringWriter();
    marshaller.marshal(customer, writer);
    s = writer.toString();
    System.out.append(s);
}

我得到这个结果:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer>
    <contactInfo xsi:type="address" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <street>1 A Street</street>
    </contactInfo>
</customer>

javax.xml.bind.UnmarshalException: Unable to create an instance of blog.inheritance.ContactInfo

我已经尝试了JAXB的默认实现jaxb-
impl-2.1.2,并基于此bug尝试了jaxb-
impl-2.2.6-b38.jar。没有一个有效。

这不应该工作还是我缺少一些设置?


问题答案:

在您的测试用例中,您需要指定DocumentBuilderFactory可以识别名称空间。没有此设置,JAXB实现的DOM输入将不会包含格式正确的xsi:type属性。

    documentBuilderFactory.setNamespaceAware(true);


 类似资料:
  • 问题内容: 我正在使用日志记录(我不想使用log4j或其他任何东西)。 这是我完整的私有logging.properties: 这是我程序中的代码: 由于这会在两行显示每个日志消息,因此我尝试了 如何使Java日志输出显示在一行上? 完全在第一个回复中复制了LogFormatter类。 在我的logging.properties中更改了一行 现在,我的日志开始以XML出现。我强烈感觉那个不喜欢我,

  • 我有一个包含HTML组件,如下所示: 此组件的CSS(或部分CSS)为: 其基本思想是,当通过页面上的某个click事件应用时,中的div会发生一些动画。因此该组件中的divs类似于: 使用CSS: 我的问题是,上面的页面(带有)是发生事件的地方,因此它也是应用类的地方。但是,实际的组件,它只是包含一堆div(应该是动画的),嗯,它们根本没有动画。 我可以看到类是在单击按钮时应用的,但我猜当类应用

  • 我是新的mongodb,我有一个文档在我的db如下: {“_id”:{“$oid”:“546e916cd38e0b3e0e79592f”},名称:“'hamed”,“实体:[“1”,“2”]} 现在我想阅读实体为[“1”,“2”]的aal文档,为此我尝试了以下代码: 但它不返回任何东西...有人能帮助我如何正确地写我的where子句吗?

  • 第一次在这里写东西。 为什么我的子程序不起作用?我正在尝试打开main函数中的子例程以获取布尔值。

  • 我有这个xml文件: 我有以下java代码: 在控制台输出中,我看到 我做错了什么? java版本“1.7.0_45”

  • 我试图散集XML。 这就是我的XML的样子 我正在尝试获取Obj123下的Id和姓名。然而,当我运行我的解组命令时,我得到了以下错误。 我的代码在主类中如下所示: 我的Obj123类如下所示: 我认为通过设置我的XMLRootElement,我应该能够跳过XML的前2行,但这似乎并没有发生。有什么想法吗? 编辑: 我的 JAXB 上下文就是这样制作的: