[英]How to move xmlns:xs and xmlns:xsi to Root Element with JAXB?
I need to generate XML files from XSD generated Java Classes.我需要从 XSD 生成的 Java 类生成 XML 文件。
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.这些 Java 类中的一些字段作为
Object
而不是任何具体类型,因此保证生成的 XML 文件中有xsi:type
属性,这很好。
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.但
xsi:type
是,除了xsi:type
,还添加了完整的命名空间定义( xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
),并使 XML 非常不可读。
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>
And this is what I would like to generate:这就是我想要生成的:
<?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>
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.
假设您使用JAXBContext的编组器的解决方案,您可以为您的命名空间或模式位置属性设置一个属性。 In my case I needed a noSchemaLocation:
就我而言,我需要一个 noSchemaLocation:
jaxbMarshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, "facturaComputarizadaEstandar.xsd");
You might need to set a different property for your specific case.您可能需要为您的特定情况设置不同的属性。
You can explicitly declare xsi in package-info.java
:您可以在
package-info.java
显式声明 xsi :
@javax.xml.bind.annotation.XmlSchema(
xmlns = {
@javax.xml.bind.annotation.XmlNs(
prefix = "ns",
namespaceURI = "https://example.com"),
@javax.xml.bind.annotation.XmlNs(
prefix = "xsi",
namespaceURI = javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI) },
namespace = "https://example.com",
elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package org.foo;
<ns:RootTag>
<ns:RootTag>
<RootTag>
<RootTag>
See JAXBContextImpl.xmlNsSet见JAXBContextImpl.xmlNsSet
NamespaceContextImpl.declareNsUri() NamespaceContextImpl.declareNsUri()
JAXBContextImpl.schemaLocation JAXBContextImpl.schemaLocation
In the older jaxb implementation 2.1 the @XmlNs
is only used when generating a schema file and as a workaround you can add:在较旧的 jaxb 实现 2.1 中,
@XmlNs
仅在生成架构文件时使用,作为解决方法,您可以添加:
@XmlSeeAlso(DummyTypeWithinXsi.class)
public class RootTag ...
...
@XmlRootElement(namespace = javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI)
public class DummyTypeWithinXsi {
}
_ _
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.