Appending an element
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Senthil_Ram
Posted On:   Tuesday, December 4, 2001 03:13 AM

I am trying to add an element (a record) in the xml file. But the way it adds the element is not correct. I might be doing something wrong in my code. I have also included the code, and the xml files. AppendData.java is the code I used to parse and add an element. Database.xml is the file where it reads the data. Test.xml is the output xml file, generated by the code. I need to put the element under "seed" element. If anyone enlighten some idea would be appreciated. appendData.java: import java.io.*; import org.w3c.dom.*; import org.apache.xerces.parsers.DOMParser; import org.apache.xml.serialize.*; import java.io.FileInputStream; public class appendData { publi   More>>

I am trying to add an element (a record) in the xml file. But the way it adds the element is not correct. I might be doing something wrong in my code. I have also included the code, and the xml files. AppendData.java is the code I used to parse and add an element. Database.xml is the file where it reads the data. Test.xml is the output xml file, generated by the code. I need to put the element under "seed" element. If anyone enlighten some idea would be appreciated.


appendData.java:


import java.io.*;
import org.w3c.dom.*;
import org.apache.xerces.parsers.DOMParser;
import org.apache.xml.serialize.*;
import java.io.FileInputStream;

public class appendData {
public static void main(String[] args) {
try {
DOMParser parser = new DOMParser();
parser.parse("Database.xml");
Document document = parser.getDocument();
Element root = document.createElement("Entry");
Element serviceName = document.createElement("ServiceName");
Text serviceNameText = document.createTextNode("servicename");
Element databaseType = document.createElement("DatabaseType");
Text databaseTypeText = document.createTextNode("databasetype");

serviceName.appendChild(serviceNameText);
databaseType.appendChild(databaseTypeText);
root.appendChild(serviceName);
root.appendChild(databaseType);
document.appendChild(root);

String filename = "test.xml";
FileWriter fw = new FileWriter(filename);
BufferedWriter bw = new BufferedWriter(fw);
XMLSerializer serial = new XMLSerializer(bw, new OutputFormat(document));
try {
serial.startNonEscaping();
serial.startDocument();
serial.serialize(document);
serial.endDocument();
bw.close();
fw.close();
} catch(IOException ioExcp) {
System.out.println("Save failed: ");
}

} catch (Exception e) {
System.out.println("Error!!!");
e.printStackTrace(System.err);
}
}
}


Database.xml:




dbAdmin
Database


edx0@gulper
oracle




test.xml:


<?xml version="1.0" encoding="UTF-8"?>


dbAdmin
Database


edx0@gulper
oracle


servicename databasetype

   <<Less

Re: Appending an element

Posted By:   Bozidar_Dangubic  
Posted On:   Tuesday, December 4, 2001 08:38 AM

the problem is in this line

document.appendChild(root);


you are appending to the document. when you do that, it will always be added to the end of the document. what you need to do is find the element inside the document and append 'root' to seed element with

seedElement.appendChild(root);

and not to the document as you are doing. you are appending child to and not to the end of the document.
About | Sitemap | Contact