标签: jaxb

wsimport:无法重命名包名称

我在https://mycompany.com/mywspath/Documents.svc?wsdl这样的地址有一个 wsdl 似乎 xsd 在 https://mycompany.com/mywspath/Documents.svc?wsdl&xsd=xsd0处可见

给一个

wsimport https://mycompany.com/mywspath/Documents.svc?wsdl
Run Code Online (Sandbox Code Playgroud)

我获得包含与文档和依赖项相关的类的层次结构(com.microsoft,...)

com
-microsoft
--...
-mycompany
--...
org
-datacontract
--schemas
---...
Run Code Online (Sandbox Code Playgroud)

我的意愿是将 com.mycompany.mywspath 中生成的 clasees 重构为 com.mycompany.mywspath.test 或 com.mycompany.mywspathA

这是因为在同一命名空间中还有其他 Web 服务包含其他定义,因此生成的类存储在同一包 com.mycompany.mywspath 中,并且从第一个 wsimport 获取的 ObjectFactory.class 会被以下调用生成的类覆盖。

我的意愿是使用 wsimport 的 -b 选项避免在来自不同 Web 服务的命名空间的不同包上进行这种绑定。

我使用了这样的 XML 绑定文件:

 <jxb:bindings 
        jxb:version="2.1"  
        xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"   
        xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
        xmlns:xs="http://www.w3.org/2001/XMLSchema" 
        xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
        xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc">

        <bindings schemaLocation="https://https://mycompany.com/mywspath/Documents.svc?xsd=xsd0"
                node="//xsd:schema[@targetNamespace='http://mycompany.com/mywspath']">
            <jxb:schemaBindings>
                <jxb:package name="com.mycompany.mywspath"/>
            </jxb:schemaBindings>
        </bindings>

    </jxb:bindings>
Run Code Online (Sandbox Code Playgroud)

类起源于同一位置,没有任何修改,并且 com/mycompany/mywspath/test 不是起源的。

如何将这些类移至 com/mycompany/mywspath/test 包?

java xml jaxb wsimport

2
推荐指数
1
解决办法
4899
查看次数

java.net.MalformedURLException:使用 JAXB 时没有带有 javax.xml.stream.XMLStreamException 的协议

我正在使用 JAXB 来解组 XML 文档。解析 XML 时,它会抛出用 XMLStreamException 包装的 MalformedURLException。我的理解是,在创建 XMLStreamReader 对象本身时,它会抛出异常。请问有什么建议吗?

我正在使用的代码片段:

    XMLInputFactory xif = XMLInputFactory.newFactory();      
    XMLResolver resolver = new XMLResolver(); //to capture systemID, base URI etc.
    xif.setXMLResolver(resolver);

    //Throws MalformedURLException  while processing the below line
    XMLStreamReader xsr = xif.createXMLStreamReader(new StreamSource(fileToProcess));

    JAXBContext jaxbContext = JAXBContext.newInstance(MyPackage.MyClassName.class);
Run Code Online (Sandbox Code Playgroud)

这是异常跟踪:

     class javax.xml.stream.XMLStreamException
     javax.xml.stream.XMLStreamException: java.net.MalformedURLException: no protocol:       [XML_FILEPATH/XML_FILE_NAME]
Run Code Online (Sandbox Code Playgroud)

fileToProcess是包含绝对路径的字符串,例如 /home/project/input/myproject.xml

运行时 JDK 是 1.7。我缺少任何签名/协议吗?

谢谢,巴斯卡

xml jaxb malformedurlexception xml-parsing xmlstreamreader

2
推荐指数
1
解决办法
9362
查看次数

@XmlSchema:“注释类型不适用于此类声明”

2.11 和 java7。我正在尝试用@XmlSchema如下所示的方式注释我的包。

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSchema;

@XmlSchema(namespace = "http://www.sitemaps.org/schemas/sitemap/0.9",
    elementFormDefault = XmlNsForm.QUALIFIED)
@XmlRootElement(name="urlset")
public class Urlset {

    private String name;

    @XmlElement
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}
Run Code Online (Sandbox Code Playgroud)

但是我收到此编译错误“注释类型不适用于此类声明”

有谁知道为什么会发生这种情况?

在此输入图像描述

java xml jaxb

2
推荐指数
1
解决办法
3612
查看次数

