Solved

Use JAVA to convert tab delimited text file to xml

Posted on 2006-06-21
4
652 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:mayankeagle
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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:

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now