Pelide
Pelide

Reputation: 528

How to loop XSLT transformation trough directories?

I'm transforming some XML files using an XSLT.

Here the script I used


import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;



public class XsltTransformer {
    public static void main(String[] args) throws IOException, URISyntaxException, TransformerException {
            String xmlFile = "/my/directory/file.xml"; //put path of input XML file between ""
            String xslFile = "/my/directory/file.xsl"; //put path of input XSL file between ""
            TransformerFactory factory = TransformerFactory.newInstance();
            Source xslt = new StreamSource(new File(xslFile));
            Transformer transformer = factory.newTransformer(xslt);
            Source text = new StreamSource(new File(xmlFile));
            transformer.transform(text, new StreamResult(new File("/my/directory/output.xml"))); //put path of newly created XML file between ""
    }
}

By this I'm able to process one single file and it works.

However, I need to extend the code to process multiple xml files iterating through directories.

The structure is:

DirA
 |
 |-->SubDirA1
 |       |
 |       |---->XMLFile
 |
DirB
 |
 |-->SubDirB1
 |       |
 |       |---->XMLFile
 |
...

The question I think was already partially answered here however, I never used ant as suggested.

Upvotes: 0

Views: 113

Answers (1)

andreoss
andreoss

Reputation: 1820

You can try to walk through your directory and process each file writing the results to a separate directory. You usually use Files.walk for that

import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import java.io.IOException;
import java.nio.file.*;

public class XsltTransformer {
    private final Path input;
    private final Path output;
    private final Transformer transformer;

    XsltTransformer(Path input, Path output, Transformer transformer) {
        this.input = input;this.output = output;this.transformer = transformer;
    }

    public static void main(String[] args) throws Exception {
        final Transformer transformer = TransformerFactory
            .newInstance()
            .newTransformer(new StreamSource(Paths.get("/my/directory/file.xsl").toFile()));
        new XsltTransformer(
            Paths.get("/my/directory"),
            Paths.get("/my/output"),
            transformer
        ).run();
    }

    private Path transform(Path file) {
        final StreamSource resource = new StreamSource(file.toFile());
        final Path output = this.resolveOutput(file);
        final Result result = new StreamResult(
            output.toFile()
        );
        try {
            this.transformer.transform(resource, result);
            return output;
        } catch (TransformerException ex) {
            throw new IllegalStateException(ex);
        }
    }

    private Path resolveOutput(Path file) {
        return this.output.resolve(this.input.relativize(file));
    }

    public void run() throws IOException {
        Files.walk(this.input)
            .filter(file -> file.getFileName().endsWith(".xml"))
            .map(this::transform)
            .forEach(System.out::println);
    }
}

Upvotes: 2

Related Questions