All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aliyuncs.unmarshaller.XmlUnmashaller Maven / Gradle / Ivy

Go to download

Aliyun Open API SDK for Java Copyright (C) Alibaba Cloud Computing All rights reserved. 版权所有 (C)阿里云计算有限公司 http://www.aliyun.com

There is a newer version: 4.7.3
Show newest version
package com.aliyuncs.unmarshaller;

import java.io.StringReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

import com.aliyuncs.AcsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.HttpResponse;

/**
 * @author VK.Gao
 * @date 2018/04/11
 */
public class XmlUnmashaller implements Unmarshaller {

    @Override
    public  T unmarshal(Class clazz, HttpResponse httpResponse) throws ClientException {
        String xmlContent = httpResponse.getHttpContentString();
        try {
            JAXBContext jc = JAXBContext.newInstance(clazz);
            javax.xml.bind.Unmarshaller unmarshaller = jc.createUnmarshaller();
            T xmlPojo = (T)unmarshaller.unmarshal(new StringReader(xmlContent));

            return xmlPojo;
        } catch (JAXBException e) {
            throw newUnmarshalException(clazz, xmlContent, e);
        }
    }

    private ClientException newUnmarshalException(Class clazz, String xmlContent, Exception e) {
        return new ClientException("SDK.UnmarshalFailed",
            "unmarshal response from xml content failed, clazz = " + clazz.getSimpleName() + ", origin response = " + xmlContent, e);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy