没有名称空间前缀的 JAXB XML 对象编组

发布于 2024-11-18 17:08:42 字数 12382 浏览 2 评论 0原文

我正在开发一个 java 项目,其中我需要从 XML 文件中读取一些对象,进行一些处理以更改对象的属性,然后将对象写入另一个 XML 文件。为此,我使用 JAXB 及其编组和解组功能,每个功能都在一个方法中,如下所示:

private MyObject unmarshallXMLFile(String file) {
    MyObject t=null;
    try {
        jc = JAXBContext.newInstance("foo.bar");
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
        unmarshaller.setSchema(sf.newSchema(new File("MySchema.xsd")));
        t = (Task) unmarshaller.unmarshal(new File(file));
    } catch (JAXBException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    }
    return t;
}

private void marshallXMLFile(String file) {
    task.setReplay(Boolean.TRUE);
    Marshaller marshaller;
    try {
        marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));
        marshaller.marshal(task, new FileOutputStream(file));
    } catch (JAXBException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

}

问题是自动生成的名称空间前缀(例如 ns2 或 ns3)不断出现在输出文件中,然后当我想要使用 unmarshallXMLFile 方法重用此文件(稍后我将再次使用输出文件作为输入),它不会根据架构进行验证,并抛出org.xml.sax.SAXParseException。以下是我编写的文件:

XML 架构:MySchema.xsd

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/MySchema"
    xmlns:spm="http://www.example.org/MySchema"
    elementFormDefault="qualified"
    attributeFormDefault="qualified">

    <element name="task" >
        <complexType>
            <sequence>
                <element name="replay" type="boolean" default="false"/>
                <element name="threads" type="spm:spThread" maxOccurs="unbounded" minOccurs="1" />
            </sequence>
        </complexType>
    </element>

    <complexType name="spThread">
        <sequence>
            <element name="SPThreadID" type="int" />
            <element name="durtime" minOccurs="0" default="0">
                <simpleType>
                    <restriction base="int">
                        <minInclusive value="0" />
                    </restriction>
                </simpleType>
            </element>
            <element name="minexecutions" minOccurs="0" default="0">
                <simpleType>
                    <restriction base="int">
                        <minInclusive value="0" />
                    </restriction>
                </simpleType>
            </element>
            <element name="numThreads" type="int" />
            <element name="procedures" type="spm:procedure" minOccurs="1"
                maxOccurs="unbounded" />
        </sequence>
    </complexType>

    <complexType name="procedure">
        <sequence>
            <element name="id" type="int" minOccurs="1" />
            <element name="name" type="string" minOccurs="1" />
            <element name="weight" minOccurs="1">
                <simpleType>
                    <restriction base="int">
                        <minInclusive value="0" />
                        <maxInclusive value="100" />
                    </restriction>
                </simpleType>
            </element>
            <element name="parameterPool" type="spm:parameter" nillable="true"
                minOccurs="0" maxOccurs="unbounded" />
        </sequence>
    </complexType>

    <complexType name="parameter">
        <sequence>
            <element name="name" type="string" minOccurs="1" />
            <element name="dataType" type="spm:parameterDataType" default="integer"/>
            <element name="parmType" type="spm:parameterType" default="in"
                minOccurs="0" />
            <element name="minValue" type="string"/>
            <element name="maxValue" type="string"/>
            <element name="value" type="string"/>
        </sequence>
    </complexType>

    <simpleType name="parameterDataType">
        <restriction base="string">
            <enumeration value="integer" />
            <enumeration value="varchar" />
            <enumeration value="char" />
        </restriction>
    </simpleType>

    <simpleType name="parameterType">
        <restriction base="string">
            <enumeration value="in" />
            <enumeration value="out" />
            <enumeration value="in_out" />
        </restriction>
    </simpleType>

</schema>

