Java 在xml中设置xmlschema

Java 在xml中设置xmlschema,java,android,xsd,jibx,Java,Android,Xsd,Jibx,我正在使用jibx库生成xsd到java代码。我还使用ant创建了jar文件。 我们需要设置xmlschemalocation,这样当我们按照下面的方式处理消息时,我们可以得到xsd位置 public String marshalMessage(Object message) { try { IBindingFactory jc = BindingDirectory.getFactory(DeviceCapability.class);

我正在使用jibx库生成xsd到java代码。我还使用ant创建了jar文件。 我们需要设置xmlschemalocation,这样当我们按照下面的方式处理消息时,我们可以得到xsd位置

public String marshalMessage(Object message) 
    {
        try {
            IBindingFactory jc = BindingDirectory.getFactory(DeviceCapability.class);       

            IMarshallingContext marshaller = jc.createMarshallingContext();     

            ByteArrayOutputStream out = new ByteArrayOutputStream();        

            marshaller.marshalDocument(message, URL_ENCODING, null, out);
            return out.toString(STRING_ENCODING);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (JiBXException e) {
            e.printStackTrace();
        }
        return null;
    }
//这用于创建对象

DeviceCapability devicecapability  = new  DeviceCapability();
 devicecapability.setHref("Hello");
 String xml = marshalMessage(devicecapability);

生成的xml o/p是 ?xml version=“1.0”encoding=“UTF-8”?> /设备容量xmlns=”http://zigbee.org/sep“href=”你好“/>

我想要下面这样的订单 ?xml version=“1.0”encoding=“UTF-8”>设备容量xmlns=”http://zigbee.org/sep“href=“Hello”xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance“xsi:schemaLocation=”http://zigbee.org/abc abc.xsd/>


有人能告诉我如何使用jibx添加schemalocation吗?我使用了ant codegen/bind工具。

jibx可以轻松处理多个名称空间。您所要做的就是指定模式定义(.xsd)文件中的所有名称空间。例如,下面是我的一个模式定义中的一个片段:








JiBX生成的XML具有您想要的名称空间定义

Don-JiBX贡献者