开发者

text file to xml conversion java

开发者 https://www.devze.com 2023-03-13 10:45 出处:网络
I have text file and I want to convert this in to a XML file in java: Readthe file in java and then convert in to xml.

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();
 }
 }


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.


  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!!!");
    }

  }
  }


    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.


  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.

0

精彩评论

暂无评论...
验证码 换一张
取 消