2
我通過構建輸入XML
並作爲請求發送,從android調用.net SOAP
Web服務。接收回應爲XML
字符串並解析它。以上所有工作正常。創建xml字符串的替代解決方案
爲構建XML字符串我正在追蹤使用DocumentBuilderFactory
例的方法是這樣
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.newDocument();
Element rootElement = document.createElement("XMLINPUT");//Parent Node of the xml
document.appendChild(rootElement);
Element childElementCNN = document.createElement("CNNREFF");//Child Node
rootElement.appendChild(childElementCNN);
childElementCNN .appendChild(document.createTextNode(strCNN));
Element childElementCTT = document.createElement("CTTREFF");//Child Node
rootElement.appendChild(childElementCTT);
childElementCTT.appendChild(document.createTextNode(strCTT));
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
Properties outFormat = new Properties();
outFormat.setProperty(OutputKeys.INDENT, "yes");
outFormat.setProperty(OutputKeys.METHOD, "xml");
outFormat.setProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
outFormat.setProperty(OutputKeys.VERSION, "1.0");
outFormat.setProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperties(outFormat);
DOMSource domSource = new DOMSource(document.getDocumentElement());
OutputStream output = new ByteArrayOutputStream();
StreamResult result = new StreamResult(output);
transformer.transform(domSource, result);
String strInputXML = output.toString(); //Storing into a string
我想知道在android系統構建XML,因爲我想用同樣的功能來構建不同的XML。還有其他解決方案嗎?如果有請發佈您的答案。
是的。除此之外,你真的有問題嗎? – njzk2
@ njzk2是的,我需要一個替代解決方案來構建XML字符串。 –
@ njzk2所以請發佈任何你知道的。 –