输入文件:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<task xmlns="http://www.example.org/MySchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/MySchema MySchema.xsd ">
    <replay>true</replay>
    <threads>
        <SPThreadID>0</SPThreadID>
        <durtime>10</durtime>
        <minexecutions>2</minexecutions>
        <numThreads>3</numThreads>
        <procedures>
            <id>1</id>
            <name>run</name>
            <weight>15</weight>
            <parameterPool>
                <name>energy</name>
                <dataType>integer</dataType>
                <parmType>in</parmType>
                <minValue>10</minValue>
                <maxValue>50</maxValue>
                <value>11</value>                
            </parameterPool>
            <parameterPool>
                <name>speed</name>
                <dataType>integer</dataType>
                <parmType>in</parmType>
                <minValue>12</minValue>
                <maxValue>80</maxValue>
                <value>13</value>                                
            </parameterPool>
        </procedures>
    </threads>
</task>

输出文件(不进行任何处理:只是使用前面提到的方法进行解组和编组)

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:task xmlns="http://www.example.org/MySchema" xmlns:ns2="http://www.example.org/MySchema.xsd">
    <replay>true</replay>
    <threads>
        <SPThreadID>0</SPThreadID>
        <durtime>10</durtime>
        <minexecutions>2</minexecutions>
        <numThreads>3</numThreads>
        <procedures>
            <id>1</id>
            <name>run</name>
            <weight>15</weight>
            <parameterPool>
                <name>energy</name>
                <dataType>integer</dataType>
                <parmType>in</parmType>
                <minValue>10</minValue>
                <maxValue>50</maxValue>
                <value>11</value>
            </parameterPool>
            <parameterPool>
                <name>speed</name>
                <dataType>integer</dataType>
                <parmType>in</parmType>
                <minValue>12</minValue>
                <maxValue>80</maxValue>
                <value>13</value>
            </parameterPool>
        </procedures>
    </threads>
</ns2:task>

异常(再次使用输出文件作为输入时):

javax.xml.bind.UnmarshalException
 - with linked exception:
[org.xml.sax.SAXParseException: cvc-elt.1: The declaration of the element'ns3:task' could not be found.]
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:326)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:500)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:206)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:175)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:148)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:153)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:162)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:180)
    at Test.main(Test.java:48)
Caused by: org.xml.sax.SAXParseException: cvc-elt.1: The declaration of the element'ns3:task' could not be found.
    at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
    at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    at org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
    at org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
    at org.apache.xerces.jaxp.validation.XMLSchemaValidatorHandler.startElement(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.ValidatingUnmarshaller.startElement(ValidatingUnmarshaller.java:85)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:113)
    at org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown Source)
    at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
    at org.apache.xerces.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
    at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:202)
    ... 6 more

I´我一直在阅读有关该主题的内容并尝试了大量相关答案,但似乎没有一个能够删除前缀。我浏览了这个指南,但我使用的jaxb版本不支持NamespacePrefixMapper。我尝试使用注释 这里配置前缀,但这不起作用。

也许有一种方法可以摆脱这个命名空间前缀:我发现的所有论坛、答案和讨论都在谈论自定义这个前缀,我只是想摆脱它们。但不知怎的,它让我觉得我在输入文件和模式中丢失了一些东西。它们写得好吗?我想说这就是问题所在,因为这是我第一次在这个深度上使用 xml 和 xsd,而且我所做的只是基于我在网上找到的内容。 有关改进 xml 和 xsd 设计的任何提示将受到高度赞赏

如果我在输入文件或模式中使用某种前缀,以便 JAXB 框架在编组时不会生成随机前缀,那么

?预先感谢,我希望你们能帮助我。

——

非常感谢您的回答。这样我就可以使用 NamespacePrefixMapper。但是,当我使用它时,我的代码在运行时不断抛出异常:

Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name javax.xml.bind.Messages, locale de_DE
    at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:863)
    at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:832)
    at java.util.ResourceBundle.getBundle(ResourceBundle.java:576)
    at javax.xml.bind.Messages.format(Messages.java:47)
    at javax.xml.bind.Messages.format(Messages.java:36)
    at javax.xml.bind.PropertyException.<init>(PropertyException.java:99)
    at javax.xml.bind.helpers.AbstractMarshallerImpl.setProperty(AbstractMarshallerImpl.java:349)
    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.setProperty(MarshallerImpl.java:527)
    at Test.main(Test.java:95)

我发现它必须对 .properties 文件执行某些操作:我没有使用类似的东西,我没有更改任何内容。

