R
roy.sebastien
Hello everyone,
I have created a document in memory using the following code
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
Element root = document.getDocumentElement();
document.setXmlVersion("1.0");
Node rootNode =
document.appendChild(document.createElement("ExportRH"));
(I add all the fields here)
Then, once the document is created, I pass it to the following method
which transforms it using a stylesheet:
private void transform(InputStream xsltStream, Object xmlStream,
OutputStream outputFile) throws TransformerConfigurationException,
TransformerException, ParserConfigurationException, SAXException,
IOException {
DocumentBuilderFactory builderfactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderfactory.newDocumentBuilder();
Document document = null;
if (xmlStream instanceof String)
document = builder.parse((String)xmlStream);
else
document = (Document)xmlStream;
//builder.parse((InputStream)xmlStream);
Source xsltSource = new StreamSource(xsltStream);
Source source = new DOMSource(document);
Result result = new StreamResult(outputFile);
TransformerFactory factory = TransformerFactory.newInstance();
Templates transformation = factory.newTemplates(xsltSource);
Transformer transformer = transformation.newTransformer();
transformer.transform(source, result);
}
It works fine until I have non-us characters. Then I receive a
TransformerException. I could serialize it and transform it on disk
but I was looking for a solution that would do the whole thing in
memory.
Can someone help me? I know this is trivial, but I'm not familiar with
XML.
Thanks
Sebastien
I have created a document in memory using the following code
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
Element root = document.getDocumentElement();
document.setXmlVersion("1.0");
Node rootNode =
document.appendChild(document.createElement("ExportRH"));
(I add all the fields here)
Then, once the document is created, I pass it to the following method
which transforms it using a stylesheet:
private void transform(InputStream xsltStream, Object xmlStream,
OutputStream outputFile) throws TransformerConfigurationException,
TransformerException, ParserConfigurationException, SAXException,
IOException {
DocumentBuilderFactory builderfactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderfactory.newDocumentBuilder();
Document document = null;
if (xmlStream instanceof String)
document = builder.parse((String)xmlStream);
else
document = (Document)xmlStream;
//builder.parse((InputStream)xmlStream);
Source xsltSource = new StreamSource(xsltStream);
Source source = new DOMSource(document);
Result result = new StreamResult(outputFile);
TransformerFactory factory = TransformerFactory.newInstance();
Templates transformation = factory.newTemplates(xsltSource);
Transformer transformer = transformation.newTransformer();
transformer.transform(source, result);
}
It works fine until I have non-us characters. Then I receive a
TransformerException. I could serialize it and transform it on disk
but I was looking for a solution that would do the whole thing in
memory.
Can someone help me? I know this is trivial, but I'm not familiar with
XML.
Thanks
Sebastien