Commit b487eb56 by Owen Ryan Ang

logic changes when updating xml if existing.

parent da4b5379
......@@ -189,7 +189,8 @@ public class GDEWebServices {
if (!file.exists()) {
response = XMLUtil.generateXML(xml);
} else {
response = XMLUtil.udpateXML(xml);
// response = XMLUtil.udpateXML(xml);
response = XMLUtil.generateXML(xml);
}
} catch (Exception e) {
return Response.status(500).entity("Fail").build();
......
package com.svi.webgde.restservice.utils;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringReader;
import java.nio.file.Files;
import java.nio.file.Paths;
......@@ -108,8 +110,17 @@ public class XMLUtil {
transformer.setOutputProperty(OutputKeys.STANDALONE, "no");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
Result result = new StreamResult(xml.getOutputDir());
transformer.transform(source, result);
try (OutputStream outputStream = new FileOutputStream(xml.getOutputDir())) {
// Set the output stream for the StreamResult
Result result = new StreamResult(outputStream);
// Perform the transformation
transformer.transform(source, result);
} catch (IOException e) {
// Handle IOException
e.printStackTrace();
}
return new String(Files.readAllBytes(Paths.get(xml.getOutputDir())));
}
......@@ -158,25 +169,38 @@ public class XMLUtil {
Node imagename = document.getElementsByTagName("imagename").item(imagenames.get(xml.getImageName()));
Node xmlSubRecord = imagename.getParentNode().getLastChild().getPreviousSibling();
// xml.getFields().forEach((key, value) -> {
for (Map.Entry<String, String> entry : xml.getFields().entrySet()) {
for (int i = 0; i < xmlSubRecord.getChildNodes().getLength(); i++) {
String key = entry.getKey();
String value = entry.getValue();
if (xmlSubRecord.getChildNodes().item(i).getNodeType() == Node.ELEMENT_NODE) {
if (xmlSubRecord.getChildNodes().item(i).getAttributes().getNamedItem("no").getNodeValue()
.equals(key)) {
for (int j = 0; j < xmlSubRecord.getChildNodes().item(i).getChildNodes().getLength(); j++) {
if (xmlSubRecord.getChildNodes().item(i).getChildNodes().item(j)
.getNodeType() == Node.ELEMENT_NODE) {
xmlSubRecord.getChildNodes().item(i).getChildNodes().item(j).setTextContent(value);
}
}
}
}
}
}
String key = entry.getKey();
String value = entry.getValue();
boolean fieldExists = false;
// Use XPath to find the field with the same "no" attribute
XPath xPath = XPathFactory.newInstance().newXPath();
String expression = String.format(".//field[@no='%s']", key);
try {
Node existingField = (Node) xPath.compile(expression).evaluate(xmlSubRecord, XPathConstants.NODE);
if (existingField != null) {
// Update the value of the existing field
existingField.getFirstChild().setTextContent(value);
fieldExists = true;
}
} catch (XPathExpressionException e) {
e.printStackTrace(); // Handle the exception appropriately in your production code
}
// If the field does not exist, create and append a new one
if (!fieldExists) {
Element xmlField = document.createElement("field");
Element valueField = document.createElement("value");
valueField.appendChild(document.createTextNode(value));
xmlField.setAttribute("no", key);
xmlField.appendChild(valueField);
xmlSubRecord.appendChild(xmlField);
}
}
// });
} else {
NodeList xmlRecords = document.getElementsByTagName("record");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment