hi,
I have a small program that reads an XML and writes it. There are some entities in the xml attributes, which I don't want to be resolved, but justto be written back as they appear in the source file. I'm using the DocumentBuilderFactory class and it's method setExpandEntityReferences(false) to avoid the expansions of the entities, but it still resolves the entities within the attributes. Why? What's wrong? I'm working xerces.jar (2.7.1), xalan.jar(2.7.0) and xml-apis(2.7.1) and jdk 1.3.1.
there's the code:
public static void main(
String[] args) {
if (args.length <= 0) {
System.out.println("Usage:
java EntityLister fileName");
return;
}
String fileName = args[0];
File xmlFile = null;
try
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// By default JAXP does not include entity reference nodes
// in the tree. You have to explicitly request them by
// telling DocumentBuilderFactory not to expand entity
// references.
factory.setExpandEntityReferences(false);
DocumentBuilder parser = factory.newDocumentBuilder();
// Read the file
xmlFile = new File(fileName);
// construct the document
Document document = parser.parse(xmlFile);
DOMSource domSource = new DOMSource(document);
StreamSource xslSource = new StreamSource("transformer.xsl"); // XSL for empty transformation
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer(xslSource);
StreamResult streamResult = new StreamResult(new OutputStreamWriter(new FileOutputStream("result.xml"),"ISO-8859-1"));
transformer.transform(domSource, streamResult);
}
catch (SAXException e) {
System.out.println(xmlFile + " is not well-formed.");
}
catch (IOException e) {
System.out.println(
"Due to an IOException, the parser could not read " + xmlFile
);
}
catch (FactoryConfigurationError e) {
System.out.println("Could not locate a factory class");
}
// catch (ParserConfigurationException e) {
// System.out.println("Could not locate a JAXP parser");
// }
catch(Exception e) {
System.out.println("Exception!!! "+ e.toString());
}
} // end main
}
so if the input file is:
<?xml version="1.0" encoding="UTF-8"?>
<Tokenization>
<StartWordChars>
<Char From="" To="ゟ"/>
<Char From="゠" To="ヿ"/>
<Char From="豈" To=""/>
<Char From="一" To="龯"/>
<Char From="丽" To=""/>
</StartWordChars>
<EndWordChars>
<Char From="" To="ゟ"/>
<Char From="゠" To="ヿ"/>
<Char From="豈" To=""/>
<Char From="一" To="龯"/>
<Char From="丽" To=""/>
</EndWordChars>
<NonDelimeterChars>
<Char From="" To="ゟ"/>
<Char From="゠" To="ヿ"/>
<Char From="豈" To=""/>
<Char From="一" To="龯"/>
<Char From="丽" To=""/>
<Char Value="."/>
<Char Value=""/>
</NonDelimeterChars>
</Tokenization>
I get the output file:
<?xml version="1.0" encoding="UTF-8"?><Tokenization>
<StartWordChars>
<Char From="?" To="?"/>
<Char From="?" To="?"/>
<Char From="?" To="?"/>
<Char From="?" To="?"/>
<Char From="丽?" To="?"/>
</StartWordChars>
<EndWordChars>
<Char From="?" To="?"/>
<Char From="?" To="?"/>
<Char From="?" To="?"/>
<Char From="?" To="?"/>
<Char From="丽?" To="?"/>
</EndWordChars>
<NonDelimeterChars>
<Char From="?" To="?"/>
<Char From="?" To="?"/>
<Char From="?" To="?"/>
<Char From="?" To="?"/>
<Char From="丽?" To="?"/>
<Char Value="."/>
<Char Value="�"/>
</NonDelimeterChars>
</Tokenization>
anyone can help?