Java XSLT transformation problem

Posted on 2006-03-25
Last Modified: 2013-11-23
   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 = 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");

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?
Question by:NGInterface
    LVL 6

    Expert Comment

    try using your own serialisation...

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

    depending on input maybe sax is not the right approach
    use .eg.
    LVL 6

    Accepted Solution

    // Serialize the document
          OutputFormat format = new OutputFormat(doc);
          XMLSerializer serializer = new XMLSerializer(System.out, format);
    LVL 86

    Assisted Solution

    >>the transformation process seems unable to handle such a large original file

    That seems unlikely. More likely, perhaps, is that the browser can't handle it.

    Assuming Windows, issue the command:

    type yourfile.xml

    and look for the closing tag


    Author Comment

    Thanks. I adjusted the serializer to conform with the DOM4J's API and it now works!
    LVL 86

    Expert Comment

    LVL 6

    Expert Comment

    pleased to help you

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Suggested Solutions

    Title # Comments Views Activity
    withoutTen challenge 14 68
    wordsFront challenge 8 47
    userCompare  challenge 3 53
    strDist challenge 35 69
    Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
    Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now