entity references not preserved in XML Output
Hi,
I have an XML file, an element of which refers to a general entity.
I'm using DOm parser and JAXP API to parse it and form a resulting XML file.
I want the entity reference to be preserved in the resulting XML file which is currently NOT.
----------
DTD.....
----------
<!ENTITY x "Jones">
<!ELEMENT message (approved, signature )>
<!ELEMENT approved EMPTY>
<!ATTLIST approved flag ( true | false ) "false">
<!ELEMENT signature ( #PCDATA )>
-----------
sample.XML......
-----------
<?xml version="1.0" ?>
<!DOCTYPE message SYSTEM "sample.dtd">
<message>
<approved flag="true"/>
<signature>Chairperson &x; PhD</signature>
</message>
-----------
sampleoutput.xml
-----------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE message SYSTEM "sample.dtd">
<message>
<approved flag="true"/>
<signature>Chairperson Jones PhD</signature>
</message> <....>
I DO NOT want Jones. Instead, I want it as &x;
That is, I want to have the exact same XML output as the SOURCE.
-------------------------
My JAVA code follows....
-------------------------
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
factory.setExpandEntityReferences(true);
factory.setValidating(true);
factory.setIgnoringElementContentWhitespace(true);
DocumentBuilder builder=factory.newDocumentBuilder();
builder.setErrorHandler(new MyErrorHandler());
document=builder.parse(new File("C:/sample.xml"));
TransformerFactory tf=TransformerFactory.newInstance();
Transformer t=tf.newTransformer();
t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "sample.dtd");
FileWriter fileOut = new FileWriter("sampleoutput.xml");
t.transform(new DOMSource(document), new StreamResult(fileOut));
--------------------------------------
sample.xml -- source XML
sampleoutput.xml -- output XML
sample.dtd -- DTD
Could anyone please figure out how to preserve entity references?
Any help in this regard is grately appreciated.
- Srivalli.
|