我有一个固定长度字段的平面文件,如下所示:
ret
我想将其转换为xml文件:
ITEM1234LED Light Set
ITEM1235Ratchet Tie
实现这一目标的最佳途径是什么?
谢谢。
答案 0 :(得分:1)
您可以使用简单的XMLStreamWriter
来创建XML文档。无需为记录创建类。只需将ID和描述提取为字符串,然后将这些字符串推送到XML。这也适用于大文件。输入文件和XML文档都不能完全保存在内存中。
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
public class Items {
private static final int POS_ID = 4;
private static final int POS_DESCR = 8;
public static void main(String[] args) {
// Files for input and output
final Path inFile = Paths.get("items.txt");
final Path outFile = Paths.get("items.xml");
// Unfortunately, XMLStreamWriter doesn't implement AutoCloseable,
// so we cannot use it with try-with-resources.
XMLStreamWriter xmlWriter = null;
try(
// BufferedReader for the input file (assuming UTF-8 for the encoding)
BufferedReader reader = Files.newBufferedReader(
inFile, StandardCharsets.UTF_8);
// BufferedOutputStream, so encoding is handled entirely by
// the XMLStreamWriter.
OutputStream out = new BufferedOutputStream(
Files.newOutputStream(outFile));
)
{
// Use a XMLStreamWriter to create the XML document.
xmlWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(out);
xmlWriter.writeStartDocument();
xmlWriter.writeStartElement("ITEMS");
String line;
while((line = reader.readLine()) != null) {
// Parse the input line with fixed length fields
final String id = line.substring(POS_ID, POS_DESCR);
final String descr = line.substring(POS_DESCR);
xmlWriter.writeStartElement("ITEM");
xmlWriter.writeStartElement("ITEMID");
xmlWriter.writeCharacters(id);
xmlWriter.writeEndElement(); // ITEMID
xmlWriter.writeStartElement("DESCRIPTION");
xmlWriter.writeCharacters(descr);
xmlWriter.writeEndElement(); // DESCRIPTION
xmlWriter.writeEndElement(); // ITEM
}
xmlWriter.writeEndElement(); // ITEMS
xmlWriter.writeEndDocument();
} catch (IOException | XMLStreamException | FactoryConfigurationError e) {
e.printStackTrace();
} finally {
// Cleaning up
if(xmlWriter != null) {
try {
xmlWriter.close();
} catch (XMLStreamException e) {
e.printStackTrace();
}
}
}
}
}
答案 1 :(得分:0)
1)创建一个Java类,它映射到平面文件中的数据,例如:
public class Item {
private String itemId;
private String description;
/**
* @return the itemId
*/
public String getItemId() {
return itemId;
}
/**
* @param itemId the itemId to set
*/
public void setItemId(String itemId) {
this.itemId = itemId;
}
/**
* @return the description
*/
public String getDescription() {
return description;
}
/**
* @param description the description to set
*/
public void setDescription(String description) {
this.description = description;
}
}
2)将平面文件解析为“Items”列表(Item对象列表)
3)使用像'xStream'这样的优秀轻量级框架,并使用适当的方法将Java对象序列化为XML文件。例如:xStream.toXml(Object obj, Writer out)
PS:这只是一种标准方式(使用经过良好测试的框架,因此,不是重新发明轮子),但不是最佳方案。最佳地,为了提高性能和减少内存占用,您可以解析平面文件并同时写入XML文件。答案 2 :(得分:-1)
创建java对象以表示您的逻辑数据结构。
解析平面文件并生成java对象。
使用XML库(例如JAXB)将java对象树序列化为文件。
答案 3 :(得分:-1)
答案 4 :(得分:-1)
我认为bchetty提到的内容很好,但您不需要任何XML库来输出XML。
PrintWriter out = new PrintWriter (file);
out.println ("<ITEMS>");
for (Item item : Items)
{
out.println (" <ITEM>");
out.println (" <ITEMID>" + item.getItemId() + "</ITEMID>");
out.println (" <DESCRIPTION>" + item.getDescription () + "</DESCRIPTION>");
out.println (" </ITEM>");
}
out.println ("</ITEMS>");
out.close ();