I have a text file that has many records. Each record is a xml element. each record is separated by a text – RECORD_BREAK.
Ex
<customer><name>John</name><phone>123-345-1123</phone></customer>
RECORD_BREAK
<customer><name>John</name><phone>123-345-1123</phone></customer>
RECORD_BREAK
<customer><name>John</name><phone>123-345-1123</phone></customer>
RECORD_BREAK
<customer><name>John</name><phone>123-345-1123</phone></customer>
RECORD_BREAK
while reading the records, i split the each record by
String[] strings = xml.split(“RECORD_BREAK”);
now i need to add a Header and Footer for the file. This is to identify the file is not corrupted. how to read the header and footer of the file. Header and footer is as simple as
<CustomerInfo>
<customer><name>John</name><phone>123-345-1123</phone></customer>
RECORD_BREAK
<customer><name>John</name><phone>123-345-1123</phone></customer>
RECORD_BREAK
</CustomerInfo>
how can i check for the and tag at starting and ending of records before i split the records.
Ideally, you would not have the RECORD_BREAK markers in the file, because you do not need them: You already have records separated by tags. Just parse the whole thing with an XML parser (especially easy after wrapping it into a top-level CustomerInfo tag).
This also gives you at least the same very modest level of “corruption check” that your header and footer would provide, and would not fail if some customer happens to be named “Little Bobby Tables RECORD_BREAK”.
You can still parse the file as it is now (with RECORD_BREAK in it) using an XML parser. If the header does not match the footer, it will not be valid XML and the parser will tell you (it will also tell you about malformed Customer tag contents). The result will be a nice sequence of Customer nodes and text nodes (the record breaks).