jaxb xmlElement命名空间不起作用 [英] jaxb xmlElement namespace not working

查看:530
本文介绍了jaxb xmlElement命名空间不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间以来,我在向属性添加名称空间时遇到了麻烦.我的要求是创建xml,它将在子元素而不是root上具有名称空间uri.我正在将jaxb与eclipselink moxy,jdk7一起使用.

I'm having trouble adding namespace to property for some time now. My requirement is to create xml which will have namespace uri on child element rather than root. I'm using jaxb with eclipselink moxy, jdk7.

<document>
<Date> date </Date>
</Type>type </Type>
<customFields xmlns:pns="http://abc.com/test.xsd">
  <id>..</id>
  <contact>..</contact>
</customFields>
</document>

Classes are:

@XmlRootElement(name = "document")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = {"type","date", "customFields"})
public class AssetBean {

@XmlElement(name="Type")
private String type;
@XmlElement(name="Date")


@XmlElement(name = "CustomFields",namespace = "http://api.source.com/xsds/path/to/partner.xsd")    
private CustomBean customFields = new CustomBean();

//getters/setters here

}

public class CustomBean {

 private String id;
 private String contact;
 //getter/setter
}
package-info.java
@javax.xml.bind.annotation.XmlSchema (        
 xmlns = { 
 @javax.xml.bind.annotation.XmlNs(prefix="pns",
           namespaceURI="http://api.source.com/xsds/path/to/partner.xsd")
 },
elementFormDefault = javax.xml.bind.annotation.XmlNsForm.UNQUALIFIED
)
package com.abc.xyz

我关注了这篇文章以寻求帮助,但无法获得正在尝试的内容 http://blog.bdoughan.com/2010/08/jaxb-namespaces.html

I followed this article for help but cant get what I'm trying http://blog.bdoughan.com/2010/08/jaxb-namespaces.html

谢谢

推荐答案

域模型(根)

在下面的域对象中,我将使用@XmlElement批注将名称空间分配给其中一个元素.

In the domain object below I'll assign a namespace to one of the elements using the @XmlElement annotation.

import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {

    String foo;

    @XmlElement(namespace="http://www.example.com")
    String bar;

}

演示代码

在下面的演示代码中,我们将创建域对象的实例并将其编组为XML.

In the demo code below we will create an instance of the domain object and marshal it to XML.

import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Root.class);

        Root root = new Root();
        root.foo = "FOO";
        root.bar = "BAR";

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(root, System.out);
    }

}

输出

下面是运行演示代码的输出.我们使用@XmlElement注释将名称空间分配给的元素是正确的名称空间限定,但是名称空间声明出现在根元素上.

Below is the output of running the demo code. The element that we assigned the namespace to with the @XmlElement annotation is properly namespace qualified, but the namespace declaration appears on the root element.

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:ns0="http://www.example.com">
   <foo>FOO</foo>
   <ns0:bar>BAR</ns0:bar>
</root>

更多信息

这篇关于jaxb xmlElement命名空间不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