开发者

how to process multiple files using java-xslt method?

I'm handling a task in which .java, .xslt, .bat and .properties files are involved.

Idea: to transform an xml-based file with extension tmx into a txt file, using xslt. Java is used as a kind of medium, connecting .properties and xslt. In the properties file, some parameters are appointed.

xml=abc.tmx xsl=aaa.xsl output=bbb.txt

The bat file acts as a launcher:

java -Xms512m -Xmx768m -DentityExpansionLimit=2000000 -classpath . transformations.TMXTransform

As you can imagine I have to modify the properties file for each processing, with only one output generated, which is not ideal in terms of efficiency.

Now I'd like to h开发者_开发知识库ave a better solution.

  1. all files in a specific location with specific extension are detected automatically, say a.tmx, b.tmx, c.tmx....z.tmx

  2. And with one double-click, corresponding txt output files with customizable filename,say a_output.txt, b_output.txt...z_output.txt) are generated to a specific location.

I suppose this is not difficult, but since I'm new to Java, I wonder if anybody can help.

BTW, directly using xslt is OK, too.

Thanks in advance.


Since you're not a Java wizard, I wonder whether you wouldn't be better off doing this in either Ant or XProc? Both are XML-based languages that allow you to control a sequence of transformations to selected files. It seems to me you're trying to invent a new property file syntax that's a simple control language for XSLT processing, and you don't need to, because several already exist.

Or another candidate would be xmlsh, a shellscript-like language for controlling XML processing tasks.


Try to use the properties file in a different way. Create a custom properties file where each line represents a processing job where you specify all needed parameters. You program should read each line of the properties file, parse the parameters and process the file(s). With a single invocation your program can process as many files as you defined in your custom properties file. In order to read the properties file, use BufferedReader.readLine() and StringTokenizer or String.split() to parse the parameteres.

This is just an idea.


Per Michael Kay's suggestion, you could use a simple ANT build script, like this one, to transform all of your *.tmx files.

<project name="TranformXml" basedir="." default="TransformFiles">
    <target name="TransformFiles">
        <xslt basedir="${basedir}" 
            destdir="build" 
            style="transform.xsl" 
            includes="*.tmx"
            extension="_output.txt">
        </xslt>
    </target>
</project>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