Link to home
Start Free TrialLog in
Avatar of NGInterface
NGInterface

asked on

Java XSLT transformation problem

Hi,
   I am trying to transform a very large XML document using DOM4J in Java. Here's what I do:

1. Read in the original XML file using SAXReader (reader is a SAXReader object):
Document myDoc = reader.read(new File("very_large_xml_file.xml"))...

2. Pass the Document myDoc to the following function that transforms the file:

public Document stylesheet_trans(Document document, String stylesheet)
            throws Exception
      {
        // load the transformer using JAXP
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer(
            new StreamSource(stylesheet)
        );

        // now lets style the given document
        DocumentSource source = new DocumentSource(document);
        DocumentResult result = new DocumentResult();
        transformer.transform(source, result);

        // return the transformed document
        Document transformedDoc = result.getDocument();
        return transformedDoc;
    }

finally, write the returned Document to a new file:

final_file = new FileWriter("final_transformed_large_xml_file.xml");
returned_doc.write(final_file);

The problem here is, when I opened the final_transformed_large_xml_file.xml in a browser, it tells me that the document is truncated (that is, the transformation process seems unable to handle such a large original file...) -- there is no real problem with the actual tag transformations, but it is just cut off before the end. Can anyone help me with this problem?
Avatar of mightyone
mightyone

try using your own serialisation...

it is not recommenden using Document.write() for documents not opened with document.open

see: http://cafeconleche.org/quotes2005.html
and
depending on input maybe sax is not the right approach
http://cafeconleche.org/books/xmljava/chapters/ch06s07.html
use .eg.
ASKER CERTIFIED SOLUTION
Avatar of mightyone
mightyone

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of NGInterface

ASKER

Thanks. I adjusted the serializer to conform with the DOM4J's API and it now works!
:-)
pleased to help you