Im working on a java project where i need to read some objects from an XML file, do some processing which will alter the object´s atributes and then write the Object to another XML file. For that purpose, i am using JAXB with its marshalling and unmarshalling capabilities, each of them in a method, like this:

private MyObject unmarshallXMLFile(String file) {
    MyObject t=null;
    try {
        jc = JAXBContext.newInstance("foo.bar");
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
        unmarshaller.setSchema(sf.newSchema(new File("MySchema.xsd")));
        t = (Task) unmarshaller.unmarshal(new File(file));
    } catch (JAXBException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    }
    return t;
}

private void marshallXMLFile(String file) {
    task.setReplay(Boolean.TRUE);
    Marshaller marshaller;
    try {
        marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));
        marshaller.marshal(task, new FileOutputStream(file));
    } catch (JAXBException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

}

The problem is that automatically-generated namespace prefixes such as ns2 or ns3 keep appearing in the output file, and then when i want to reuse this files with the unmarshallXMLFile method (i will be using the output files as input again later) it wont get validated against the schema, and throws a org.xml.sax.SAXParseException. Here are the files i wrote:

XML Schema: MySchema.xsd

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/MySchema"
    xmlns:spm="http://www.example.org/MySchema"
    elementFormDefault="qualified"
    attributeFormDefault="qualified">

    <element name="task" >
        <complexType>
            <sequence>
                <element name="replay" type="boolean" default="false"/>
                <element name="threads" type="spm:spThread" maxOccurs="unbounded" minOccurs="1" />
            </sequence>
        </complexType>
    </element>

    <complexType name="spThread">
        <sequence>
            <element name="SPThreadID" type="int" />
            <element name="durtime" minOccurs="0" default="0">
                <simpleType>
                    <restriction base="int">
                        <minInclusive value="0" />
                    </restriction>
                </simpleType>
            </element>
            <element name="minexecutions" minOccurs="0" default="0">
                <simpleType>
                    <restriction base="int">
                        <minInclusive value="0" />
                    </restriction>
                </simpleType>
            </element>
            <element name="numThreads" type="int" />
            <element name="procedures" type="spm:procedure" minOccurs="1"
                maxOccurs="unbounded" />
        </sequence>
    </complexType>

    <complexType name="procedure">
        <sequence>
            <element name="id" type="int" minOccurs="1" />
            <element name="name" type="string" minOccurs="1" />
            <element name="weight" minOccurs="1">
                <simpleType>
                    <restriction base="int">
                        <minInclusive value="0" />
                        <maxInclusive value="100" />
                    </restriction>
                </simpleType>
            </element>
            <element name="parameterPool" type="spm:parameter" nillable="true"
                minOccurs="0" maxOccurs="unbounded" />
        </sequence>
    </complexType>

    <complexType name="parameter">
        <sequence>
            <element name="name" type="string" minOccurs="1" />
            <element name="dataType" type="spm:parameterDataType" default="integer"/>
            <element name="parmType" type="spm:parameterType" default="in"
                minOccurs="0" />
            <element name="minValue" type="string"/>
            <element name="maxValue" type="string"/>
            <element name="value" type="string"/>
        </sequence>
    </complexType>

    <simpleType name="parameterDataType">
        <restriction base="string">
            <enumeration value="integer" />
            <enumeration value="varchar" />
            <enumeration value="char" />
        </restriction>
    </simpleType>

    <simpleType name="parameterType">
        <restriction base="string">
            <enumeration value="in" />
            <enumeration value="out" />
            <enumeration value="in_out" />
        </restriction>
    </simpleType>

</schema>

input file:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<task xmlns="http://www.example.org/MySchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/MySchema MySchema.xsd ">
    <replay>true</replay>
    <threads>
        <SPThreadID>0</SPThreadID>
        <durtime>10</durtime>
        <minexecutions>2</minexecutions>
        <numThreads>3</numThreads>
        <procedures>
            <id>1</id>
            <name>run</name>
            <weight>15</weight>
            <parameterPool>
                <name>energy</name>
                <dataType>integer</dataType>
                <parmType>in</parmType>
                <minValue>10</minValue>
                <maxValue>50</maxValue>
                <value>11</value>                
            </parameterPool>
            <parameterPool>
                <name>speed</name>
                <dataType>integer</dataType>
                <parmType>in</parmType>
                <minValue>12</minValue>
                <maxValue>80</maxValue>
                <value>13</value>                                
            </parameterPool>
        </procedures>
    </threads>
