Solved

Use JAVA to convert tab delimited text file to xml

Posted on 2006-06-21
4
662 Views
Last Modified: 2012-06-27
hi to any experts out there...

I have a tab-delimited text file "contents.txt" with the following contents :-

contents.txt
--------------
FE Global Electronics Pte Ltd          kh.loh@fe-electronics.com     12345678     12345678     Buyer          595171083    
Electronic Components     Manufacturer    


How to convert the text file to a xml file using java programming?

desired output of the xml file: -

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE Pip3A4PurchaseOrderRequest SYSTEM "3A4_MS_V02_03_PurchaseOrderRequest.dtd"><Pip3A4PurchaseOrderRequest>
   <fromRole>
      <PartnerRoleDescription>
         <ContactInformation>
            <contactName>
               <FreeFormText>FE Global Electronics Pte Ltd</FreeFormText>
            </contactName>
            <EmailAddress>kh.loh@fe-electronics.com</EmailAddress>
            <facsimileNumber>
               <CommunicationsNumber/>
            </facsimileNumber>
            <telephoneNumber>
               <CommunicationsNumber/>
            </telephoneNumber>
         </ContactInformation>
         <GlobalPartnerRoleClassificationCode>Buyer</GlobalPartnerRoleClassificationCode>
         <PartnerDescription>
            <BusinessDescription>
               <GlobalBusinessIdentifier>595171083</GlobalBusinessIdentifier>
               <GlobalSupplyChainCode>Electronic Components</GlobalSupplyChainCode>
            </BusinessDescription>
            <GlobalPartnerClassificationCode>Manufacturer</GlobalPartnerClassificationCode>
         </PartnerDescription>
      </PartnerRoleDescription>
   </fromRole>
</Pip3A4PurchaseOrderRequest>
0
Comment
Question by:feliciayong
4 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 16956894
You can use this for creating XML files:

http://www.javaalmanac.com/egs/org.w3c.dom/pkg.html

0
 
LVL 24

Expert Comment

by:slyong
ID: 16956898
0
 
LVL 10

Accepted Solution

by:
mukundha_expert earned 500 total points
ID: 16957034


            String in = "FE Global Electronics Pte Ltd\tkh.loh@fe-electronics.com\t12345678\t12345678\tBuyer\t595171083\tElectronic Components\tManufacturer";
            
            String[] str = in.split( "\t" ) ;
            
            for ( int i = 0 ; i < str.length ; i ++ )
                  System.out.println( str[i] ) ;
            
            DocumentBuilderFactory fact = DocumentBuilderFactory .newInstance() ;
            DocumentBuilder builder = fact.newDocumentBuilder() ;
            org.w3c.dom.Document doc = builder.newDocument() ;
            
            Element root = doc.createElement( "Pip3A4PurchaseOrderRequest" ) ;
            doc.appendChild( root ) ;

Similarly add all the nodes to the root
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16957581
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question