Writing to XML file

Discussion in 'XML' started by Lana, Dec 29, 2003.

  1. Lana

    Lana Guest

    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);
    }

    }

    }
     
    Lana, Dec 29, 2003
    #1
    1. Advertising

  2. Lana

    GIMME Guest

    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();
     
    GIMME, Dec 29, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kelly G.

    Writing XML file

    Kelly G., Jan 7, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    2,852
    Kelly G.
    Jan 8, 2004
  2. HNguyen
    Replies:
    4
    Views:
    2,453
    HNguyen
    Dec 21, 2004
  3. Christopher Boomer
    Replies:
    0
    Views:
    419
    Christopher Boomer
    Jul 28, 2003
  4. jkflens
    Replies:
    2
    Views:
    1,544
    jkflens
    May 30, 2006
  5. Erik Wasser
    Replies:
    5
    Views:
    520
    Peter J. Holzer
    Mar 5, 2006
Loading...

Share This Page