JAXB 解组神秘的 XML

我正在使用 JAXB 来解组 XML 文件。

关于 XML 文件,我所知道的只是它是有效的 XML。

那么我应该如何为 newInstance 指定一个类和/或包呢?

JAXBContext jaxbContext = JAXBContext.newInstance(??????);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Object o = (Object) unmarshaller.unmarshal(myFile);
Run Code Online (Sandbox Code Playgroud)

我在文档中没有看到任何解决此问题的内容。

java xml jaxb

2
推荐指数
1
解决办法
800
查看次数

使用 Groovy 解组 JAXB

我正在尝试为以下 xml 创建模型类:

<Response>
    <Success>N</Success>
    <Errors>
        <Error>
            <Number>29002</Number>
            <Message>A key field was missing from the control xml</Message>
        </Error>
        <Error>
            <Number>29004</Number>
            <Message>Unable to accept messages at this time</Message>
        </Error>
    </Errors>
</Response>
Run Code Online (Sandbox Code Playgroud)

这是我的 Response.class

@XmlRootElement (name="Response")
@XmlAccessorType( XmlAccessType.FIELD )
class Response {

  @XmlElement(name="Success")
  private String success

  @XmlElement(name="Errors")
  private Errors errors

  public String getSuccess() {
    return success
  }

  public Errors getErrors() {
    return errors;
  }
}
Run Code Online (Sandbox Code Playgroud)

这是我的 Errors.class

@XmlRootElement(name="Errors")
@XmlAccessorType(XmlAccessType.FIELD)
class Errors {

  public Errors() {
    errorList = new ArrayList<Error>()
  }

  @XmlElement(name = …
Run Code Online (Sandbox Code Playgroud)

xml groovy jaxb unmarshalling

2
推荐指数
1
解决办法
1991
查看次数

JAXB 生成的类无法识别

我正在 Maven 项目中尝试使用 JAXB 进行 xml 映射。我将 JAXB jar 放在一个单独的项目中,并将其作为依赖项添加到我的主项目中。我正在使用 Eclipse。

现在,JAXB 的功能似乎很好,并且从某些 XSD 模式中,在 target/ generated-sources/xjc 文件夹中生成了几个 Java 类。问题是 Eclipse 无法解析主项目中单元测试中的那些类。我什至手动导入这些包名称,但类名称仍然无法解析。我有什么遗漏的吗?

java xml eclipse jaxb maven

2
推荐指数
1
解决办法
7421
查看次数

jaxb2-maven-plugin XJB 中的错误:元素“绑定”的命名空间必须来自架构命名空间“

我试图将不同的名称空间分配给不同的 xsd 文件,并使用 jaxb2-maven 插件来构建由这些 xsd 文件定义的工件。

Maven 无法生成源并出现以下错误:The namespace of element 'bindings' must be from the schema namespace, 'http://www.w3.org/2001/XMLSchema'

这是我的配置:

<jaxb:bindings 
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb 
                http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
version="2.1">

<jaxb:bindings schemaLocation="xsd/TheRequest.xsd" node="/xsd:schema">
    <jaxb:schemaBindings>
        <jaxb:package name="com.package.request" />
    </jaxb:schemaBindings>
</jaxb:bindings>

<jaxb:bindings schemaLocation="xsd/TheResponse.xsd" node="/xsd:schema">
    <jaxb:schemaBindings>
        <jaxb:package name="com.package.response" />
    </jaxb:schemaBindings>
</jaxb:bindings>

</jaxb:bindings>

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
targetNamespace="http://company.services.com" 
xmlns:tns="http://company.services.com" 
elementFormDefault="unqualified">

<xsd:complexType name="FindSomething">
    <xsd:sequence>
        <xsd:element name="TestMode" type="xsd:string" maxOccurs="1" minOccurs="0"/>
        <xsd:element name="Channel" type="xsd:string" maxOccurs="1" minOccurs="1"/>
        <xsd:element name="UserId" type="xsd:string" maxOccurs="1" minOccurs="1"/>
        <xsd:element name="Role" type="xsd:string" maxOccurs="1" minOccurs="0"/>
        <xsd:element name="Format" …
Run Code Online (Sandbox Code Playgroud)

xsd jaxb maven jaxb2-maven-plugin xjb

2
推荐指数
1
解决办法
6599
查看次数

JAXB 解析错误:ArrayIndexOutOfBoundsException

我面临一个问题,但我没有任何线索来解决它!

问题很简单,我从 XSD 文件生成 JAXB 类。(一个真正复杂的)。但是当编组发生时,我得到一个数组索引超出范围:[在此处插入随机负数]

    javax.ejb.EJBException: The bean encountered a non-application exception; nested exception is: 
    java.lang.ArrayIndexOutOfBoundsException: Array index out of range: -11
    at org.apache.openejb.core.ivm.BaseEjbProxyHandler.convertException(BaseEjbProxyHandler.java:358)
    at org.apache.openejb.core.ivm.BaseEjbProxyHandler.invoke(BaseEjbProxyHandler.java:286)
    at com.sun.proxy.$Proxy60.parseXMLFromData(Unknown Source)
    at com.michelin.v2k.services.bo.integration.impl.CasingBufferServiceTestCase.generateDOOMessage(CasingBufferServiceTestCase.java:106)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:76)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:607)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at com.michelin.v2k.V2KAbstractTestCase$1$1.evaluate(V2KAbstractTestCase.java:236)
    at org.junit.rules.RunRules.evaluate(RunRules.java:18)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at …
