Reputation: 455
I need to modify a XML file but all the comments were trimmed in the output. I searched and think I need to use the Transformer.setProperty() fucntion but don't know know how to use it. Any help or hint will be very much appreciated. Please go easy on me on XML terminology because I am a XML newbie.
Thanks,
Below if the part that I want to keep, starting from the beginning of the XML file:
<!DOCTYPE ProductDataeXchangePackage [
<!-- DTD for IPC-2571
Public Identifier: "-//IPC//DTD 2571 200111//EN"
Official Location: http://webstds.ipc.org/2571.dtd -->
<!ELEMENT AdditionalAttribute EMPTY>
<!ATTLIST AdditionalAttribute name CDATA #REQUIRED
value CDATA #REQUIRED
dimension CDATA #IMPLIED
dataType (String |
Boolean |
Float |
Double |
Decimal |
DateTime |
Binary |
UriReference |
Other ) #IMPLIED
dataTypeOther CDATA #IMPLIED
description CDATA #IMPLIED >
<!ELEMENT AdditionalAttributes (AdditionalAttribute+)>
]>
<?pdx_version 1.0?>
<?generated_by Oracle/Extract/9.3.4/63?>
.....
Here is my code which re-write it to another file:
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = null;
try {
trans = transfac.newTransformer();
-->trans.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "??")<-- ;
} catch (TransformerConfigurationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(doc);
try {
trans.transform(source, result);
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String xmlString = sw.toString();
OutputStream f0;
byte buf[] = xmlString.getBytes();
f0 = new FileOutputStream("pdx1.xml");
for(int n=0;n<buf.length;n++) {
f0.write(buf[n]);
}
f0.close();
buf = null;
Upvotes: 0
Views: 512
Reputation: 167401
A DTD is not part of the XSLT/XPath data model so it is not preserved by an XSLT transformation. There are some ways around it but they depend on certain tools or extensions. As you use Java and only a default transformer to serialize some DOM tree you might first want to check whether LSSerializer
(https://docs.oracle.com/javase/8/docs/api/org/w3c/dom/ls/LSSerializer.html) is not doing a better job than a default Transformer
to output the DTD and/or CDATA section nodes.
Upvotes: 1