简体   繁体   English

JAXB 命名空间前缀缺失

[英]JAXB namespace prefixes missing

I have generated Java classes from XSD, all works fine from a unmarshalling point of view.我从 XSD 生成了 Java 类,从解组的角度来看,一切正常。

However, when I marshall from JAXB classes I get the following:但是,当我从 JAXB 类编组时,我得到以下信息:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<message xmlns="http://poc.cmc.com/ScreenLayout">
    <Data>
        <Type>Sample</Type>
     . . .
</message>

But I need但是我需要

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns0:message xmlns:ns0="http://poc.cmc.com/ScreenLayout">
    <ns0:Data>
        <ns0:Type>Sample</ns0:Type>
    . . .

how can I control that from Java?如何从 Java 控制它?

Thanks a lot非常感谢

You can use the @XmlSchema annotation on a package-info class to assign a prefix to the namespace:您可以在package-info class 上使用@XmlSchema注释来为命名空间分配前缀:

@XmlSchema(
    namespace = "http://poc.cmc.com/ScreenLayout",
    elementFormDefault = XmlNsForm.QUALIFIED,
    xmlns={@XmlNs(prefix="ns0", namespaceURI="http://poc.cmc.com/ScreenLayout")})    
package your.package;


import javax.xml.bind.annotation.*;

Cant post this as a comment!不能将此作为评论发布!

because the consuming application is very dumb and needs the prefix因为消费应用程序非常愚蠢并且需要前缀

In that case the dumb application is not really consuming xml.在这种情况下,愚蠢的应用程序并没有真正消耗 xml。 Take a look at this link http://bdoughan.blogspot.com/2010/08/jaxb-namespaces.html and play with the namespace options.看看这个链接http://bdoughan.blogspot.com/2010/08/jaxb-namespaces.html并使用命名空间选项。 Specifically具体来说

@XmlSchema (
   xmlns = {
         @javax.xml.bind.annotation.XmlNs(prefix = "ns1", namespaceURI="http:test"),
         @javax.xml.bind.annotation.XmlNs(prefix = "xsd", namespaceURI="http:www.w3.org2001XMLSchema")
   },
   namespace = "http:test",
   elementFormDefault = XmlNsForm.UNQUALIFIED,
   attributeFormDefault = XmlNsForm.UNSET
)

used in a package-info.java file.在 package-info.java 文件中使用。

@XmlType(namespace="http://www.example.org/type")

Used on a class declaration用于 class 声明

@XmlElement(namespace="http://www.example.org/property")

Used on a property.用于属性。

Some combination or only one of these options may give you what you want.某些组合或仅其中一个选项可能会给您想要的东西。 However you should understand that you're fighting an uphill battle when you move from valid xml to xml that must contain a specific namespace prefix on all elements.但是,您应该明白,当您从有效的 xml 移动到 xml 时,您正在打一场艰苦的战斗,必须在所有元素上包含特定的命名空间前缀。

According to XML spec both xml's are the same, as xmlns="" defines default namespace which applies to current and all child elements.根据 XML 规范,两个 xml 是相同的,因为 xmlns="" 定义了适用于当前和所有子元素的默认命名空间。 XML parsers should give you the same DOM or SAX in both cases XML 解析器在这两种情况下都应该为您提供相同的 DOM 或 SAX

I did the following steps and everything works.我做了以下步骤,一切正常。 Basically I've compiled form the wsdl or whatever schema file you have.基本上我已经从 wsdl 或您拥有的任何模式文件中编译。

  1. Download from https://repo1.maven.org/maven2/com/sun/xml/bind/jaxb-ri/ (I've used version 2.3.4)https://repo1.maven.org/maven2/com/sun/xml/bind/jaxb-ri/下载(我使用的是 2.3.4 版)
  2. With the following command:使用以下命令:
xjc.sh -wsdl -npa -mark-generated -d src/main/java -p your.package.hierarchy src/main/resources/wsdl/*

I had wsdl files and that's the reason for the first flag, the -npa is to add the namespace information in the annotation rather than on a package-info.java since for some reason that wasn't working for me.我有 wsdl 文件,这就是第一个标志的原因,-npa 是在注释中而不是在 package-info.java 中添加命名空间信息,因为由于某种原因这对我不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM