2

There is my old working code what rewrite an xml file:

private static void rewriteXmlDocument(Document document, File xmlFile) throws TransformerException,
FileNotFoundException {
 File temporaryFile = new File(xmlFile.getParent() + File.separator + "temporary.xml"); // write to a temporary file
 DOMSource source = new DOMSource(document);
 TransformerFactory transformerFactory = TransformerFactory.newInstance();
 Transformer transformer = transformerFactory.newTransformer();
 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
 transformer.setOutputProperty(OutputKeys.METHOD, "xml");
 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
 if (temporaryFile.exists()) temporaryFile.delete(); // there is a trash
 StreamResult result = new StreamResult(new FileOutputStream(temporaryFile));
 transformer.transform(source, result);
 xmlFile.delete(); // write succeeded, now we can delete the old
 temporaryFile.renameTo(xmlFile); // rename temporary to old name
}

If I add these two dependency to pom.xml (and some code for finding in a docx), I receive an exception from the transformerFactory.newTransformer() row.

<dependency>
 <groupId>org.docx4j</groupId>
 <artifactId>docx4j</artifactId>
 <version>6.1.2</version>
</dependency>
<dependency>
 <groupId>org.glassfish.jaxb</groupId>
 <artifactId>jaxb-runtime</artifactId>
 <version>2.3.1</version>
</dependency>

Exception is:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xml/serializer/TreeWalker
 at org.apache.xalan.processor.TransformerFactoryImpl.newTransformer(TransformerFactoryImpl.java:818)
 at AnanlyzeEmails/AnalyzeEmailsApplication.AnalyzeEmailsApp.rewriteXmlDocument(AnalyzeEmailsApp.java:170)

How can I use my old xml manipulation code beside jaxb?

Nick
6423 gold badges10 silver badges32 bronze badges
asked Dec 17, 2025 at 17:22
3
  • 1
    Welcome to Stack Overflow. Please take the tour to learn how Stack Overflow works and read How to Ask on how to improve the quality of your question. Then edit your question to include your source code as a working minimal reproducible example, which can be compiled and tested by others to provide an answer faster. Commented Dec 17, 2025 at 17:45
  • 2
    When you use Java 9 or newer, you can change TransformerFactory.newInstance() to TransformerFactory.newDefaultInstance() to always get the built-in transformer, no matter what additional dependencies have been added to the environment. Commented Dec 18, 2025 at 14:32
  • Thank you, it is the solution, my code run again. Commented Dec 18, 2025 at 16:24

1 Answer 1

5

You need to change your code to load a specific TransformerFactory rather than loading the first one it finds on the classpath. I don't know specifically why your code is failing but this is a common problem with JAXPs use of the Java service provider mechanism - different implementations of TransformerFactory are not sufficiently compatible to make them fully interchangeable. You probably want to load the default system-provided TransformerFactory: since Java 9 you can do this using TransformerFactory.getDefaultInstance().

answered Dec 17, 2025 at 21:00
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.