如何使用JAXB将xmlns:xs和xmlns:xsi移到根元素? [英] How to move xmlns:xs and xmlns:xsi to Root Element with JAXB?

查看:294
本文介绍了如何使用JAXB将xmlns:xs和xmlns:xsi移到根元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从XSD生成的Java类生成XML文件.

I need to generate XML files from XSD generated Java Classes.

这些Java类中的某些字段作为 Object 而不是任何具体类型,因此可以保证在生成的XML文件中具有 xsi:type 属性,这很好

Some of the fields in those Java Classes as Object instead of any concrete type, and thus warrant a xsi:type attribute in the generated XML file, which is fine.

虽然没有什么好处,但是与该 xsi:type 一起添加了完整的名称空间定义( xmlns:xs ="http://www.w3.org/2001/XMLSchema"xmlns:xsi =" http://www.w3.org/2001/XMLSchema-instance"),并且使XML非常难以阅读.

What is NOT fine though is that along with that xsi:type, the full namespace definition is added (xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"), and makes the XML very unreadable.

总而言之,这就是我现在生成的:

To sum it up, here is what I generate now:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns:RootTag xmlns:ns="https://example.com">
    <ns:SomeObjectField xsi:type="xs:boolean" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">true</ns:SomeObjectField>
    <ns:SomeOtherObjectField xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Some other value</ns:SomePtherObjectField>
</ns:RootTag>

这就是我想生成的:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns:RootTag xmlns:ns="https://example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <ns:SomeObjectField xsi:type="xs:boolean">true</ns:SomeObjectField>
    <ns:SomeOtherObjectField xsi:type="xs:string">Some other value</ns:SomePtherObjectField>
</ns:RootTag>

推荐答案

我遇到了同样的问题.该解决方案假定您使用的是 JAXBContext marshaller ,则可以为名称空间或架构位置属性设置一个属性.就我而言,我需要一个noSchemaLocation:

I had the same issue. The solution assuming you are using the marshaller of the JAXBContext, you can set a property for your namespace or schema location property. In my case I needed a noSchemaLocation:

 jaxbMarshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, "facturaComputarizadaEstandar.xsd");

您可能需要为特定情况设置其他属性.

You might need to set a different property for your specific case.

这篇关于如何使用JAXB将xmlns:xs和xmlns:xsi移到根元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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