Run Code Online (Sandbox Code Playgroud)

xml jaxb indexoutofboundsexception jakarta-ee

2
推荐指数
1
解决办法
4500
查看次数

仅在 getter 上使用 XmlTransient 注释?

我正在尝试将 XML 解组为一个我期望应该具有特定字段的对象。但是,我不想将该对象编组到包含它的 XML 中。我喜欢的是类似于这样的:

@XmlRootElement(name = "User")
public class User {

    private String name;

    @XmlTransient
    public String getName() {
        return this.name
    }

    @XmlElement(name = "Name")
    public void setName(String name) {
        this.name = name
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,由于注释冲突,这不起作用,因为我无法将任何其他 XML 注释与@XmlTransient. 我还尝试@XmlTransient在字段本身而不是 getter 上添加注释,并设置了此选项:

XmlAccessorType(XmlAccessType.FIELD)

此外,我将@XmlElement注释保留在设置器上,这对于排除该字段进行编组绝对没有任何作用。

我想保留@XmlElement注释,因为我希望能够将具有不同名称的字段(这里只是大小写差异)翻译成我想要的任何字段。

我也无法删除 getter,因为我确实在应用程序中使用了它。

鉴于此,我不知道此时我的选择是什么,除了编写一个适配器(我可以这样做,但如果有另一种解决方案,我宁愿不使用自定义适配器,因为这个字段)。任何帮助将不胜感激。

java xml jaxb marshalling xmltransient

2
推荐指数
1
解决办法
4872
查看次数

JAXB 覆盖 package-info.java:“命名空间”应该是什么?

我们使用 xjc 生成用于 XML 生成的 JAXB Java 类。一切工作正常,除了我们尝试按照此处所述调整生成的名称空间前缀。由于我们使用的 JAXB 版本,我们陷入了“解决方案 2”,调整 package-info.java。

我们的结构是多个深度导入:根命名空间导入其他命名空间,后者又导入第三个命名空间。

MCVE xsds

root.xsd(导入 other.xsd):

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="root" xmlns:other="other" targetNamespace="root" version="1.0">
    <xs:import namespace="other" schemaLocation="other.xsd" />
    <xs:element name="rootElem">
        <xs:complexType>
            <xs:choice>
                <xs:element ref="rootElem1"/>
            </xs:choice>
        </xs:complexType>
    </xs:element>
    <xs:element name="rootElem1" nillable="false">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="data">
                    <xs:complexType>
                        <xs:choice>
                            <xs:element ref="other:otherElem"/>
                        </xs:choice>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    </xs:schema>
Run Code Online (Sandbox Code Playgroud)

other.xsd(导入third.xsd):

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:other="other" xmlns:third="third" targetNamespace="other" elementFormDefault="qualified" attributeFormDefault="unqualified" version="1.0">
<xsd:import namespace="third" schemaLocation="third.xsd" />
    <xsd:element name="otherElem">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element …
Run Code Online (Sandbox Code Playgroud)

java xml jaxb

2
推荐指数
1
解决办法
1万
查看次数