liamgannon
asked on
Writing to xml with java
I have a java application that reads from an xml file, parses the file, updates the values stored in the tree but i don't know how to save the changes to the XML file. I've included the code below. I know it's a bit distorted but maybe you might be able follow it. I do't think you need to see the xml file but i can include it if it will help. In the class writeTree i've began writing to the xml file. Do i have to write each node and child to the xml file seperately or is there an easier way. Any help would be much appreciated, Thanks
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.File;
import java.io.*;
import java.text.*;
public class Parse{
public static void main(String args[]){
File docFile = new File("students.xml");
Document doc = null;
try{
DocumentBuilderFactory dbf = DocumentBuilderFactory.new Instance() ;
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(docFile);
}catch(Exception e){
System.out.println("error parsing file");
}
Element root = doc.getDocumentElement();
System.out.println("Root: "+root.getNodeName());
transverseDomTree(root);
//getSubjects(root);
//getResult(root);
//createElement(root, doc);
//writeTree(doc);
}
public static void transverseDomTree(Node start){
Node child;
System.out.println(start.g etNodeName ()+"="+
start.getNodeValue());
for(child = start.getFirstChild();chil d!=null;ch ild=
child.getNextSibling()){
transverseDomTree(child);
}
}
public static void getSubjects(Element start){
NodeList subjects = start.getElementsByTagName ("subject" );
for (int i = 0;i < subjects.getLength();i++){
NamedNodeMap atts = subjects.item(i).getAttrib utes();
System.out.println(atts.it em(0).getN odeValue() );
//System.out.println(subje cts.item(i ).getChild Nodes().it em(1).getF irstChild( ).getNodeV alue());
}
}
public static void getResult(Element start){
System.out.println("Enter subject name: ");
String input = new String();
BufferedReader console = new BufferedReader(new InputStreamReader(System.i n));
try{
input = console.readLine();
NodeList subjects = start.getElementsByTagName ("subject" );
for (int i = 0;i < subjects.getLength();i++){
NamedNodeMap atts = subjects.item(i).getAttrib utes();
if(input.equals(atts.item( 0).getNode Value())){
float result = Float.parseFloat(subjects. item(i).ge tChildNode s().item(1 ).getFirst Child().ge tNodeValue ());
System.out.println("Subjec t: "+atts.item(0).getNodeValu e()+
"Result: "+result);
}
}
}catch (IOException e) {
System.out.println("name = < "+ e + ">");
}
}
public static void createElement(Element start, Document doc){
Element subjectNode = doc.createElement("subject ");
Element resultNode = doc.createElement("result" );
subjectNode.setAttribute(" name","Jav a Programming");
resultNode.appendChild(doc .createTex tNode("60. 00"));
subjectNode.appendChild(re sultNode);
start.insertBefore(subject Node,start .getLastCh ild());
}
public static void removeChild(Element start){
System.out.println("Enter subject to be deleted: ");
String input = new String();
BufferedReader console = new BufferedReader(new InputStreamReader(System.i n));
try{
input = console.readLine();
NodeList subjects = start.getElementsByTagName ("subject" );
for (int i = 0;i < subjects.getLength();i++){
NamedNodeMap atts = subjects.item(i).getAttrib utes();
if(input.equals(atts.item( 0).getNode Value())){
Element deleted=null;
deleted.removeChild(atts.i tem(0));
}
}
}catch (IOException e) {
System.out.println("name = < "+ e + ">");
}
}
public static void writeTree(Document doc){
try{
File newFile = new File("students.xml");
FileWriter newFileStream = new FileWriter(newFile);
newFileStream.write("<?xml version=\"1.0\"?>\n");
newFileStream.close();
}catch (IOException e) {
System.out.println("name = < "+ e + ">");
}
}
}
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.File;
import java.io.*;
import java.text.*;
public class Parse{
public static void main(String args[]){
File docFile = new File("students.xml");
Document doc = null;
try{
DocumentBuilderFactory dbf = DocumentBuilderFactory.new
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(docFile);
}catch(Exception e){
System.out.println("error parsing file");
}
Element root = doc.getDocumentElement();
System.out.println("Root: "+root.getNodeName());
transverseDomTree(root);
//getSubjects(root);
//getResult(root);
//createElement(root, doc);
//writeTree(doc);
}
public static void transverseDomTree(Node start){
Node child;
System.out.println(start.g
start.getNodeValue());
for(child = start.getFirstChild();chil
child.getNextSibling()){
transverseDomTree(child);
}
}
public static void getSubjects(Element start){
NodeList subjects = start.getElementsByTagName
for (int i = 0;i < subjects.getLength();i++){
NamedNodeMap atts = subjects.item(i).getAttrib
System.out.println(atts.it
//System.out.println(subje
}
}
public static void getResult(Element start){
System.out.println("Enter subject name: ");
String input = new String();
BufferedReader console = new BufferedReader(new InputStreamReader(System.i
try{
input = console.readLine();
NodeList subjects = start.getElementsByTagName
for (int i = 0;i < subjects.getLength();i++){
NamedNodeMap atts = subjects.item(i).getAttrib
if(input.equals(atts.item(
float result = Float.parseFloat(subjects.
System.out.println("Subjec
"Result: "+result);
}
}
}catch (IOException e) {
System.out.println("name = < "+ e + ">");
}
}
public static void createElement(Element start, Document doc){
Element subjectNode = doc.createElement("subject
Element resultNode = doc.createElement("result"
subjectNode.setAttribute("
resultNode.appendChild(doc
subjectNode.appendChild(re
start.insertBefore(subject
}
public static void removeChild(Element start){
System.out.println("Enter subject to be deleted: ");
String input = new String();
BufferedReader console = new BufferedReader(new InputStreamReader(System.i
try{
input = console.readLine();
NodeList subjects = start.getElementsByTagName
for (int i = 0;i < subjects.getLength();i++){
NamedNodeMap atts = subjects.item(i).getAttrib
if(input.equals(atts.item(
Element deleted=null;
deleted.removeChild(atts.i
}
}
}catch (IOException e) {
System.out.println("name = < "+ e + ">");
}
}
public static void writeTree(Document doc){
try{
File newFile = new File("students.xml");
FileWriter newFileStream = new FileWriter(newFile);
newFileStream.write("<?xml
newFileStream.close();
}catch (IOException e) {
System.out.println("name = < "+ e + ">");
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
:-)
ASKER