Problems with java and JDOM

Hi all...Im getting to learn XML in java.
I have this little example in java

import java.io.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
public class PrettyPrinter {
   public static void main(String[] args) {
        // Assume filename argument
        String filename = args[0];
        try {
            // Build the document with SAX and Xerces, no validation
            SAXBuilder builder = new SAXBuilder();
            // Create the document
            Document doc = builder.build(new File(filename));
            // Output the document, use standard formatter
            XMLOutputter fmt = new XMLOutputter();
            fmt.output(doc, System.out);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

I suppose that i did a good job setting up jdom for java (if you have nice instructions on how to setup this in windows..would be highly appeciated :)) Compilation process goes fine, but when i run it i get this message

[Document: No DOCTYPE declaration, Root is[Element: <myRootElement/>]]

and this happens with every single example i try in java..is there a solution for that?? thank you for your help!!
ticoldam12Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sudhakar_koundinyaCommented:
show me your xml file
0
ticoldam12Author Commented:
Well, i was thinking that the example above was going to create a new xml file...
let me put it in this way...basically I would like to create the xml from scratch...I have also found this example. This one is supposed to do one from scratch

import org.jdom.*;

public class example1{
      public static void main(String[] args){

            Element root = new Element("myRootElement");
            DocType dt = new DocType("myRootElement");
            Document doc = new Document(root, dt);
            root.setText("This is a root element");
            System.out.println(doc);
      }
}

And the same message appears
[Document: No DOCTYPE declaration, Root is[Element: <myRootElement/>]]

The basic idea is to create a new xml file...
0
sudhakar_koundinyaCommented:
try like this and let me know. This should work

Document doc = new Document();
  Element root = new Element("myRootElement");
  root.setText("This is a root element");
  doc.setRootElement(root);
System.out.println(doc);
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

ticoldam12Author Commented:
it tells me...

D:\example1.java:6: Document() has protected access in org.jdom.Document
            Document doc = new Document();
                               ^
1 error

Tool completed with exit code 1
0
sudhakar_koundinyaCommented:
Strange. It is a public constructor.
OK try this

 Element root = new Element("myRootElement");
 root.setText("This is a root element");
Document doc = new Document(root);
System.out.println(doc);
0
sudhakar_koundinyaCommented:
This is working perfectly at  my end

import org.jdom.*;
import org.jdom.output.*;

public class JDOMTest {
  public JDOMTest() {
  }

  public static void main(String s[]) throws Exception {
    Element root = new Element("myRootElement");
    root.setText("This is a root element");
    Document doc = new Document(root);
    doc.setDocType(new DocType("SomeDocType"));
    XMLOutputter fmt = new XMLOutputter();
    fmt.output(doc, System.out);

  }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ticoldam12Author Commented:
You're right, im like surprised because i find examples and they are all supposed to work...
I really dont know if i need to do an special setup for the jdom or something...sudhakar_koundinya do you have like steps to follow or something because im not like very sure about the installation i did...
I have in this path D:\jdk1.3\jre\lib\ext the following:

ant.jar
colections.jar
crimson.jar
jaxp.jar
jdom.jar
xalan.jar
xerces.jar

I dont know if i need something else from the jdom-b7 folder or do i need to execute some again build.bat file??

And with your last example, i get the same thing =(
[Document: No DOCTYPE declaration, Root is[Element: <myRootElement/>]]
0
sudhakar_koundinyaCommented:
>>And with your last example, i get the same thing =(
>>[Document: No DOCTYPE declaration, Root is[Element: <myRootElement/>]]

That's true. The reason is  document is not foormatted as real xml document.

for format it into xml document try the above example
0
sudhakar_koundinyaCommented:
to learn XMl programming using JDOM

follow this link
http://www.cafeconleche.org/books/xmljava/chapters/ch14.html
0
ticoldam12Author Commented:
sudhakar_koundinya finally i got it =D
Exactly, the example you gave me JDOMTest works fine for me now

Guess what..I had to rename the class name...I had before example1.java....changed to JDOMTest and then it worked...that is weird!!

But im still unable to see an xml file in D:/

The example you gave me creates an output file??
0
sudhakar_koundinyaCommented:
>>But im still unable to see an xml file

I guess you will be going to ask abt that question only. You need to create FilePutputStream object to PrintStream object

see below example

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

public class JDOMTest {
  public JDOMTest() {
  }

  public static void main(String s[]) throws Exception {
    Element root = new Element("myRootElement");
    root.setText("This is a root element");
    Document doc = new Document(root);
    doc.setDocType(new DocType("SomeDocType"));
    XMLOutputter fmt = new XMLOutputter();
    //prints the document to standard output device
    print(doc, fmt, System.out);

    //prints the document to standard error device
    print(doc, fmt, System.err);

    //prints the document to file
    print(doc, fmt, new PrintStream(new FileOutputStream("c:/myfirst.xml")));

  }

  public static void print(Document doc, XMLOutputter formatter,
                           PrintStream out) throws Exception {

  }
}
0
sudhakar_koundinyaCommented:
a small change

 public static void print(Document doc, XMLOutputter formatter,
                           PrintStream out) throws Exception {
    formatter.output(doc, out);
  }
0
sudhakar_koundinyaCommented:
these are changes  in your example1

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

public class example1 {
  public example1() {
  }

  public static void main(String s[]) throws Exception {
    Element root = new Element("myRootElement");
    root.setText("This is a root element");
    Document doc = new Document(root);
    doc.setDocType(new DocType("SomeDocType"));
    XMLOutputter fmt = new XMLOutputter();
    //prints the document to standard output device
    print(doc, fmt, System.out);

    //prints the document to standard error device
    print(doc, fmt, System.err);

    //prints the document to file
    print(doc, fmt, new PrintStream(new FileOutputStream("c:/myfirst1.xml")));

  }

  public static void print(Document doc, XMLOutputter formatter,
                           PrintStream out) throws Exception {
    formatter.output(doc, out);
  }
}
0
ticoldam12Author Commented:
Great!! the code above is amazing!!

All right sudhakar_koundinya...I think im all set!!

Thank you very very much for the help, i appreciate it!!...
0
sudhakar_koundinyaCommented:
to  your other question

which version of JDom have you downloaded.

I expect the one you have is from http://www.jdom.org/dist/binary/jdom-1.0.zip. If not download this. It is latest.
Regarding the setups what you did at your end is perfect.

Regards
Sudhakar


0
sudhakar_koundinyaCommented:
thanks for accepting (:-)
0
sudhakar_koundinyaCommented:
I hope you have already information abt the latest api from

http://www.jdom.org/dist/binary/
http://www.jdom.org/downloads/
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.