1

名前空間に問題があります。パブリック API (Prestashop) から非整列化する必要があります。この API には、以下のような xlink タイプとして xml があります。

<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 クラスを含む 2 つのパッケージを生成しました。製品リストから ID を指定して、任意の製品のプロパティを取得したいと思います。

@XMLSchema の名前空間を持つ製品がありますが、この名前空間は 1 つのパスに対してのみ静的です。私はそれがそれを行う方法ではないことを知っていました。

以下、私のクライアントクラス。

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

助けてくれませんか?

4

1 に答える 1

0

次のことができます。

@XmlAccessorType(XmlAccessType.FIELD)
public class Product {

    @XmlAttribute
    private int id;

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

詳細については

于 2013-11-27T17:40:00.583 に答える