1

我有这个xml:

<?xml version="1.0" encoding="utf-8"?>
 <ProductItem xmlns="http://providers.natinst.com/pdi-rest/1.0/meta/" urn="urn:product-item:910796-76" url="http://immix-test2.natinst.com/pdi-rest/1.0/en-US/product-item/910796-76.xml">
<partNumber>910796-76</partNumber>
<inventoryItemId>560427</inventoryItemId>
<nicInventoryItemId>765430</nicInventoryItemId>
<name />
<description>LABVIEW CORE 2 SELF-PACED ONLINE TRAINING (6 MONTHS ACCESS)</description>
<isCustomerFacing>true</isCustomerFacing>
<itemType>CE</itemType>
<partType>Training Program</partType>
<bookingsClassName />
<bookingsClassCode />
<lifecyclePhase>Released</lifecyclePhase>
<salesClass />
<firstOrderableDate />
<locale>en-US</locale>
<ngpmProductHierarchy />
<productRevisions url="http://immix-test2.natinst.com/pdi-rest/1.0/en-US/product-item/910796-76/productRevisions.xml" />
<serviceOptionsForProduct url="http://immix-test2.natinst.com/pdi-rest/1.0/en-US/product-item/910796-76/serviceOptionsForProduct.xml" />
<serviceOptionsByService url="http://immix-test2.natinst.com/pdi-rest/1.0/en-US/product-item/910796-76/serviceOptionsByService.xml" />
<productFeatures url="http://immix-test2.natinst.com/pdi-rest/1.0/en-US/product-item/910796-76/productFeatures.xml" />
 </ProductItem>

我正在尝试获取描述标签。这是我的java代码。

package com.ni.apps.elearningrest.client;

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


import javax.xml.bind.annotation.XmlElementWrapper;

@XmlRootElement(name = "ProductItem")
public class DescriptionDTO {

    private String description;
    private String uri;


    @XmlElement(name = "description")
    public String getDescription() {
        return this.description;
    }

    public void setDescription(String description) {
        this.description = description;
    }               
}

但我不断收到“意外元素(uri:”http://providers.natinst.com/pdi-rest/1.0/meta/,local:“ProductItem”)。预期的元素是 <{}ProductItem>" 错误。我可以做些什么来解决这个问题?

4

2 回答 2

2

虽然您可以在@XmlRootEleemnt@XmlElement注释上指定命名空间属性,但我建议使用该@XmlSchema注释来指定默认命名空间。

com/ni/apps/elearningrest/client/package-info.java

@XmlSchema( 
    namespace = "http://providers.natinst.com/pdi-rest/1.0/meta/", 
    elementFormDefault = XmlNsForm.QUALIFIED) 
package com.ni.apps.elearningrest.client;

了解更多信息

于 2012-05-21T19:03:03.127 回答
1

尝试将命名空间添加到 JAXB 注释:

@XmlRootElement(name = "ProductItem", namespace="http://providers.natinst.com/pdi-rest/1.0/meta/")

于 2012-05-21T15:48:43.750 回答