我通过构建输入SOAP
并作为请求发送来从android调用.net XML
Web服务。接收响应为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。还有其他替代解决方案吗?如果有,请发表你的答案。
答案 0 :(得分:2)
是的,请查看SimpleXML
您可以根据XML文档结构定义bean,并使用getters / setters来操作值。
@Root(name = "XMLINPUT")
class MyBean {
@Element(name = "CNNREFF")
private String child1;
@Element(name = "CTTREFF")
private String child2;
}