I have XML that have no fixed format I have following code using org.w3c.dom to dynamically set value to same XML.
public String generateXML(String[] tags,String[] tagValues,String xmlfilePath){
String strXML = "";
try{
if(tags == null || tagValues == null || xmlfilePath == null){
}else{
File file = new File(xmlfilePath);
if (file.exists()){
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
org.w3c.dom.Document doc = builder.parse(file);
NodeList nodeList = doc.getElementsByTagName("*");
int k =0;
for (int i=0; i<nodeList.getLength(); i++) {
Node node = (Node)nodeList.item(i);
if(node.getNodeName().trim().equalsIgnoreCase(tags[k])){
node.setTextContent(tagValues[k]);
k++;
}
}
DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation();
LSSerializer lsSerializer = domImplementation.createLSSerializer();
strXML = lsSerializer.writeToString(doc);
}else{
}
}
}catch (Exception e) {
e.printStackTrace();
}
return strXML;
}
But its not working in some older version of JDK so i want to do the same with JDOM.
How is it possible ? Each example needs tag name but i want create common method.