java+nojaxbclassmap_如何在Java中使用JAXB2MARSCALER禁用XMLNS命名空间

我有一个运行良好的xml文件编写器类,它使用jaxb2marshaller编写最终的xml文件,代码如下:

final String XSD_FILE_NAME = myxsd.xsd

Jaxb2Marshaller marshaller = new Jaxb2Marshaller();

marshaller.setSchema(resourceLoader.getResource("classpath:xml/" + XSD_FILE_NAME));

marshaller.setContextPath("com.my.project.type");

marshaller.setMarshallerProperties(ImmutableMap.of(

JAXB_FORMATTED_OUTPUT, Boolean.TRUE,

JAXB_NO_NAMESPACE_SCHEMA_LOCATION, XSD_FILE_NAME

));

marshaller.afterPropertiesSet();

// this is the way to write into cache for validation:

JAXBElement myFileToMarshall = new ObjectFactory().createMyFile();

try {

marshaller.marshal(myFileToMarshall, new SAXResult(new DefaultHandler()));

} catch (XmlMappingException xme) {

throw new RuntimeException("Unable to create exact xml file, because the data is not valid!", xme);

}

try (FileOutputStream fileOutputStream = new FileOutputStream("C:/tmp/file.xml")) {

marshaller.marshal(myFileToMarshall, new StreamResult(fileOutputStream));

} catch (IOException e) {

throw new RuntimeException("Exact XML writing failed", e);

}

xsd的开头如下:

targetNamespace="http://xmlns.myproject.com/v1"

xmlns:xs="http://www.w3.org/2001/XMLSchema"

xmlns:tns="http://xmlns.myproject.com/v1">

最终生成的文件的开头如下所示:

我想要的是:

我想从最终文件中删除

xmlns="http://xmlns.myproject.com/v1"

部分,但能够使用xsd验证最终文件。

有可能吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值