0

I'm trying to create XML file from Java property file. Wherein tag names are dot separated key part of Property file and the Tag value is value part of property file.

Here is my property file:

Inter.con.Compa.Plugin.Name=Compacon
Inter.con.Compa.Plugin.Config.Mode=Fast
Inter.con.Compa.Plugin.Config.DataSrc=Compa
Inter.con.Compa.Plugin.Config.Query=DONT KNOW
Inter.con.Compa.Plugin.Config.ClassName=FastCompaconComponentImpl
Inter.con.Compa.Plugin.Name=CompaconComponent
Inter.con.Compa.Plugin.Config.Mode=Detailed
Inter.con.Compa.Plugin.Config.DataSrc=Compa
Inter.con.Compa.Plugin.Config.Query=DONT KNOW
Inter.con.Compa.Plugin.Config.ClassName=DetailedCompaconComponentImpl
Inter.con.port.Plugin.Name=CompaconComponent
Inter.con.port.Plugin.Config.Mode=Fast
Inter.con.port.Plugin.Config.DataSrc=Compa
Inter.con.port.Plugin.Config.Query=DONT KNOW
Inter.con.port.Plugin.Config.ClassName=FastCompaconComponentImpl
Inter.con.port.Plugin.Name=CompaconComponent
Inter.con.port.Plugin.Config.Mode=Detailed
Inter.con.port.Plugin.Config.DataSrc=Compa
Inter.con.port.Plugin.Config.Query=DONT KNOW
Inter.con.port.Plugin.Config.ClassName=DetailedCompaconComponentImpl

Here is the program I wrote to xml file.

public static void myConvertToXML(String output_filename)
            throws IOException {
        Element root = new Element("Property");
        root.setAttribute("schemaVersion", "1.0");
        Namespace xmlns = Namespace.getNamespace("xsi",
                "http://www.w3.org/2001/XMLSchema-instance");
        root.setNamespace(xmlns);
        Document doc = new Document(root);
        BufferedReader buffer = new BufferedReader(
                new FileReader(inputFilePath));
        String line;
        while ((line = buffer.readLine()) != null) {
            line = line.trim();
            String[] inputLine = line.split("=");
            String keyPart = inputLine[0].trim();
            String valuePart = "";
            if (inputLine.length == 1) {
                    valuePart = " ";
            } else {                
                valuePart = inputLine[1].trim();
            }
            addToXML(root, keyPart, valuePart);

        }
        XMLOutputter outputter = new XMLOutputter();
        FileOutputStream output = new FileOutputStream(output_filename);
        outputter.output(doc, output);
        System.out.println("Done");

    }

    private static void addToXML(Element root, String key_Part,
            String value_Part) {
        int dot;
        String name = key_Part;
        Element current = root;
        Element test = null;
        while ((dot = name.indexOf(".")) != -1) {
            String subName = name.substring(0, dot);
            name = name.substring(dot + 1);
            if ((test = current.getChild(subName)) == null) {
                Element subElement = new Element(subName);
                current.addContent(subElement);
                current = subElement;
            } else {
                current = test;
            }
        }
        Element last = new Element(name);                        
        last.setText(value_Part); 
        current.addContent(last);
    }

The output I'm getting

<?xml version="1.0" encoding="UTF-8"?>
<xsi:Source xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    schemaVersion="1.0">
    <Inter>
        <con>
            <Compa>
                <Plugin>
                    <Name>Compacon</Name>
                    <Config>
                        <Mode>Fast</Mode>
                        <DataSrc>Compa</DataSrc>
                        <Query>DONT KNOW</Query>
                        <ClassName>FastCompaconComponentImpl</ClassName>
                        <Mode>Detailed</Mode>
                        <DataSrc>Compa</DataSrc>
                        <Query>DONT KNOW</Query>
                        <ClassName>DetailedCompaconComponentImpl</ClassName>
                    </Config>
                    <Name>CompaconComponent</Name>
                </Plugin>
            </Compa>
            <port>
                <Plugin>
                    <Name>CompaconComponent</Name>
                    <Config>
                        <Mode>Fast</Mode>
                        <DataSrc>Compa</DataSrc>
                        <Query>DONT KNOW</Query>
                        <ClassName>FastCompaconComponentImpl</ClassName>
                        <Mode>Detailed</Mode>
                        <DataSrc>Compa</DataSrc>
                        <Query>DONT KNOW</Query>
                        <ClassName>DetailedCompaconComponentImpl</ClassName>
                    </Config>
                    <Name>CompaconComponent</Name>
                </Plugin>
            </port>
        </con>
    </Inter>
</xsi:Source>

Expected output:

<?xml version="1.0" encoding="UTF-8"?>
<xsi:Source xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    schemaVersion="1.0">
    <Inter>
        <con>
            <Compa>
                <Plugin>
                    <Name>Compacon</Name>
                    <Config>
                        <Mode>Fast</Mode>
                        <DataSrc>Compa</DataSrc>
                        <Query>DONT KNOW</Query>
                        <ClassName>FastCompaconComponentImpl</ClassName>
                    </Config>
                </Plugin>
                <Plugin>
                    <Name>CompaconComponent</Name>
                    <Config>
                        <Mode>Detailed</Mode>
                        <DataSrc>Compa</DataSrc>
                        <Query>DONT KNOW</Query>
                        <ClassName>DetailedCompaconComponentImpl</ClassName>
                    </Config>
            </Plugin>
            </Compa>
            <port>
                <Plugin>
                    <Name>CompaconComponent</Name>
                    <Config>
                        <Mode>Fast</Mode>
                        <DataSrc>Compa</DataSrc>
                        <Query>DONT KNOW</Query>
                        <ClassName>FastCompaconComponentImpl</ClassName>
                    </Config>
                </Plugin>
                <Plugin>
                    <Name>CompaconComponent</Name>
                    <Config>
                        <Mode>Detailed</Mode>
                        <DataSrc>Compa</DataSrc>
                        <Query>DONT KNOW</Query>
                        <ClassName>DetailedCompaconComponentImpl</ClassName>
                    </Config>
                </Plugin>
            </port>
        </con>
    </Inter>
</xsi:Source>

Its adding everything in tag directly I want two separate tag. Could anyone please help. Let me know IF I missed anything

Enix
  • 138
  • 1
  • 7
  • This obviously occurs because you have duplicate property keys with different values. A quick dirty fix if you just need this done once would be to temporarily rename half of the `Inter.con.Compa.Plugin` to `Inter.con.Compa.Plugin2` and the same for `Inter.con.port.Plugin` in your properties file, then run the program and then use notepad++ or something to replace all `Plugin2` with `Plugin` to revert the temporary differentiating changes. – Ceiling Gecko Feb 20 '15 at 13:23
  • Hmm thanks @CeilingGecko but my original property file is far bigger than this with lots different tag with same problem. – Enix Feb 20 '15 at 14:02

0 Answers0