java 文本文件到xml转换java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6383104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
text file to xml conversion java
提问by user838691
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.
我有文本文件,我想将其转换为 Java 中的 XML 文件:在 Java 中读取文件,然后转换为 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();
}
}
回答by yossi
回答by Balanivash
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!!!");
}
}
}
回答by Balanivash
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.
这将帮助您阅读文件。这里的 Item 是一个带有 {Name,LName, Email} 的类,itemList 是一个数组列表,所以,首先我读取文件并将其存储在一个数组列表中,然后使用 arrayList 创建 XML 文件。尝试合并同时读取和写入 XML。
回答by Qwerky
You need a loop to read the file line by line, I'd suggest a
BufferedReader
for this.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 XMLElement
s for each of the data items, i.e. Firstname, Secondname, Email, URL.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 aURL
from it.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.
您需要一个循环来逐行读取文件
BufferedReader
,为此我建议使用一个循环。在循环内部,您正在检查以“Contact”开头和结尾的行序列。这些中的每一个都将是一个 XML
Element
。在这些序列中,您可以Element
为每个数据项创建 XML ,即名字、第二名、电子邮件、URL。您可以通过尝试创建一个
InternetAddress
使用它来检查一行是否是电子邮件地址。您可以通过尝试从中创建一个来检查一行是否是一个 URLURL
。有两种编写 XML 输出的策略。最简单的方法是在内存中构建整个 XML 文档,然后在完全读取输入文件后将其写入文件。如果您的输入文件非常大,这可能不切实际,您可能需要
Element
在开始下一个联系人之前将每个联系人写入输出流。