TestMain.java 3.07 KB
package th.co.ais.ssbsrfc.control;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.util.Scanner;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPMessage;

import th.co.ais.ssbsrfc.instance.AdjustmentResponseIns;
import th.co.ais.ssbsrfc.message.MessageParser;

@XmlAccessorType(XmlAccessType.FIELD)




public class TestMain {

	public static Object fromSoap(String strSoap, Class objClass) {
		Object obj = null;
		
		try {
			SOAPMessage message = MessageFactory.newInstance().createMessage(null, new ByteArrayInputStream(strSoap.getBytes()));
			Unmarshaller unmarshaller = JAXBContext.newInstance(objClass).createUnmarshaller();
			SOAPBody body = message.getSOAPBody();
			if(body != null && !body.getTextContent().trim().equals("")) {
				obj = unmarshaller.unmarshal(body.extractContentAsDocument());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return obj;
	}
	
	public static void main(String[] args) throws Exception {
		
//		String msg = "";
//        XMLInputFactory xif = XMLInputFactory.newFactory();
//        StreamSource xml = new StreamSource("D:\\car.xml");
//        XMLStreamReader xsr = xif.createXMLStreamReader(xml);
//        xsr.nextTag();
//       
//        while(!xsr.getLocalName().equals("ResultHeader")) {
//            if(xsr.getLocalName().equals("ResultHeader")){
//            	
//            }
//        	xsr.nextTag();
//            System.out.println(xsr.getLocalName());
//        }
//       
//        JAXBContext jc = JAXBContext.newInstance(Customer.class);
//        Unmarshaller unmarshaller = jc.createUnmarshaller();
//        JAXBElement<Customer> jb = unmarshaller.unmarshal(xsr, Customer.class);
//        xsr.close();
// 
//        Customer customer = jb.getValue();
//        System.out.println("Version:"+customer.Version);
//        System.out.println("ResultCode:"+customer.ResultCode);
//        System.out.println("MsgLanguageCode:"+customer.MsgLanguageCode);
//        System.out.println("ResultDesc:"+customer.ResultDesc);
//        System.out.println("AcctKey:"+customer.AcctKey);
        
        
       /* System.out.println("BalanceID:"+customer.AdjustmentInfo.get(0).getBalanceID());
        System.out.println("BalanceType:"+customer.AdjustmentInfo.get(0).getBalanceType());
        System.out.println("BalanceTypeName:"+customer.AdjustmentInfo.get(0).getBalanceTypeName());
        System.out.println("CurrencyID:"+customer.AdjustmentInfo.get(0).CurrencyID);
        System.out.println("NewBalanceAmt:"+customer.AdjustmentInfo.get(0).NewBalanceAmt);
        System.out.println("OldBalanceAmt:"+customer.AdjustmentInfo.get(0).OldBalanceAmt);*/
        
		String value = new Scanner(new File("D:\\car.xml")).useDelimiter("\\Z").next();
		
		
		AdjustmentResponseIns ins = (AdjustmentResponseIns)MessageParser.fromSoap(value, AdjustmentResponseIns.class);
		
		System.out.println(MessageParser.toJson(ins));
		
	}
}