Search code examples
javaxmltextjdom

Handling a Fixed-Width File with Java and an XML Template (JDOM)


My current dilemma is with handling a fixed-width file through an XML file (Java language, JDOM). The files that I am working with however are not typical to examples seen on the internet.

An example of my data I tried to recreate:

<table style="width:100%">
  <tr>
    <th>Name</th>
    <th>Date</th> 
    <th>Hobbies</th>
    <th>Status</th>
   </tr>
  <tr>
    <td>Jack</td>
     <td>8/16/00</td> 
    <td>Video Games Fixing</td>
    <td>Single</td>
   </tr>
  <tr>
    <td></td>
     <td></td> 
    <td>Computers Reading</td>
    <td></td>
   </tr>
  <tr>
    <td>Jill</td>
     <td>9/9/03</td> 
    <td>Skiing Cooking</td>
    <td>Married</td>
   </tr>
</table> 

Within the XML Template File I am specifying the pos and len of the data so it is mapped properly, but I get a String Out of Bounds Exception when trying to handle the row that spills over into the one below. It seems like the source file would hold null values for those empty spaces in between or after (could be blank then null after the content for that row, my observation is based on how it handles in Notepad). How do I go about handling multiple rows of data in one column for one initial value in a fixed width file, do I need to find the exact length?

Once the data is gathered, a path is specified for where the data is supposed to go in a new file based on the XML Template.


Solution

  • After some more research, I was able to determine a fix. In the XML Template file you would include a mapping for a "colspan" and "rowspan" of your data. Then in Java, you would create a way to handle those two new values so as to get all your data in the target file and not have any Out of Bounds exceptions being thrown.