We help IT Professionals succeed at work.

Java XSLT transformation problem

NGInterface
NGInterface asked
on
Medium Priority
426 Views
Last Modified: 2013-11-23
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?
Comment
Watch Question

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.
// Serialize the document
      OutputFormat format = new OutputFormat(doc);
      format.setLineWidth(65);
      format.setIndenting(true);
      format.setIndent(2);
      XMLSerializer serializer = new XMLSerializer(System.out, format);
      serializer.serialize(doc);

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT
Top Expert 2016
Commented:
>>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

Commented:
Thanks. I adjusted the serializer to conform with the DOM4J's API and it now works!
CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)
pleased to help you
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.