gpt4 book ai didi

java - 为生成的类添加 xml 根元素注释的方法?

转载 作者:行者123 更新时间:2023-11-30 08:02:45 25 4
gpt4 key购买 nike

我已经从 WSDL 文件生成了 JAXB 类,我想做的是将 XML 转换为 Java 对象。下面是生成的 JAXB 类示例:

XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "GetProductListResponse", propOrder = {
"productList",
"productListDate",
"failed",
"failedDescription"
})
public class GetProductListResponse {

@XmlElementRef(name = "ProductList", namespace = "http://productService.productsdata", type = JAXBElement.class, required = false)
protected JAXBElement<ArrayOfProductListDetail> productList;
@XmlElementRef(name = "ProductListDate", namespace = "http://productService.productsdata", type = JAXBElement.class, required = false)
protected JAXBElement<String> productListDate;
@XmlElement(name = "Failed")
protected boolean failed;
@XmlElement(name = "FailedDescription", required = true, nillable = true)
protected String failedDescription;

...
}

我需要转换为 GetProductListResponse 对象的 XML 示例存储在 products.xml 文件中,如下所示:

<GetProductListResult xmlns="http://productService.productsdata">
<ProductList>
<ProductListDetail>
<ProductName>SomeProductName</ProductName>
<ProductCost>9,45</ProductCost>
</ProductListDetail>
<ProductListDate>09.09.2015</ProductListDate>
<Failed>false</Failed>
<FailedDescription/>
</ProductList>
</GetProductListResult>

convertXmlProductsTest 方法中设置转换调用 - 为此目的使用 jaxb unmarshaller:

public class ProductHandler {

public static GetProductListResponse convertXmlProductsTest(){
try {
JAXBContext jaxbContext = JAXBContext.newInstance(GetProductListResponse.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
GetProductListResponse retval = (GetProductListResponse) jaxbUnmarshaller.unmarshal(new File("products.xml"));

return retval;
} catch (JAXBException ex) {
Logger.getLogger(ProductMockWs.class.getName()).log(Level.SEVERE, null, ex);
}
throw new UnsupportedOperationException("XML to Java object conversion failed.");
}
}

问题是生成的 JAXB 类 GetProductListResponse 不包含 @XmlRootElement 注释,因此此转换失败并显示著名的错误消息 javax.xml.bind。 UnmarshalException:意外的元素......预期的元素是......

当我手动添加 @XmlRootElement 注释到 GetProductListResponse 类,并将其设置为:

@XmlRootElement(name="GetProductsListResult")
public class GetProductListResponse { ...}

转换成功。

问题:有什么方法可以从该类外部为生成的类 (GetProductListResponse ) 设置 @XmlRootElement 吗?

我想避免自定义生成的类,我不想更改 WSDL 定义。我还阅读了有关设置运行时注释的信息,但我想避免使用任何 Java 字节码操纵器(如 Javassist)。

最佳答案

    JAXBContext jaxbContext = JAXBContext.newInstance(GetProductListResponse.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

JAXBElement<GetProductListResponse> root = jaxbUnmarshaller.unmarshal(new StreamSource(
file), GetProductListResponse.class);
GetProductListResponse productListResponse = root.getValue();

对于缺少@XmlRootElement,这篇文章对我帮助很大。看一看:

http://www.source4code.info/2013/07/jaxb-marshal-unmarshal-with-missing.html

你可以在这里找到你的错误,看看你能做什么!希望能帮助到你 !

关于java - 为生成的类添加 xml 根元素注释的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36663402/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com