//classpath: dom4j-1.6.1.jar
String csvFile = "C:/mstrblog/deploy.csv";
String line = "";
String cvsSplitBy = ",";
String str;
String strXmlToken1,strXmlToken2;
String node0, node1, node2, node3, node4, node5;
String node6, node7, node8, node9, node10;

try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
while ((line = br.readLine()) != null) {
String[] rowData = line.trim().split(cvsSplitBy);
str = "";
strXmlToken1 = "";
strXmlToken2 = ""; 
for (int c = 0; c < rowData.length; c++) {
if ((c == 11) && (rowData[11].equals("1"))) {

node0 = ((rowData[0].trim().equals("")) ? "" : ("/" + rowData[0]));
node1 = ((rowData[1].trim().equals("")) ? "" : ("/" + rowData[1]));
node2 = ((rowData[2].trim().equals("")) ? "" : ("/" + rowData[2]));
node3 = ((rowData[3].trim().equals("")) ? "" : ("/" + rowData[3]));
node4 = ((rowData[4].trim().equals("")) ? "" : ("/" + rowData[4]));
node5 = ((rowData[5].trim().equals("")) ? "" : ("/" + rowData[5]));
node6 = ((rowData[6].trim().equals("")) ? "" : ("/" + rowData[6]));
node7 = ((rowData[7].trim().equals("")) ? "" : ("/" + rowData[7]));
node8 = ((rowData[8].trim().equals("")) ? "" : ("/" + rowData[8]));
node9 = ((rowData[9].trim().equals("")) ? "" : ("[@Name='" + rowData[9] + "']"));
node10 = ((rowData[10].trim().equals("")) ? "" : (",\"Value\",\"" + rowData[10] + "\");")); 
strXmlToken1 = node0 + node1 + node2 + node3 + node4 + node5 + node6 + node7 + node8 + node9;
strXmlToken2 = rowData[10];

String token_xpath=strXmlToken1;
String token_name="Value";
String token_value=strXmlToken2;
org.dom4j.io.SAXReader reader = new org.dom4j.io.SAXReader();
org.dom4j.Document document = reader.read(inputFile);
org.dom4j.Element classElement = document.getRootElement();
List<org.dom4j.Node> nodes = document.selectNodes(token_xpath);
for (org.dom4j.Node node : nodes) {
org.dom4j.Element element = (org.dom4j.Element) node;
element.setAttributeValue(token_name, token_value);
}
org.dom4j.io.XMLWriter writer = new org.dom4j.io.XMLWriter(new FileWriter("C:/mstrblog/duplicateProject.xml"));
writer.write(document);
writer.close();
}
}
}
printOut("XML Updated Successfully");
} catch (Exception e) {
printErr("Error reading properties file: " + e.getLocalizedMessage());
}

Leave a Reply

Your email address will not be published. Required fields are marked *