Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6032963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:24:50+00:00 2026-05-23T05:24:50+00:00

I have text file and I want to convert this in to a XML

  • 0

I have text file and I want to convert this in to a XML file in java:
Read the file in java and then convert in to xml.

Contact
Arun_niit
Arun_niit
nura_e@yahoo.co.in
Contact
Contact
Bống MũnHải
Hải Anh Bống Mũn
http://www.facebook.com/profile.php?id=689849077
Contact

I want the output in the following order:

<contact>
<Firstname>Arun_niit</Firstname>
<Secondname>Arun_niit</Secondname>
<Email>nura_e@yahoo.co.in</Email>
</contact>
<contact>
<Firstname>Bống MũnHải</Firstname>
<Secondname>Hải Anh Bống Mũn</Secondname>
<URL>http://www.facebook.com/profile.php?id=689849077</URL>
</contact>

Please help me guys & sample codes will be highly appreciated.

Is this correct way of doing it, please…
[code]

  import java.io.*;
  import java.io.ObjectInputStream.GetField;
  import org.w3c.dom.*;
  import javax.xml.parsers.*;
  import javax.xml.transform.*;
  import javax.xml.transform.stream.*;
  import javax.xml.transform.dom.*;
  import java.util.Scanner;

public class Item  {

private String Name;
private String LName;
private String Email;
public static void main (String args[]) {

public static void readFile(File file)  
{
Scanner freader = new Scanner("D:/juin17.part");
String line = null;
while (freader.hasNextLine()) {
    line = freader.nextLine();
   if(line==contact){
Item item = new item();
    item.Name =freader.nextLine();
    item.LName =freader.nextLine();
    item.Email =freader.nextLine();
    freader.nextLine();
    }
    itemList.add(item);
}
freader.close();  
  }

 FileOutputStream fos = new FileOutputStream(info.file);
 OutputFormat of = new OutputFormat("XML","windows-1250",true);
 of.setIndent(1);
 of.setIndenting(true);
 ContentHandler hd = serializer.asContentHandler();
 hd.startDocument();
 AttributesImpl atts = new AttributesImpl();
 hd.startElement("","",info.category,atts);
 for (int i=0;i<itemList.size();i++)
{
   atts.clear();
    Item temp = new Item();
    temp = itemList.get(i);
    hd.startElement("","","FirstName",atts);
    hd.characters(temp.Name.toCharArray(),0,temp.Name.length());
    hd.endElement("","","FirstName");
    hd.startElement("","","LastName",atts);
    hd.characters(temp.LName.toCharArray(),0,temp.LName.length());
    hd.endElement("","","LastName");
    hd.startElement("","","Email",atts);
    hd.characters(temp.mail.toCharArray(),0,temp.mail.length());
    hd.endElement("","","EMail");
 }
 hd.endElement("","",info.category);
 hd.endDocument();
 fos.close();
 }
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T05:24:51+00:00Added an answer on May 23, 2026 at 5:24 am

    here is an example of creating an xml using DOM
    i think you can alter it to fit your own purposes.

    if all that you want is to create an xml from text i think it will be simpler than using SAX

    hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to convert the text file into xml file .I have a large
I have this php code saved in text file, I want to convert this
I have a text file that I want to edit using Java. It has
I have a text file that I want to send over the network, this
I have a text file. I want read that file. But In that if
I have a csv file which I want to convert to xml but the
I have an XML file containing some scientific text that I want to display
I have an word document which I want to convert to text (.txt) file
I have this large XML file. There is a field that I want to
I have a text file that I want to edit by rewriting it to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.