after having a good look online, I am struggling with this one. Basically, I call an API, it returns XML, this xml requires to be updated, then sent back to the api. The code I currently have is :
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;
}
However, doc returns [#document: null] - which means the document is actually there, I can make the amendments, but I cannot seem to get the xml from the doc as this is required to bepassed into another method to upload the xml to the API, but i cannot figure out how!!
I figured this out....
using the below code:
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;
I was able to transform the contents to a string and upload the string as I required...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.