我有一组要转换为 XML 的 CSV 数据。代码看起来不错,但输出不够完美。它省略了一些列,因为它们没有价值,并生成一长串 XML 数据而不是破坏它。
这是我的 CSV 数据示例:
Name Age Sex
chi 23
kay 19 male
John male
我的代码:
public class XMLCreators {
// Protected Properties
protected DocumentBuilderFactory domFactory = null;
protected DocumentBuilder domBuilder = null;
public XMLCreators() {
try {
domFactory = DocumentBuilderFactory.newInstance();
domBuilder = domFactory.newDocumentBuilder();
} catch (FactoryConfigurationError exp) {
System.err.println(exp.toString());
} catch (ParserConfigurationException exp) {
System.err.println(exp.toString());
} catch (Exception exp) {
System.err.println(exp.toString());
}
}
public int convertFile(String csvFileName, String xmlFileName,
String delimiter) {
int rowsCount = -1;
try {
Document newDoc = domBuilder.newDocument();
// Root element
Element rootElement = newDoc.createElement("XMLCreators");
newDoc.appendChild(rootElement);
// Read csv file
BufferedReader csvReader;
csvReader = new BufferedReader(new FileReader(csvFileName));
int fieldCount = 0;
String[] csvFields = null;
StringTokenizer stringTokenizer = null;
// Assumes the first line in CSV file is column/field names
// The column names are used to name the elements in the XML file,
// avoid the use of Space or other characters not suitable for XML element
// naming
String curLine = csvReader.readLine();
if (curLine != null) {
// how about other form of csv files?
stringTokenizer = new StringTokenizer(curLine, delimiter);
fieldCount = stringTokenizer.countTokens();
if (fieldCount > 0) {
csvFields = new String[fieldCount];
int i = 0;
while (stringTokenizer.hasMoreElements())
csvFields[i++] = String.valueOf(stringTokenizer.nextElement());
}
}
// At this point the coulmns are known, now read data by lines
while ((curLine = csvReader.readLine()) != null) {
stringTokenizer = new StringTokenizer(curLine, delimiter);
fieldCount = stringTokenizer.countTokens();
if (fieldCount > 0) {
Element rowElement = newDoc.createElement("row");
int i = 0;
while (stringTokenizer.hasMoreElements()) {
try {
String curValue = String.valueOf(stringTokenizer.nextElement());
Element curElement = newDoc.createElement(csvFields[i++]);
curElement.appendChild(newDoc.createTextNode(curValue));
rowElement.appendChild(curElement);
} catch (Exception exp) {
}
}
rootElement.appendChild(rowElement);
rowsCount++;
}
}
csvReader.close();
// Save the document to the disk file
TransformerFactory tranFactory = TransformerFactory.newInstance();
Transformer aTransformer = tranFactory.newTransformer();
Source src = new DOMSource(newDoc);
Result result = new StreamResult(new File(xmlFileName));
aTransformer.transform(src, result);
rowsCount++;
// Output to console for testing
// Resultt result = new StreamResult(System.out);
} catch (IOException exp) {
System.err.println(exp.toString());
} catch (Exception exp) {
System.err.println(exp.toString());
}
return rowsCount;
// "XLM Document has been created" + rowsCount;
}
}
当对上述数据执行此代码时,它会生成:
<?xml version="1.0" encoding="UTF-8"?>
<XMLCreators>
<row>
<Name>chi</Name>
<Age>23</Age>
</row>
<row>
<Name>kay</Name>
<Age>19</Age>
<sex>male</sex>
</row>
<row>
<Name>john</Name>
<Age>male</Age>
</row>
</XMLCreators>
我自己以这种形式安排了它,但输出产生了很长的一行。要产生的输出应该是:
<?xml version="1.0" encoding="UTF-8"?>
<XMLCreators>
<row>
<Name>chi</Name>
<Age>23</Age>
<sex></sex>
</row>
<row>
<Name>kay</Name>
<Age>19</Age>
<sex>male</sex>
</row>
<row>
<Name>john</Name>
<Age></Age>
<sex>male</sex>
</row>
</XMLCreators>
原文由 lee 发布,翻译遵循 CC BY-SA 4.0 许可协议
我同意 Kennet 的观点。
我简单地补充说
这在元素之间添加了一个新行并允许缩进。
更新
让我们从您提供的文件不是 CSV(逗号分隔值)文件这一事实开始,我会让您担心这个问题……
现在我在这里使用了
List
而不是Map
。您需要决定如何最好地解决缺失值问题。如果事先不知道文件的结构,这将不是一个简单的解决方案。无论如何,我最终得到
通过合并更新
使用 OpenCSV 更新
下次更新 (2022)
因此,例如,使用类似…
它会产生类似…的输出
可运行的例子