Writing to XML file

L

Lana

Hello all!
I need to add another element to an XML file along with its content, but
encountered a problem.
I tried with the code provided, but it happens so that i can't run the
program from my jbuilder.
As it has the main method, i dont understand why..
Any help would do.
Thanx in advance :)
greetings :))

the code:
import org.jdom.*;
import org.jdom.input.SAXBuilder;
import java.io.*;
import org.jdom.output.*;

class Dodaj
extends Element {

double unos;
double konkpon;
double konkuto;

public void main(String[] args) {

String filename = "jdom.xml";
SAXBuilder builder = new SAXBuilder();

try {
Document doc = builder.build(new File(filename));
Element root = doc.getRootElement();

// Output the document, use standard formatter
XMLOutputter fmt = new XMLOutputter();
fmt.output(doc, System.out);
//saving XML document
File outputFile = new File("jdom.xml");
FileOutputStream out = new FileOutputStream(outputFile);
fmt.output(doc, out);
out.flush();
out.close();

unos = 5;
konkpon = 5;

System.out.println(unos);

if (unos == konkpon) {
Element element = new Element("Year");
Element element2 = new Element("day index=\"monday\"");
Element element3 = new Element("hour index=\"10-12\"");
element3.setText("Konkurentno labos");
element.addContent(element2);
element2.addContent(element3);
}
else if (unos == konkuto) {
Element element = new Element("Year");
Element element2 = new Element("day index=\"tuesday\"");
Element element3 = new Element("hour index=\"9-11\"");
element3.setText("Konkurentno labos");
element.addContent(element2);
element2.addContent(element3);
}

}
// indicates a well-formedness error
catch (JDOMException e) {
System.out.println(filename + " is not well-formed.");
System.out.println(e.getMessage());
}
catch (IOException e) {
System.out.println(e);
}

}

}
 
G

GIMME

You definition of main is off.

Also, the logic in main should go into a separate method, as in go() below,
so you don't run into trouble with some variables not being static.

You should use the jdom tools to add attributes - as in :

Element element = new Element("Year");
Element element2 = (new Element("day")).setAttribute("index","monday");
Element element3 = (new Element("hour")).setAttribute("index","10-12");

=============

import org.jdom.*;
import org.jdom.input.SAXBuilder;
import java.io.*;
import org.jdom.output.*;

class Dodaj extends Element {

double unos;
double konkpon;
double konkuto;

public static void main(String[] args) {
Dodaj d = new Dodaj();
d.go();
}

public void go() {
String filename = "jdom.xml";
SAXBuilder builder = new SAXBuilder();

try {
Document doc = builder.build(new File(filename));
Element root = doc.getRootElement();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top