E
El Durango
I have an application which uses an XSLT file along with an XML file to
create a desired output. However when I use UTF-8 encoded files it give an
error, when I use ANSI files it works fine. I cannot figure this out and I
am stuck at the moment. If anyone knows anything regarding this I would
appreciate it. Also I have a snippet of some simple code that I am using.
It is nothing special so I don't understand why it won't work. I am using
the latest Xerces and Xalan from Apache.
public static void main(String[] args) throws TransformerException {
String xmlSourceFileName = null;
String xsltFileName = null;
String xmlResultFileName = null;
String lastSwitch = null;
// Check command-line arguments.
if (args.length != 6) {
System.err.println("Usage:");
System.err.println("java " + RFKGenerator.class.getName() + " " +
IN_ARG + " xml-source " + XSL_ARG +
" xslt-file-name " + OUT_ARG + " xml-result");
System.exit(1);
}
else {
for (int i = 0; i < args.length; i++) {
if (args.equals(IN_ARG) || args.equals(XSL_ARG) ||
args.equals(OUT_ARG)) {
lastSwitch = args;
}
else {
if (lastSwitch.equals(IN_ARG)) {
xmlSourceFileName = args;
}
else if (lastSwitch.equals(XSL_ARG)) {
xsltFileName = args;
}
else if (lastSwitch.equals(OUT_ARG)) {
xmlResultFileName = args;
}
}
}
}
if ((xmlSourceFileName != null) && (xsltFileName != null) &&
(xmlResultFileName != null)) {
File xmlSourceFile = new File(xmlSourceFileName);
File xsltFile = new File(xsltFileName);
// System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
// "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
Source xmlSource = new StreamSource(xmlSourceFile);
Source xsltSource = new StreamSource(xsltFile);
DOMResult result = new DOMResult();
// Get a transformer that uses the XSLT input.
TransformerFactory transFact = TransformerFactory.newInstance();
Transformer trans = transFact.newTransformer(xsltSource);
// Apply the transform.
System.out.println("Applying stylesheet " + xsltFileName + "...");
trans.transform(xmlSource, result); // crashes here with UTF-8
encoded files.
create a desired output. However when I use UTF-8 encoded files it give an
error, when I use ANSI files it works fine. I cannot figure this out and I
am stuck at the moment. If anyone knows anything regarding this I would
appreciate it. Also I have a snippet of some simple code that I am using.
It is nothing special so I don't understand why it won't work. I am using
the latest Xerces and Xalan from Apache.
public static void main(String[] args) throws TransformerException {
String xmlSourceFileName = null;
String xsltFileName = null;
String xmlResultFileName = null;
String lastSwitch = null;
// Check command-line arguments.
if (args.length != 6) {
System.err.println("Usage:");
System.err.println("java " + RFKGenerator.class.getName() + " " +
IN_ARG + " xml-source " + XSL_ARG +
" xslt-file-name " + OUT_ARG + " xml-result");
System.exit(1);
}
else {
for (int i = 0; i < args.length; i++) {
if (args.equals(IN_ARG) || args.equals(XSL_ARG) ||
args.equals(OUT_ARG)) {
lastSwitch = args;
}
else {
if (lastSwitch.equals(IN_ARG)) {
xmlSourceFileName = args;
}
else if (lastSwitch.equals(XSL_ARG)) {
xsltFileName = args;
}
else if (lastSwitch.equals(OUT_ARG)) {
xmlResultFileName = args;
}
}
}
}
if ((xmlSourceFileName != null) && (xsltFileName != null) &&
(xmlResultFileName != null)) {
File xmlSourceFile = new File(xmlSourceFileName);
File xsltFile = new File(xsltFileName);
// System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
// "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
Source xmlSource = new StreamSource(xmlSourceFile);
Source xsltSource = new StreamSource(xsltFile);
DOMResult result = new DOMResult();
// Get a transformer that uses the XSLT input.
TransformerFactory transFact = TransformerFactory.newInstance();
Transformer trans = transFact.newTransformer(xsltSource);
// Apply the transform.
System.out.println("Applying stylesheet " + xsltFileName + "...");
trans.transform(xmlSource, result); // crashes here with UTF-8
encoded files.