This discussion is archived
7 Replies Latest reply: May 20, 2013 4:13 AM by Suresh Karunarathne RSS

How to convert text to xml

Manjit Newbie
Currently Being Moderated
Hi ,I have this fixed column lenght text file that contains Employee Name,Age and Gender.I want to convert it to xml file.See the details below.Wondering if there is and easy way to achieve it.Please share some idea.


GeorgeBush50M
GeorgeBush60M
GeorgeBush70M
GeorgeBush80F


I want to convert it to xml

<OracleEmployee>
<Employee>
<Name>GeorgeBush</Name>
<Age>50</Age>
<Gender>M</Gender>
</Employee>

<Employee>
<Name>GeorgeBush</Name>
<Age>60</Age>
<Gender>M</Gender>
</Employee>

<Employee>
<Name>GeorgeBush</Name>
<Age>70</Age>
<Gender>M</Gender>
</Employee>
<Employee>
<Name>GeorgeBush</Name>
<Age>80</Age>
<Gender>M</Gender>
</Employee>

</OracleEmployee>

Edited by: Manjit on May 16, 2013 7:32 AM

Edited by: Manjit on May 16, 2013 7:33 AM
  • 1. Re: How to convert text to xml
    1008979 Newbie
    Currently Being Moderated
    Try this way:
    rs= stmt.executeQuery("select * from Table_name");
                   ResultSetMetaData rsmd = rs.getMetaData();
         int colCount = rsmd.getColumnCount();
         StringBuilder b = new StringBuilder("<Table>\n");

         int num = 1;
         while (rs.next()) {
         b.append("<row>");
         b.append("<num>").append(num++).append("</num>");
         for (int i = 1; i <= colCount; i++) {
         String columnName = rsmd.getColumnName(i);
         b.append('<').append(columnName).append('>');
         b.append(rs.getObject(i));
         b.append("</").append(columnName).append('>');
         }
         b.append("</row>\n");
         }
         b.append("</table>");
         System.out.println(b);
         return;
  • 2. Re: How to convert text to xml
    Kayaman Guru
    Currently Being Moderated
    Manjit wrote:
    Hi ,I have this text file that contains Employee Name,Age and Sex.I want to convert it to xml file.See the details below.Wondering if there is and easy way to achieve it.Please share some idea.
    Well, you don't seem to have a CSV or a fixed-width data, so your only choice would be to look for digits to see when the age comes up. Character.isDigit() can be used for that.

    Also, "sex" is censored on the forums???
  • 3. Re: How to convert text to xml
    gimbal2 Guru
    Currently Being Moderated
    Yes to protect the little children that post here. But luckily there is also the word "gender".
  • 4. Re: How to convert text to xml
    Manjit Newbie
    Currently Being Moderated
    Hi Kayaman it is fixed columns lenght file of total 40 bytes in lenght.That is each column width is fix ,and there is no delimiters.

    The impression I am getting is without writing java code it cannot be accomplished.I thought there must be some apache api I can use without writing lot of java code to achieve my objective.


    Manjit
  • 5. Re: How to convert text to xml
    Manjit Newbie
    Currently Being Moderated
    Any ideas please !
  • 6. Re: How to convert text to xml
    gimbal2 Guru
    Currently Being Moderated
    No, there is no Apache API that is built just for your particularly uncommon requirements. Even if there were, figuring out how to make it work on your particular data set will take longer than just doing it yourself.

    You will have to write Java code to do it. Which if you make a little effort will probably be around 10-20 lines in size and uses only very basic Java APIs (String, Reader, Writer).

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points