2016-12-14 90 views
0

我是WSO2 API Manager的新手。我想在java寫customhandler類,也是我得到了有效載荷的內容如下,將jsonObject轉換爲json或獲取xml元素

Headers : 
Body : <soapenv:Body xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"/> 
payload content 
<?xml version='1.0' encoding='utf-8'?> 
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> 
<soapenv:Body> 
    <jsonObject> 
     <?xml-multiple xxx?> 
     <xxx>cust1</xxx> 
     <xxx>cust2</xxx> 
     <first>0000</first> 
     <last>0000</last> 
     <?xml-multiple abc?> 
     <abc>val1</abc> 
     <pqr>000,000</pqr> 
    </jsonObject> 
</soapenv:Body> 
</soapenv:Envelope> 

我送JSON請求在內部通過API經理XML轉換爲above.I我在處理面臨的問題之上XML並獲取上例中cust1的第一個元素xxx的值。 1)任何人都可以幫助我發佈提取價值cust1的Java代碼。

我的JSON POST請求如下,

{ 
"xxx": [ 
    "cust1", 
    "cust2" 
], 
"first": "0000", 
"last": "0000", 
"abc": [ 
    "val1" 
], 
"pqr": "000,000" 
} 

2)我應該轉換XML到JSON?我怎樣才能做到這一點。

回答

1

我已經理清了這個問題。我想發佈解決方案,以便可以幫助其他人。

import org.apache.axiom.soap.SOAPBody; 
import org.apache.axiom.soap.SOAPEnvelope; 
import org.apache.axiom.om.OMElement; 

org.apache.axis2.context.MessageContext axisCtx = ((Axis2MessageContext)messageContext).getAxis2MessageContext(); 
    try{ 
     RelayUtils.buildMessage(axisCtx); 
    } 
    catch (IOException ex){ 
     log.error("Error occurred while building the message", ex); 
    } catch (XMLStreamException ex) { 
     log.error("Error occurred while building the message", ex); 
    } 
    System.out.println("payload content:\n"+ axisCtx.getEnvelope()); 

    //org.apache.synapse.commons.json.JsonStreamBuilder 
    SOAPEnvelope msg = axisCtx.getEnvelope(); //get the SOAP Message envelope 
    javax.xml.namespace.QName name=new javax.xml.namespace.QName("xxx"); 
    SOAPBody child = msg.getBody(); 

    OMElement elem = child.getFirstElement().getFirstChildWithName(name); 
    System.out.println("\n name of element "+elem.getText());