3

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
[email protected]
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>[email protected]</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();
 }
 }
4
  • I formatted your unreadable question, hope no semantic got lost. Commented Jun 17, 2011 at 8:19
  • Are you sure that both Firstname and Secondname should be "Bống MũnHải" for that second contact? And if so, what's the algorithm - are you just ignoring the second line ("Hải Anh Bống Mũn") and using the first line for both fields? Commented Jun 17, 2011 at 8:20
  • changed the information. Commented Jun 17, 2011 at 8:22
  • I'd like to know, why you changed the accepted answer Commented Jun 20, 2011 at 8:12

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1
  import java.io.BufferedReader;
  import java.io.FileOutputStream;
  import java.io.FileReader;
  import java.util.regex.Pattern;
  import org.xml.sax.ContentHandler;
  import com.sun.org.apache.xml.internal.serialize.OutputFormat;
  import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
  import com.sun.xml.internal.bind.util.AttributesImpl;
  public class Item  {
  public static void main (String args[]) {
  item.readWrite("juin17.part","test.xml");
  }

  public static void readWrite(String fromFile, String toFile)  
  {
try{
    Pattern p = Pattern.compile(".+@.+\\.[a-z]+");
    BufferedReader in = new BufferedReader(new FileReader(fromFile));
    FileOutputStream fos = new FileOutputStream(toFile);
    OutputFormat of = new OutputFormat("XML","windows-1250",true);
    of.setIndent(1);
    of.setIndenting(true);
    XMLSerializer serializer = new XMLSerializer(fos,of);
    ContentHandler hd = serializer.asContentHandler();
    hd.startDocument();
    AttributesImpl atts = new AttributesImpl();
    hd.startElement("","","CONTACTS",atts);
    String line = null,tag;
    while ((line=in.readLine())!=null) {
        if(line.equals("Contact")){
            line=in.readLine();
            hd.startElement("","","CONTACT",atts);
            int i=0;
            while(!line.equals("Contact")){
                if(i==0)
                    tag="FirstName";
                else if(i==1)
                    tag="LastName";
                else{
                    if(p.matcher(line).matches())
                        tag="EMail";
                    else
                        tag="URL";
                }
                hd.startElement("","",tag,atts);
                hd.characters(line.toCharArray(),0,line.length());
                hd.endElement("","",tag);
                i++;
                line=in.readLine();
            }
            hd.endElement("","","CONTACT");
        }
    }
    hd.endElement("","","CONTACTS");
    hd.endDocument();
    fos.close();
    in.close();
    }catch(Exception E){
        System.out.println("Cannot Generate XML!!!");
    }

  }
  }

5 Comments

Hi how do i import these files? import com.sun.org.apache.xml.internal.serialize.OutputFormat; import com.sun.org.apache.xml.internal.serialize.XMLSerializer; import com.sun.xml.internal.bind.util.AttributesImpl; do i have to import ant libraries because i'm using eclipse. Thank u.
These are present in jdk .. U don't need to do anything to import them other than add the lines.
Hi, did u manage to get the output? i have an error message : Exception in thread "main" java.lang.Error: Unresolved compilation problem: item cannot be resolved at Item.main(Item.java:11) Access restriction on required library.
Hi Balanivash, I added package textparser; and I changed like this Item.readWrite("D:/juin17.part","D:/juin17final.xml"); it reads the file and write output as juin17final.xml. Thank u so much :)
Hi, one final question! how do pick the email address according to the domain for example @yahoo.com; @gmail.com like this:<contact> <FirstName>Rangarajkarthik</FirstName> <LastName>karthik Rangaraj</LastName> <EMAILS> <EMail> <type>Yahoo</type> <value>[email protected]</value> </EMail> <EMail> <type>Gmail</type> <value>[email protected]</value> </EMail> </EMAILS> </contact> Thank U.
0
    public static void readFile(File file)  {
    Scanner freader = new Scanner(file);
    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();  
   }

This will help you read a file. Here Item is a class with {Name,LName, Email}, itemList is an array list, so, first part I read the file and store it in an array list, then use the arrayList to create the XML file. Try merging both ie reading and writing into XML at the same time.

5 Comments

Like how do you do what?? merging reading file and XML or file reading
Please explain me after reading file, how can i put it in the array list and how to convert as a xml and then how to save a xml file. Honestly this is my first approach here & I'm bit confused. please give me some more detailed information.
In the above code, itemList.add() adds the elements into the arrayList, and the code in the other answer, reads the arrayList and creates the xml file, the variable xml_file in the other code is the place where the final xml file will be stored
Balanviash, please check my question on the top. I did put the code. Please correct me those mistake. Thank U.
Hmm.. I asked you to merge the code to do both the reading and writing at the same time, not adding one after the other. Ll post the code
0
  1. You need a loop to read the file line by line, I'd suggest a BufferedReader for this.

  2. Inside the loop you are checking for sequences of lines that start and end with 'Contact'. Each of these will be an XML Element. Within those sequences you can create XML Elements for each of the data items, i.e. Firstname, Secondname, Email, URL.

  3. You can check to see if a line is an email address by trying to create an InternetAddress using it. You can check to see if a line is a URL by trying to create a URL from it.

  4. There are two strategies for writing the XML output. The easiest would be to build the whole XML document in memory and then write it to file once the input file has been fully read. If your input file is very large this might not be practical and you might need to write each contact Element to an output stream before starting on the next.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.