Reputation: 56
this is the sample text file content
<TRAN>
<LCL_STRT_TMST>2012-05-01T16:06:30.033</LCL_STRT_TMST>
<LCL_END_TMST>2012-05-01T16:06:30.033</LCL_END_TMST>
<TME_ZONE>-3</TME_ZONE>
<APPL>80</APPL>
<TRAN_TYPE>Exception in ECOMP request</TRAN_TYPE>
<TRAN_ID>20120501160624502879807</TRAN_ID>
<TRAN_PRIT>1</TRAN_PRIT>
<DUNS_NBR>142307417:1 US</DUNS_NBR>
<PRCS_STAT>500</PRCS_STAT>
<PRCS_MSG>Transaction 20120501160624502879807 caused GFServerException while writing SOAP response: - Unexpected exception caught (java.lang.NullPointerException)</PRCS_MSG>
</TRAN>
<TRAN>
<LCL_STRT_TMST>2012-05-01T16:06:37.283</LCL_STRT_TMST>
<LCL_END_TMST>2012-05-01T16:06:37.283</LCL_END_TMST>
<TME_ZONE>-3</TME_ZONE>
<APPL>80</APPL>
<TRAN_TYPE>Exception in EBIR request</TRAN_TYPE>
<TRAN_ID>20120501160636283855855</TRAN_ID>
<TRAN_PRIT>1</TRAN_PRIT>
<DUNS_NBR>142307417:1 US</DUNS_NBR>
<PRCS_STAT>500</PRCS_STAT>
<PRCS_MSG>Transaction 20120501160636283855855 caused GFServerException while writing SOAP response: - Unexpected exception caught (java.lang.NullPointerException)</PRCS_MSG>
</TRAN>
i need help in writing a new file in which the <TRAN>
to </TRAN>
is written in a single line and the next <TRAN>
to </TRAN>
is written in the next line.
Desired output is:
<TRAN><LCL_STRT_TMST>2012-05-01T16:06:30.033</LCL_STRT_TMST><LCL_END_TMST>2012-05-01T16:06:30.033</LCL_END_TMST><TME_ZONE>-3</TME_ZONE><APPL>80</APPL><TRAN_TYPE>Exception in ECOMP request</TRAN_TYPE><TRAN_ID>20120501160624502879807</TRAN_ID><TRAN_PRIT>1</TRAN_PRIT><DUNS_NBR>142307417:1 US</DUNS_NBR><PRCS_STAT>500</PRCS_STAT><PRCS_MSG>Transaction 20120501160624502879807 caused GFServerException while writing SOAP response: - Unexpected exception caught (java.lang.NullPointerException)</PRCS_MSG></TRAN>
<TRAN><LCL_STRT_TMST>2012-05-01T16:06:37.283</LCL_STRT_TMST><LCL_END_TMST>2012-05-01T16:06:37.283</LCL_END_TMST><TME_ZONE>-3</TME_ZONE><APPL>80</APPL><TRAN_TYPE>Exception in EBIR request</TRAN_TYPE><TRAN_ID>20120501160636283855855</TRAN_ID><TRAN_PRIT>1</TRAN_PRIT><DUNS_NBR>142307417:1 US</DUNS_NBR><PRCS_STAT>500</PRCS_STAT><PRCS_MSG>Transaction 20120501160636283855855 caused GFServerException while writing SOAP response: - Unexpected exception caught (java.lang.NullPointerException)</PRCS_MSG></TRAN>
Note : the file is very large n contains huge number of <TRAN>
tags
Upvotes: 0
Views: 46
Reputation: 65821
Here's a Folded
class that will fold a sequence of String
s as you require.
private static class Folded implements Iterator<String> {
// The Iterator I am folding.
final Iterator<String> it;
// What the start of a fold looks like.
final String start;
// What the end of a fold should look like.
final String end;
private Folded(Iterator<String> it, String start, String end) {
this.it = it;
this.start = start;
this.end = end;
}
@Override
public boolean hasNext() {
return it.hasNext();
}
@Override
public String next() {
// Get the next String.
StringBuilder n = new StringBuilder(it.next());
// If it's a "start"
if (n.toString().equals(start)) {
// Add ...
String add = it.next();
do {
// ... next ...
n.append(add);
// Until end seen.
} while (it.hasNext() && !(add = it.next()).equals(end));
// Last
n.append(add);
}
return n.toString();
}
}
public void test() throws FileNotFoundException {
String test = "<TRAN>\n"
+ "<LCL_STRT_TMST>2012-05-01T16:06:30.033</LCL_STRT_TMST>\n"
+ "<LCL_END_TMST>2012-05-01T16:06:30.033</LCL_END_TMST>\n"
+ "<TME_ZONE>-3</TME_ZONE>\n"
+ "<APPL>80</APPL>\n"
+ "<TRAN_TYPE>Exception in ECOMP request</TRAN_TYPE>\n"
+ "<TRAN_ID>20120501160624502879807</TRAN_ID>\n"
+ "<TRAN_PRIT>1</TRAN_PRIT>\n"
+ "<DUNS_NBR>142307417:1 US</DUNS_NBR>\n"
+ "<PRCS_STAT>500</PRCS_STAT>\n"
+ "<PRCS_MSG>Transaction 20120501160624502879807 caused GFServerException while writing SOAP response: - Unexpected exception caught (java.lang.NullPointerException)</PRCS_MSG>\n"
+ "</TRAN>\n"
+ "<TRAN>\n"
+ "<LCL_STRT_TMST>2012-05-01T16:06:37.283</LCL_STRT_TMST>\n"
+ "<LCL_END_TMST>2012-05-01T16:06:37.283</LCL_END_TMST>\n"
+ "<TME_ZONE>-3</TME_ZONE>\n"
+ "<APPL>80</APPL>\n"
+ "<TRAN_TYPE>Exception in EBIR request</TRAN_TYPE>\n"
+ "<TRAN_ID>20120501160636283855855</TRAN_ID>\n"
+ "<TRAN_PRIT>1</TRAN_PRIT>\n"
+ "<DUNS_NBR>142307417:1 US</DUNS_NBR>\n"
+ "<PRCS_STAT>500</PRCS_STAT>\n"
+ "<PRCS_MSG>Transaction 20120501160636283855855 caused GFServerException while writing SOAP response: - Unexpected exception caught (java.lang.NullPointerException)</PRCS_MSG>\n"
+ "</TRAN>";
// Make it into a list.
ArrayList<String> list = new ArrayList<String>(Arrays.asList(test.split("\n")));
Folded f = new Folded(list.iterator(), "<TRAN>", "</TRAN>");
for (String folded : Iterables.in(f)) {
System.out.println(folded);
}
}
You can make an Iterator<String>
from a File
like this:
Iterator<String> lines = new BufferedReader(new FileReader("test.xml"))
.lines()
.iterator();
Note that my Iterables.in
creates an Iterable
that iterates over the Iterator
- I use it here just for demonstration.
Upvotes: 0
Reputation: 3121
The simplest way I can imagine is reading the file line by line and outputting a new line after you find the closing tag. From your description, this utility method does not even need to know the file is XML or anything else.
For example, the following class
class Lines{
public static void join(String endLine, Reader reader, Writer writer) throws IOException {
String line;
BufferedReader bufferedReader = new BufferedReader(reader);
while ((line = bufferedReader.readLine()) != null) {
String tLine = line.trim();
writer.write(tLine);
if (tLine.equals(endLine)) {
writer.write(System.lineSeparator());
}
}
}
}
Can be used to filter a file into the system output, for example.
InputStream in = ...
OutputStreamWriter writer = new OutputStreamWriter(System.out);
Lines.join("</TRAN>", new InputStreamReader(in), writer);
writer.flush();
Upvotes: 1