2015-04-28 160 views
0

在网上很好看,我正在努力与这一个。基本上,我调用一个API,它返回XML,这个XML需要更新,然后发送回api。我公司目前拥有的代码是:“[#document:null]”问题与XML解析

public string update(){ 

String url = GeneralProps.getProperty("XMLUpdateURL")+testCaseID+"/block/include/data/no"; 
ArrayList<String> tempArray = HttpHelper.sendAuthGetRequest(url, GeneralProps.getProperty("Username"), GeneralProps.getProperty("Password")); 

     Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader(tempArray.get(1).toString()))); 

    XPathExpression expr = xpath.compile("//Step["+ stepNo + "]/"+ nodeName   +"/text()"); 
     Object result = expr.evaluate(doc, XPathConstants.NODESET); 
     NodeList nodes = (NodeList) result; 
     for (int i = 0; i < nodes.getLength(); i++) { 
      System.out.println(nodes.item(i).getNodeValue()); 
      nodes.item(i).setNodeValue(newNodeValue); 
      System.out.println(nodes.item(i).getNodeValue()); 

     } 

    return doc; 

    } 

然而,DOC返回[#document:空] - 这意味着该文件实际上是在那里,我可以作出适当修改,但我似乎无法从文档获取XML因为这是需要绕过到另一种方法上传XML到API,但我无法弄清楚如何!

回答

0

我想通了这一点....

使用下面的代码:

Transformer transformer = TransformerFactory.newInstance().newTransformer(); 
     transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 
     StreamResult sResult = new StreamResult(new StringWriter()); 
     DOMSource source = new DOMSource(doc); 
     transformer.transform(source, sResult); 
     String xmlString = sResult.getWriter().toString();  

     return xmlString; 

我能够将内容转换为字符串,并上载该字符串,因为我需要...