• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

setExpandEntityReferences works?

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The input XML document doesn't have any entity references that I can see. It doesn't even have a DTD. Maybe you were thinking of something else that you didn't want to change?
 
reply
    Bookmark Topic Watch Topic
  • New Topic