Error in JDOM
import java.io.File;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
import org.w3c.dom.Document;
/*
* PrettyPrinter.java
*
public class PrettyPrinter
{
/** Creates a new instance of PrettyPrinter */
public PrettyPrinter()
{
}
publicstaticvoid main(String[] args)
{
//Assume filename argument
String filename=args[0];
try
{
//Build the document which SAX and Xerces, no validation
SAXBuilder builder =new SAXBuilder();
//Create the document
Document doc = builder.build(new File(filename));//The error is displayed for this line
//Output the document, use standard formatter
XMLOutputter fmt =new XMLOutputter();
fmt.output(doc, System.out);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
When I run the above code, I got an error as shown below
incompatible types
found: org.jdom.Document
required: org.w3c.dom.Document
Could anyone please help me with this.
import org.jdom.Document;
instead of:
import org.w3c.dom.Document;
>incompatible types
>found: org.jdom.Document
>required: org.w3c.dom.Document
Document doc = builder.build(new File(filename));
the jdom sax builder build method returns a JDOM document and not a org.w3c.dom.Documentobject.
Message was edited by:
java_2006
As you said, I have imported as following
import java.io.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
But I am still getting the following errors
D:\PrettyPrinter\src>javac PrettyPrinter.java
PrettyPrinter.java:8: package org.jdom does not exist
import org.jdom.*;
^
PrettyPrinter.java:9: package org.jdom.input does not exist
import org.jdom.input.*;
^
PrettyPrinter.java:10: package org.jdom.output does not exist
import org.jdom.output.*;
^
PrettyPrinter.java:37: cannot find symbol
symbol : class SAXBuilder
location: class PrettyPrinter
SAXBuilder builder = new SAXBuilder();
^
PrettyPrinter.java:37: cannot find symbol
symbol : class SAXBuilder
location: class PrettyPrinter
SAXBuilder builder = new SAXBuilder();
^
PrettyPrinter.java:39: cannot find symbol
symbol : class Document
location: class PrettyPrinter
Document doc = builder.build(new File(filename));
^
PrettyPrinter.java:41: cannot find symbol
symbol : class XMLOutputter
location: class PrettyPrinter
XMLOutputter fmt = new XMLOutputter();
^
PrettyPrinter.java:41: cannot find symbol
symbol : class XMLOutputter
location: class PrettyPrinter
XMLOutputter fmt = new XMLOutputter();
^
8 errors
D:\PrettyPrinter\src>
That's probably because you don't have JDom on your compile-time classpath
add jdom.jar to your classpath.
I have added jdom.jar to my classpath as shown below
C:\Program Files\Java\jdk1.5.0_06\bin;C:\Program Files\netbeans-5.0\bin;C:\Program Files\Java\jdk1.5.0_06\lib\jdom.jar
I have also added jdom.jar in Libraries in netbeans-5.0. But still I am getting the previous errors. Please help me with this
>C:\Program Files\Java\jdk1.5.0_06\lib\jdom.jar
don't add jdom there. Add it is C:\Program Files\Java\jdk1.5.0_06\jre\lib\ext\ directory instead.
Classpath problem is solved. Please do tell me why am I getting the following error
D:\PrettyPrinter\src>javac PrettyPrinter.java
D:\PrettyPrinter\src>java PrettyPrinter D:\test.xml
Exception in thread "main" java.lang.NoClassDefFoundError: org/jdom/input/SAXBuilder
D:\PrettyPrinter\src>
> Classpath problem is solved. Please do tell me why am
> I getting the following error
>
> D:\PrettyPrinter\src>javac PrettyPrinter.java
>
> D:\PrettyPrinter\src>java PrettyPrinter D:\test.xml
> Exception in thread "main"
> java.lang.NoClassDefFoundError:
> org/jdom/input/SAXBuilder
>
> D:\PrettyPrinter\src>
You have 2 classpaths to consider - the compile-time one, and the runtime one. JDom is now on your compiler classpath, but not, it seems, on your runtime one
Do you know how to specify classpaths at the command-line? Or are you adding libraries to the CLASSPATH variable and to jre/lib/ext? It's almost always better to specify it on the command-line
read this: http://www.jdom.org/docs/faq.html#a0090
you need xerces.jar. See http://xerces.apache.org/xerces-j/
hth
Thanks for the help. My program is now working. Please tell me why the line <?xml version='1.0'?>
is changed to <?xml version="1.0" encoding="UTF-8"?>
and why all the entities like /
, ©
etc., are either getting converted to symbol or ascii
The following is my original XML file
<?xml version='1.0'?>
<doi>00.0000/S0000000000000000</doi>
<cpyrgt>© 2006 XYZ</cpyrgt>
<pagefield><fpage>413</fpage><x>–</x><lpage>429</lpage></pagefield>
<title type="article">XYZ : does improved
The following XML file is after running through the program
<?xml version="1.0" encoding="UTF-8"?>
<doi>00.0000/S0000000000000000</doi>
<cpyrgt>? 2006 XYZ</cpyrgt>
<pagefield><fpage>413</fpage><x>"</x><lpage>429</lpage></pagefield>
<title type="article">XYZ:
> Thanks for the help. My program is now working.
> Please tell me why the line <?xml
> version='1.0'?>
is changed to <?xml
> version="1.0" encoding="UTF-8"?>
That's the encoding the file is (or claims to be) in
> and why all
> the entities like /
,
>
etc., are either getting
> converted to symbol or ascii
That's how you declare certain symbols that way. Read this
http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references
Whatever entities are there in my input XML file should be retained in my output XML file. Please do tell me what changes to make in my source code.