Saturday, 15 June 2013

How to append an existing XML file using DOM without overwriting the existing data? in java -


I can already add XML data of the existing data, I am very confused about this, Let's say my code is the new document document (). NewDocument (); // db.newDocument (); // Create document element root = doc.createElement ("employee")

  document doc = document builder factor. Experience (). NewDocumentBuilder ()); // cretae elements doc.appendChild (root); Comment cmt = doc.create Comment ("Employee Details"); // add a comment to xml root.appendChild (cmt); Element employee = Doctor.Scent element ("employee"); // Create Element //employee.appendChild(doc.) Root.appendChild (employee); Attr genderAttr = doc.createAttribute ("Gender"); System.out.print ("Enter your gender:"); String gender = br.readLine (); GenderAttr.setValue (Gend); Employee.setAttributeNode (genderAttr); System.out.print ("Enter First Name:"); String child = br.readLine (); Element FName = doc.createElement ("first name"); FName.appendChild (doc.createTextNode (child)); // set xml text worker .appendChild (FName); System.out.print ("Enter Last Name:"); String child 1 = breadlines (); Element LName = doc.createElement ("lastname"); LName.appendChild (doc.createTextNode (child1)); Employee.appendChild (LName); //root.appendChild(employee); //doc.appendChild(root); // Transformerfifferent TF = TransformerFeature to write on file / screen New Intens (); Transformer tr = TF Nuu Transformers (); Tr.setOutputProperty (OutputKeys.INDENT, "Yes"); DOMSource source = new DOMSource (doc); // source // file store order = new file ("src" + file. Separator + "XMLPercing" + file. Separator + "XMLPERSSE 1.xml"); // Get the file StreamResult res = new stream result (new file ("src" + file. Separator + "XMLPercing" + file. Separator + "XMLPerse 1.xml"); // Destination Tr. Transsoform (source, race); // write on file   

and I can easily parse and update but I do not understand how can I add the previous data Please help me

It is very easy to say, you want to add your XML to the new employee . Root nodelight root list = doc.getElementsByName ("employee") is used by simply using getElementsByName () as

  // instead of creating a new root Will be able to; Node root = rootList.item (0); Element employee = Doctor.Scent element ("employee"); // Create new element root. AppendChild; //   

to be attached as before document.getElementById () method can be used as well if any element The identifier is assigned to normally attachment ()

edit:

(Sample code added)
You have two root nodes i.e. two & lt; Employee & gt; Tags can not be in the form of root. It is invalid XML; You can get a single root & lt; Employee & gt; Multiple & lt; Employee & gt; tag Also, stick in case of camel or capital. I am using capitals for stability.

  // root nodelight root address = doc.getElementsByName ("employee"); Node root = rootList.item (0); // Use the helpful method root.appendChild (Construction worker (Doctor, "Male", "John", "Do")); Public Emant Construction Worker (Document Doctor, String Lang, String Fanem, String Lam) {// Create New Employee Employee Employee = Doctor. Stent Element ("Employee"); Employee.setAttribute ("gender", gender); // Create Child Nodes Element firstName = doc.createElement ("FirstName"); FirstName.appendChild (doc.createTextNode (fname)); Element last name = doc.createElement ("LastName"); LastName.appendChild (doc.createTextNode (lname)); // Supplementary and return employees .appendChild (first name); Employee.appendChild (lastName); Return worker; }    

No comments:

Post a Comment