简体   繁体   English

JAXB Unmarshalling Error,没有命名空间的package-info文件?

[英]JAXB Unmarshalling Error, package-info file for no namespace?

I am attempting to Unmarshall XML from a file using JAXB. 我试图使用JAXB从文件解组XML。 I also have a web service. 我也有一个网络服务。 I want to use no namespace, not the namespace defined by the web service. 我想不使用命名空间,而不是Web服务定义的命名空间。

I am getting the following error: 我收到以下错误:

Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"CustomerName"). Expected elements are <{http://www.ws.NET}CustomerName>

I think that the solution is to change the package-info.java file to use no namespace. 我认为解决方案是将package-info.java文件更改为不使用命名空间。 Is this the correct approach? 这是正确的方法吗? and what changes should I make to this file? 我应该对这个文件做些什么改变? The package-info.java file looks as follows: package-info.java文件如下所示:

@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.ws.NET", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package mynewpackage;

Thanks for your help 谢谢你的帮助

Changing QUALIFIED to UNQUALIFIED will help if the top-level element (corresponding to the @XmlRootElement annotated class) is in the target namespace but its children aren't, eg 更改QUALIFIEDUNQUALIFIED会帮助,如果(对应于顶级元素@XmlRootElement注释类) 在目标命名空间,但它的孩子都没有,如

<ns:Response xmlns:ns="http://www.ws.NET">
  <CustomerName>Ian</CustomerName>
</ns:Response>

but this won't help if the top level (in this case Response ) element is also unqualified. 但如果顶级(在本例中为Response )元素也不合格,这将无济于事。

When you have a JAXB model that is annotated to be namespace qualified and you want to unmarshal XML that isn't there are a couple of options. 如果您有一个注释为命名空间限定的JAXB模型,并且您想要解组不具有多个选项的XML。

If You Can Change the Model 如果您可以更改模型

You may just need to remove the @XmlSchema annotation from the package-info class. 您可能只需要从package-info类中删除@XmlSchema注释。 If there are no other package level annotations you could remove the whole class. 如果没有其他包级别注释,则可以删除整个类。 Note that namespace information may also be specified on other annotations such as @XmlElement . 请注意,也可以在其他注释(例如@XmlElement上指定名称空间信息。

If You Can't Change The Model 如果您无法更改模型

If you can't change the JAXB model to remove the namespace qualification then you can leverage a SAX XMLFilter to apply a namespace to the XML as it's being read. 如果您无法更改JAXB模型以删除命名空间限定,那么您可以利用SAX XMLFilter在读取XML时将命名空间应用于XML。

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

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