0

We have some partners that send there product orders via an XML file. Wich they send to our server using FTP. A windows command line script will take it from the FTP server ans send it to our AS400/System I. Where it will be processed into a registered order. Now are we starting to work with a new partner wich sends us XML in the same way a other partners the only problem is that this XML is made of 1 long line of code in stet of a pretty print lay out. This is a problem for the AS400 program. I would like to ad some code in the Windows command line script to get a line break after each end tag For example Here is a part of the original XML

    <?xml version="1.0" encoding="UTF-8"?><czavinh3 xmlns="urn:com.sap.b1i.vplatform:entity"><envelope><sender>Lottum</sender><receiver>8714253093123</receiver></envelope><message><docnum>1122259</docnum><docdtm>2015-09-28</docdtm><txts>test1234</txts>

Here is an example of what the as400 wants

    <?xml version="1.0" encoding="UTF-8"?>
    <czavinh3 xmlns="urn:com.sap.b1i.vplatform:entity">
    <envelope>
    <sender>Lottum</sender>
    <receiver>8714253093123</receiver>
    </envelope>
    <message>
    <docnum>1122259</docnum>
    <docdtm>2015-09-28</docdtm>
    <txts>test1234</txts>

I think the fastest way is to search and replace the >< for a >(enter)< this is easily done bye hand bud i have no idea how to automate this

2 Answers2

0

First of all, the posted document doesn't conform to xml standards. It's seem to be missing the closing tags </message></czavinh3>. Assuming they have been simply omitted and the xml is well-formed, you could use a small java program (following Pretty-printing output from javax.xml.transform.Transformer with only standard java api (Indentation and Doctype positioning)) like:

package xmlprettyprint;

import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;

public class XmlPrettyPrint {

    public static void main(String[] args) throws TransformerException, FileNotFoundException {

        // Instantiate transformer input
        Source xmlInput = new StreamSource(new FileReader(args[0]));
        StreamResult xmlOutput = new StreamResult(new StringWriter());

        // Configure transformer
        Transformer transformer = TransformerFactory.newInstance()
            .newTransformer(); // An identity transformer
        transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "testing.dtd");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(xmlInput, xmlOutput);

        System.out.println(xmlOutput.getWriter().toString());
    }
}

It reads the filename from the commandline, parses it and does a pretty-print output, resulting in output like:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE czavinh3 SYSTEM "testing.dtd">
    <czavinh3 xmlns="urn:com.sap.b1i.vplatform:entity">
    <envelope>
    <sender>Lottum</sender>
    <receiver>8714253093123</receiver>
    </envelope>
    <message>
    <docnum>1122259</docnum>
    <docdtm>2015-09-28</docdtm>
    <txts>test1234</txts>
    </message>
    </czavinh3>

Provided the jar file would be named xmlPrettyPrint.jar, you can call the java program from command line like:

java -jar xmlPrettyPrint.jar input.xml

and output would be like above.

Community
  • 1
  • 1
sahlix
  • 61
  • 1
  • 3
  • Hi, thanks for your help I'm trying to test the code you made but I get an error C:\batch\CarriageReturn>java -jar xmlPrettyPrint Agrocultuur.xml Error: Unable to access jarfile xmlPrettyPrint – Joran van der Griend Oct 23 '15 at 08:25
  • You have to compile the given java code into a jar file. Or directly compile it into a class file. – sahlix Oct 23 '15 at 15:00
  • I have compiled the code and when I run the file is get another error big change it is something small bud i have no knowledge of java what so ever this is the error I'm getting Error: Could not find or load main class XmlPrettyPrint – Joran van der Griend Oct 26 '15 at 08:36
  • Thanks for your help but I found an other way to solve my problem – Joran van der Griend Oct 26 '15 at 12:22
0

Not being a great programmer made my question a bit of a hard problem. Talking with some friends who have some more programming experience told me about FART (Find and Replace Text)

Download here.

This tool is an exe file witch allows you to change the ASCII from files. My problem was that the incoming XML file is made out of 1 string this was a problem for processing it.

With the following statement I changed the XML to a Pretty Print file by entering a break line

    fart -c --c-style input.xml "><" ">\r<"

The option -c --c-style allows you to use c-cstyle extended characters like \r (Carriage Return)

Here are the other options u can use

    Find And Replace Text  v1.99b                         by Lionello Lunesu

    Usage: FART [options] [--] <wildcard>[,...] [find_string] [replace_string]

    Options:
    -h, --help          Show this help message (ignores other options)
    -q, --quiet         Suppress output to stdio / stderr
     -V, --verbose       Show more information
     -r, --recursive     Process sub-folders recursively
     -c, --count         Only show filenames, match counts and totals
     -i, --ignore-case   Case insensitive text comparison
     -v, --invert        Print lines NOT containing the find string
     -n, --line-number   Print line number before each line (1-based)
     -w, --word          Match whole word (uses C syntax, like grep)
     -f, --filename      Find (and replace) filename instead of contents
     -B, --binary        Also search (and replace) in binary files (CAUTION)
     -C, --c-style       Allow C-style extended characters (\xFF\0\t\n\r\\ etc.)
         --cvs           Skip cvs dirs; execute "cvs edit" before changing files
         --svn           Skip svn dirs
         --remove        Remove all occurences of the find_string
     -a, --adapt         Adapt the case of replace_string to found string
     -b, --backup        Make a backup of each changed file
     -p, --preview       Do not change the files but print the changes