0

I am trying to split txt file line by line and want to add tab. for first level is 1 tab, second level is 2 tabs indented and so on.I know how to do this if user add input line by line but I want to split txt file contents.

This is input file:

<company><name>xyz</name><name>ABC PQR</name><address>
<line1>G M Road</line1><line2>akurdi</line2><state>Maharashtra</state>
<city>Pune</city></address><company>

And I want output like this:

<company>
    <name>xyz</name>
    <name>ABC PQR</name>
    <address>
        <line1>G M Road</line1>
        <line2>akurdi</line2>
        <state>Maharashtra</state>
        <city>Pune</city>
    </address>
<company>

This is what i tried but its not working:

class lineSplit {

public static void main(String[] args) throws Exception {
    BufferedReader br = new BufferedReader(new FileReader("input.txt"));
    List<String> list = new ArrayList<String>();
    String line = null;
    String[] values;
    while ((line = br.readLine()) != null) {
        values = line.split(">");

        for (String str : values) {
            list.add(str + ">");
        }

    }
    Iterator itr = list.iterator();
    while (itr.hasNext()) {
        System.out.println(itr.next());
    }

    br.close();
}
}

How can i do this?

muhammad tayyab
  • 727
  • 7
  • 18
Vinay
  • 2,272
  • 4
  • 19
  • 34

1 Answers1

0

The thing that you're looking for called pretty print. And here is the answer for it: How to pretty print XML from Java?

Naya
  • 850
  • 6
  • 19