</task>

output file (without any processing whatsoever: just unmarshalling and marshalling back with the methods mentioned earlier)

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:task xmlns="http://www.example.org/MySchema" xmlns:ns2="http://www.example.org/MySchema.xsd">
    <replay>true</replay>
    <threads>
        <SPThreadID>0</SPThreadID>
        <durtime>10</durtime>
        <minexecutions>2</minexecutions>
        <numThreads>3</numThreads>
        <procedures>
            <id>1</id>
            <name>run</name>
            <weight>15</weight>
            <parameterPool>
                <name>energy</name>
                <dataType>integer</dataType>
                <parmType>in</parmType>
                <minValue>10</minValue>
                <maxValue>50</maxValue>
                <value>11</value>
            </parameterPool>
            <parameterPool>
                <name>speed</name>
                <dataType>integer</dataType>
                <parmType>in</parmType>
                <minValue>12</minValue>
                <maxValue>80</maxValue>
                <value>13</value>
            </parameterPool>
        </procedures>
    </threads>
</ns2:task>

exception (when using the ouput file again as an input):

javax.xml.bind.UnmarshalException
 - with linked exception:
[org.xml.sax.SAXParseException: cvc-elt.1: The declaration of the element'ns3:task' could not be found.]
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:326)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:500)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:206)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:175)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:148)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:153)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:162)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:180)
    at Test.main(Test.java:48)
Caused by: org.xml.sax.SAXParseException: cvc-elt.1: The declaration of the element'ns3:task' could not be found.
    at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
    at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    at org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
    at org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
    at org.apache.xerces.jaxp.validation.XMLSchemaValidatorHandler.startElement(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.ValidatingUnmarshaller.startElement(ValidatingUnmarshaller.java:85)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:113)
    at org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown Source)
    at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
    at org.apache.xerces.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
    at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:202)
    ... 6 more

I´ve been reading about the subject and tried loads of related answers, but none of them seems to remove the prefixes. I went trough this guide but the version of jaxb i am using does not support NamespacePrefixMapper. I tried using annotations as described here to configure the prefixes but that wouldnt work.

Maybe there is a way of getting rid of this namespace prefixes: all the forums, answers and discussions i´ve found talk about customizing this prefixes, i just want to get rid of them. But somehow it makes me think that i´m missing something in both my input file and schema. Are they well written? I would say that right there is the problem, because it is the first time i work with xml and xsd at this depth, and what i´ve done is only based on what i´ve found online. Any tips on improving the xml and xsd designs would be highly appreciated

should i be using some kind of prefixes in the input file or in the schema so the JAXB framework wont generate random prefixes at time of marshalling?

thanks in advance, i hope you guys can help me.

--

Thank you very much for the answer. In that way i can use the NamespacePrefixMapper. However, when i use it, my code keeps throwing exceptions when i run it:

Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name javax.xml.bind.Messages, locale de_DE
    at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:863)
    at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:832)
    at java.util.ResourceBundle.getBundle(ResourceBundle.java:576)
    at javax.xml.bind.Messages.format(Messages.java:47)
    at javax.xml.bind.Messages.format(Messages.java:36)
    at javax.xml.bind.PropertyException.<init>(PropertyException.java:99)
    at javax.xml.bind.helpers.AbstractMarshallerImpl.setProperty(AbstractMarshallerImpl.java:349)
    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.setProperty(MarshallerImpl.java:527)
    at Test.main(Test.java:95)

I found that it has to do something with a .properties file: i am not using anything like it, i havent changed anything.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

他夏了夏天 2024-11-25 17:08:42

无需在每个元素中指定 @XmlElement 的命名空间属性,在包级别进行注释会更简单。
您可以通过在要注释的包下面创建一个文件 package-info.java 来实现这一点。

例如,如果要注释包 org.example,则必须将名为 package-info.java 的文件放置在目录 org/example 中,其内容如下:

@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.example.org/StoredProceduresSchema", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package org.example;

需要注意的是,您必须注释每个包含您计划编组或被这些类引用的类的包。

