2016-10-14 26 views
2

我必須將Excel文件中的數據作爲XML發送到Web服務。表中的數據看起來有點像這樣: see example of table here!將包含XML的字符串添加到使用Java的SOAPElement中

第一行始終包含列的數據的XML標記。大多數數據列只包含字符串,但有些包含xml。那些子節點對Web服務接受數據很重要。

我正在使用SOAP,爲每列創建一個新的SOAPElement,其中每一行都是新的SOAP請求。

SOAPElement newElement = body.addChildElement(tagForThisColumn); 
newElement.addTextNode(stringValueOfCell); 

這完全適用的字符串值,但逃脫的SOAPElement所有「<」和「>」使用XML細胞。

我已經搜索的答案,發現類似問題的一些解決方案,但沒有適合我的..

回答

2

你應該節點添加到的SOAPElement。 我已經做了在SOAP頭添加SAML令牌到的SOAPElement類似的東西:

SOAPHeader header = envelope.addHeader(); 

SOAPElement security = header.addChildElement("Security", "wsse", 
    "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"); 

DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); 

documentBuilderFactory.setNamespaceAware(true); 

DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); 
File file = new File("your-file.xml"); 

/* parse existing file to DOM */ 
Document document = documentBuilder.parse(new FileInputStream(file)); 

Document securityDoc = security.getOwnerDocument(); 
Node newNode = securityDoc.importNode(document.getFirstChild(), true); 

//Add the Node 
security.appendChild(newNode); 

我希望這是有益的。

相關問題