append/insert data to xml file
hi,
i would like to append my xml file/insert new data to xml file. i use appendChild but it never works. I also want to ask about methode isSupported in Node.
the method is boolean isSupported(String feature, String version)
but i don't know what they mean by feature here, can someone explain about this method too.thank you
this is my xml file:
<?xml version="1.0" encoding="iso-8859-1"?>
<logins>
<user id="x" system="y" envlib="a" env="2" prefix="SPW" depot="NL2" proddiv="FD" lang="NED" />
<user id="r" system="a" envlib="aO" env="2" prefix="SPW" depot="NL2" proddiv="FD" lang="NED" />
<user id="t" system="s7" envlib="w" env="4" prefix="SPR" depot="ASD" proddiv="AV" lang="NED" />
</logins>
this is my code:
String strFileName = getServletContext().getRealPath("/xml/user/users.xml");
File file = new File(strFileName);
Document doc = null;
try{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(file);
}
catch(IOException e){
System.out.println(e);
}
Element root = doc.getDocumentElement();
String nodeValue = root.getNodeName();
out.println(root.getParentNode().getNodeName());
Element newElement = doc.createElement("user");
newElement.setAttributeNode(doc.createAttribute("i d"));
newElement.setAttribute("id", "andhiez");
newElement.setAttributeNode(doc.createAttribute("s ystem"));
newElement.setAttribute("system", "andhiez");
newElement.setAttributeNode(doc.createAttribute("e nvlib"));
newElement.setAttribute("envlib", "andhiez");
newElement.setAttributeNode(doc.createAttribute("e nv"));
newElement.setAttribute("env", "andhiez");
newElement.setAttributeNode(doc.createAttribute("p refix"));
newElement.setAttribute("prefix", "andhiez");
newElement.setAttributeNode(doc.createAttribute("d epot"));
newElement.setAttribute("depot", "andhiez");
newElement.setAttributeNode(doc.createAttribute("p roddiv"));
newElement.setAttribute("proddiv", "andhiez");
newElement.setAttributeNode(doc.createAttribute("l ang"));
newElement.setAttribute("lang", "andhiez");
root.appendChild(newElement);
|