I'm creating a document using the DOM
API. Part of the data I'm building into the document is HTML which contains
entity references. ( I parse the HTML and create nodes of the appropriate type
in the document.) One question is how do I get ENTITY elements into the
document? I tried the following:
Document doc
= null;
DocumentBuilderFactoryImpl docBuilderFactoryImpl = new DocumentBuilderFactoryImpl(); StringBufferInputStream sbis =
new StringBufferInputStream("<?xml version=\"1.0\"?> <!DOCTYPE TEST_DOC
[ <!ENTITY nbsp \" \" > ]>
<TEST_DOC></TEST_DOC>");
try
{
doc = docBuilderFactoryImpl.newDocumentBuilder().parse(sbis); } catch (Exception ex) { ex.printStackTrace(); } Node rootElement =
doc.getLastChild();
After adding other element to the document I can
dump it:
StringWriter docWriter = new StringWriter();
try
{
((XmlDocument)doc).write(docWriter); } catch (Exception ex) { ex.printStackTrace(); } System.out.println(docWriter.toString()); and the entity references appear in the output.
However, when I transform the
document:
//
Use the DOM Document to define a DOMSource
object.
DOMSource domSource = new DOMSource(doc);
XmlDocument xmlDoc = (XmlDocument)doc;
// Perform the transformation, placing the output in the DOMResult.
TransformerFactory tFactory =
TransformerFactory.newInstance();
if(tFactory.getFeature(DOMSource.FEATURE) &&
tFactory.getFeature(DOMResult.FEATURE))
{ File file = new File(i_xsltDocPath); transformer = tFactory.newTransformer(new StreamSource(file)); transformer.transform(domSource, new javax.xml.transform.stream.StreamResult(stringWriter));
}
to HTML the entity references don't showup in
the output. Any ideas who isn't processing them? It seem like it could be Xerces
or Xalan that is neglecting them. Right now it doesn't seem that the DOM API is
complete enough to create documents with.
Ted X. Toth
Elegiant, Inc. 8705 Shoal Creek Blvd., Suite 202 Austin, TX 78757 Phone: 512.206.0660 Fax: 512.206.0003 |