希望这有帮助:)

Instead of specifying the @XmlElement's namespace attribute in every element, it's simpler to annotate at package level.
You do that by creating a file package-info.java just underneath the package you want to annotate.

For example, if you want to annotate the package org.example, then a file named package-info.java must be placed within the directory org/example with the following content:

@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.example.org/StoredProceduresSchema", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package org.example;

It's important to note that you must annotate every package that contains classes that your plan to marshal or are referenced by those.

Hope this helps :)

花之痕靓丽 2024-11-25 17:08:42

尝试使用 NamespacePrefixMapper

NamespacePrefixMapper mapper = new NamespacePrefixMapper() {
    public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) {
        return "";
    }
};
marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", mapper);

Try using a NamespacePrefixMapper:

NamespacePrefixMapper mapper = new NamespacePrefixMapper() {
    public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) {
        return "";
    }
};
marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", mapper);
没有伤那来痛 2024-11-25 17:08:42

好吧,经过一番研究,我尝试在我尝试序列化的类的每个属性上使用 @XMLElement 标记,明确指定我的命名空间是什么,并在每个属性上使用相同的标记:

@XmlElement(required = true, name="myObjectPool", namespace="http://www.example.org/StoredProceduresSchema")
    protected List<MyObject> myObjectPool;

它有效完美无缺:编组文件中不再有奇怪的名称空间。

我要感谢他的回答:我也尝试过,但我遇到了一个与语言包相关的奇怪异常。我很高兴这种更简单的方法解决了这个问题。

Well, after some research, i tried using the @XMLElement tag on every attribute of the classes i am trying to serialize, specifying clearly what my namespace was, and using the same one on every attribute:

@XmlElement(required = true, name="myObjectPool", namespace="http://www.example.org/StoredProceduresSchema")
    protected List<MyObject> myObjectPool;

It worked flawlessly: no more weird namespaces in the marshalled file.

I wanna thank for his answer: i tried that as well, but i got a weird language bundle related exception. Im glad this simpler approach solved the issue.

压抑⊿情绪 2024-11-25 17:08:42

那么您可能会使用与参考不同的 JAXB 实现。阅读本文并重试:http://blog.bdoughan。 com/2011/11/jaxb-and-namespace-prefixes.html (或者如果你很懒:将 com.sun.xml.bind.namespacePrefixMapper 替换为com.sun.xml.internal.bind.namespacePrefixMapper)

Then you might be using a different JAXB implementation then the reference. Read this article and try again: http://blog.bdoughan.com/2011/11/jaxb-and-namespace-prefixes.html (or if you are lazy: replace com.sun.xml.bind.namespacePrefixMapper with com.sun.xml.internal.bind.namespacePrefixMapper)

八巷 2024-11-25 17:08:42

Java 7/8 解决方案

这个问题与默认实现 JAXB Provider 有关。我找到了使用不同实现的解决方案:EclipseLink MOXy

1. add the dependency in pom.xml

<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>org.eclipse.persistence.moxy</artifactId>
    <version>2.5.0</version>
</dependency>`

2. create a file jaxb.properties containing the following line

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

将文件移动到包模型中

3. create package-file.java into the model package

@XmlSchema(xmlns = { @XmlNs(prefix = "video", namespaceURI = "http://www.google.com/schemas/sitemap-video/1.1"),
        @XmlNs(prefix = "", namespaceURI = "http://www.sitemaps.org/schemas/sitemap/0.9")})

package it.my.sitemap.model;

import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlSchema;

Java 7/8 solution

This problem is related to the default implementation JAXB Provider. I have found a solution using a different implementation: EclipseLink MOXy.

1. add the dependency in pom.xml

<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>org.eclipse.persistence.moxy</artifactId>
    <version>2.5.0</version>
</dependency>`

2. create a file jaxb.properties containing the following line

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

move the file into the package model

3. create package-file.java into the model package

@XmlSchema(xmlns = { @XmlNs(prefix = "video", namespaceURI = "http://www.google.com/schemas/sitemap-video/1.1"),
        @XmlNs(prefix = "", namespaceURI = "http://www.sitemaps.org/schemas/sitemap/0.9")})

package it.my.sitemap.model;

import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlSchema;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文