[jdom-interest] Internal entities in attribute values

Brian Williams Brian at amova.com
Thu Nov 30 17:29:46 PST 2000


This is either a bug or a misintrepretation on my part as to what should be
going on.

Here's a simple xml file, which uses internal entities in attribute values.

<?xml version="1.0" standalone="yes"?>
<!DOCTYPE test [
<!ELEMENT test (child)>
<!ELEMENT child ANY>
<!ATTLIST child name CDATA #REQUIRED>
<!ENTITY UserID '4'>
]>
<test>
<child name="&UserID;">
User #&UserID;
</child>
</test>


If I use the PrettyPrinter sample from the javaworld article,  I get the
following as output...

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE test><test>&UserID;<child
name="4">
User #&UserID;
</child></test>

Notice the &UserID; before the start of the child element.  When reading in
the document, the test element says it has mixed content: the UserID entity,
and the child element.  Here's some sample code (taken from the
PrettyPrinter sample)

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

public class TestEntity { 
   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)); 

	    Element root = doc.getRootElement();
	    List elements = root.getMixedContent();
	    Iterator i = elements.iterator();
	    while (i.hasNext()) {
	      System.out.println(i.next());
	    }
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 
} 

It prints out 
[Entity: &UserID;]
[Element: <child />]

I guess I could ignore the Entity, but reading section 4.4.2 of the XML spec
(my inspection of the archives indicates that people like people who read
specs...), and looking at the table in 4.4, I would assume that the Entity
is hidden from the application.  What use is passing this Entity object to
the application if you can't determine that it was actually inside the
attribute value (which leads to outputting incorrect xml, as per the
PrettyPrinter)?

Comments?


--Brian





More information about the jdom-interest mailing list