[jdom-interest] Doctype entity declaration

Harry Evans hevans at elite.com
Wed Mar 7 21:57:56 PST 2001


I am having trouble with JDOM entity declarations in a doctype declaration.
Below is the file I am trying to parse.

escape.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE example[
<!ENTITY myEntity "abcdef">
]>
<Root>
  <child>&myEntity;</child>
</Root>

The entity is parsed, and valid the first time I parse the file, but
XMLOutputter does not output the entity declaration, if I write out the
parsed document.  Therefore, if I reparse the output, I get an exception
saying the entity was referenced, but not declared. 
It just seems incomplete that SAX parses the entity, but JDOM doesn't store
that info. If there isn't anyone working on this, I might try to implement
it, if anyone has any ideas on how it might work and other types that should
also be supported (if any).

Below is the code I am using to parse the file and the console output.  I am
using jdom-b6.jar.

JDOMEntityTester.java:

/**
 * A simple test of parsing entities from DocType declarations in JDOM
 * @author <a href="mailto:hevans at elite.com">Harry Evans</a>
 */
import org.jdom.Document;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.StringReader;
import java.io.StringWriter;

class JDOMEntityTester {
	public static void main(String argv[]) {
		if(argv.length > 0) {
			try {
				System.out.println("Reading file.");
				BufferedReader reader =
				  new BufferedReader(new
FileReader(argv[0]));
				StringBuffer buf = new StringBuffer();
				while(reader.ready()) {
	
buf.append(reader.readLine()).append("\n");
				}
				System.out.println("File version:\n" +
buf.toString());
				System.out.println("Parsing data with JDOM
first time.");
				SAXBuilder builder = new SAXBuilder();
				Document doc = builder.build(new
StringReader(buf.toString()));
				StringWriter writer = new StringWriter();
				XMLOutputter outputter = new XMLOutputter();
				outputter.output(doc, writer);
				System.out.println("JDOM version:\n" +
writer.toString());
				System.out.println("Parsing data with JDOM
second time.");
				Document doc2 =
				  builder.build(new
StringReader(writer.toString()));
			} catch(Exception e) {
				e.printStackTrace();
			}
		} else {
			System.out.println("Usage: JDOMEntityTester
filename");
		}
	}
}

Console output:
D:\>java JDOMEntityTester escape.xml
Reading file.
File version:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE example[
<!ENTITY myEntity "abcdef">
]>
<Root>
  <child>&myEntity;</child>
</Root>

Parsing data with JDOM first time.
JDOM version:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE example><Root>
  <child>&myEntity;</child>
</Root>

Parsing data with JDOM second time.
org.jdom.JDOMException: Error on line 3: The entity "myEntity" was
referenced, b
ut not declared.
        at org.jdom.input.SAXBuilder.build(SAXBuilder.java, Compiled Code)
        at org.jdom.input.SAXBuilder.build(SAXBuilder.java:499)
        at JDOMEntityTester.main(JDOMEntityTester.java, Compiled Code)
Root cause: org.xml.sax.SAXParseException: The entity "myEntity" was
referenced,
 but not declared.
        at
org.apache.xerces.framework.XMLParser.reportError(XMLParser.java:1056
)
        at
org.apache.xerces.readers.DefaultEntityHandler.startReadingFromEntity
(DefaultEntityHandler.java:596)
        at
org.apache.xerces.framework.XMLDocumentScanner$ContentDispatcher.disp
atch(XMLDocumentScanner.java, Compiled Code)
        at
org.apache.xerces.framework.XMLDocumentScanner.parseSome(XMLDocumentS
canner.java, Compiled Code)
        at org.apache.xerces.framework.XMLParser.parse(XMLParser.java:948)
        at org.jdom.input.SAXBuilder.build(SAXBuilder.java, Compiled Code)
        at org.jdom.input.SAXBuilder.build(SAXBuilder.java:499)
        at JDOMEntityTester.main(JDOMEntityTester.java, Compiled Code)





More information about the jdom-interest mailing list