ticoldam12
asked on
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.printStackTr ace();
}
}
}
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!!
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
} catch (Exception e) {
e.printStackTr
}
}
}
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!!
show me your xml file
ASKER
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...
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...
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);
Document doc = new Document();
Element root = new Element("myRootElement");
root.setText("This is a root element");
doc.setRootElement(root);
System.out.println(doc);
ASKER
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
D:\example1.java:6: Document() has protected access in org.jdom.Document
Document doc = new Document();
^
1 error
Tool completed with exit code 1
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);
OK try this
Element root = new Element("myRootElement");
root.setText("This is a root element");
Document doc = new Document(root);
System.out.println(doc);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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_kound inya 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/>]]
I really dont know if i need to do an special setup for the jdom or something...sudhakar_kound
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/>]]
>>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
>>[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
to learn XMl programming using JDOM
follow this link
http://www.cafeconleche.org/books/xmljava/chapters/ch14.html
follow this link
http://www.cafeconleche.org/books/xmljava/chapters/ch14.html
ASKER
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??
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??
>>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:/myfir st.xml"))) ;
}
public static void print(Document doc, XMLOutputter formatter,
PrintStream out) throws Exception {
}
}
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:/myfir
}
public static void print(Document doc, XMLOutputter formatter,
PrintStream out) throws Exception {
}
}
a small change
public static void print(Document doc, XMLOutputter formatter,
PrintStream out) throws Exception {
formatter.output(doc, out);
}
public static void print(Document doc, XMLOutputter formatter,
PrintStream out) throws Exception {
formatter.output(doc, out);
}
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:/myfir st1.xml")) );
}
public static void print(Document doc, XMLOutputter formatter,
PrintStream out) throws Exception {
formatter.output(doc, out);
}
}
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:/myfir
}
public static void print(Document doc, XMLOutputter formatter,
PrintStream out) throws Exception {
formatter.output(doc, out);
}
}
ASKER
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!!...
All right sudhakar_koundinya...I think im all set!!
Thank you very very much for the help, i appreciate it!!...
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
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
thanks for accepting (:-)
I hope you have already information abt the latest api from
http://www.jdom.org/dist/binary/
http://www.jdom.org/downloads/
http://www.jdom.org/dist/binary/
http://www.jdom.org/downloads/