【问题标题】:JAXB + JERSEY. How to unmarshall multiple namesapacesJAXB + 泽西岛。如何解组多个命名空间
【发布时间】:2013-11-27 17:16:32
【问题描述】:

我在命名空间方面遇到了问题。 我需要从公共 api (Prestashop) 中解组。 这个 api 有 xml 作为 xlink 类型,如下所示:

<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<products>
<product id="1" xlink:href="http://localhost:8080/prestashop/api/products/1"/>
<product id="2" xlink:href="http://localhost:8080/prestashop/api/products/2"/>
</products>
</prestashop>

每个产品的api是:

<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<product>
<id>
<![CDATA[ 1 ]]>
</id>
<id_manufacturer xlink:href="http://localhost:8080/prestashop/api/manufacturers/1">
<![CDATA[ 1 ]]>
</id_manufacturer>
<id_supplier xlink:href="http://localhost:8080/prestashop/api/suppliers/1">
<![CDATA[ 1 ]]>
</id_supplier>
<id_category_default xlink:href="http://localhost:8080/prestashop/api/categories/3">
<![CDATA[ 3 ]]>
</id_category_default>
</product>
</prestashop>

我生成了两个包,其中包含每个 XML 的 pojo 类。 我想从产品列表中获取给定 id 的任何产品的属性。

我有一个产品,其名称空间位于 @XMLSchema 中,但此名称空间仅针对一个路径是静态的。我知道这不是这样做的方法。

下面是我的客户端类。

public class ClientPrestashop{  

    public static final Logger log = Logger.getLogger(ClientPrestashop.class.getCanonicalName());
    private final String pass = "LA4DKY4AVJUODHCX0H0XH8E7EROV05J6";
    private final String url="http://LA4DKY4AVJUODHCX0H0XH8E7EROV05J6@localhost:8080/prestashop/api/";

    public Object getPrestashopPackageProducts(String path, Class<?> clase) throws JAXBException, Exception{

        ClientConfig config= new DefaultClientConfig();
        Client client = Client.create(config);
        client.addFilter(new HTTPBasicAuthFilter(pass, ""));

        WebResource webresource = client.resource(url + path);
        ClientResponse response = webresource.type(MediaType.APPLICATION_XML).get(ClientResponse.class);

        mostrar(response.getStatus());

        JAXBContext jaxbContext = JAXBContext.newInstance(clase);

        //Crear XMLFilter
        XMLFilter filter = new NamespaceFilter(url+path,true);

        //El XMLReader será encapsulado en nuestro XMLFilter.
        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setNamespaceAware(true);
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();
        filter.setParent(xr);

        //Modificar UnmarshalerHandler como ContentHandler en XMLFilter 
        Unmarshaller unmarshall = jaxbContext.createUnmarshaller();
        UnmarshallerHandler unmarshallerHandler = unmarshall.getUnmarshallerHandler();
        filter.setContentHandler(unmarshallerHandler);

        //Parse del XML
        InputSource sr = new InputSource(response.getEntityInputStream());
        filter.parse(sr);
        Object presta = unmarshallerHandler.getResult();

        return presta;
    }

这里有代码:https://github.com/JorgeP86/webservice.git

你能帮帮我吗?

【问题讨论】:

  • 我认为您可以在生成的 package-info.java 中调整预期的架构 url。里面应该有注释。如果没有,您可以使用对限定名称限制不那么严格的 Castor。
  • 谢谢你,贾梅尔汤姆斯。我的包信息没有从给定的 xml 模式中自动生成。我必须生成一个新包,然后填写以下代码@XmlSchema(namespace="http://LA4DKY4AVJUODHCX0H0XH8E7EROV05J6@localhost:8080/prestashop/api/products/1") elementFormDefault = XmlNsForm.QUALIFIED,。然后我用 JAXB 生成 POJO 类。
  • 但是如果我想要这个命名空间只适用于一种产品.../products/2 我需要更改它。如何获取所有命名空间?

标签: java xml namespaces jaxb xlink


【解决方案1】:

您可以执行以下操作:

@XmlAccessorType(XmlAccessType.FIELD)
public class Product {

    @XmlAttribute
    private int id;

    @XmlAttribute(namespace="http://www.w3.org/1999/xlink")
    private String href;
}

更多信息

【讨论】:

    猜你喜欢
    • 2018-07-06
    • 1970-01-01
    • 2014-10-06
    • 1970-01-01
    • 1970-01-01
    • 2011-08-02
    • 1970-01-01
    • 2014-11-30
    • 1970-01-01
    相关资源
    最近更新 更多