gpt4 book ai didi

java - 如何使用 DOM 正确解析我的 csv 文件?

转载 作者:行者123 更新时间:2023-12-01 10:06:33 24 4
gpt4 key购买 nike

我已经设法让文件输出 XML。但是由于某种原因,当parentLocalityName中不存在元素时,只有结束标记会像这样出现,正如您所看到的,只有结束标记出现。我如何更改修改我的代码,以便显示整个标签,但其中没有任何内容?

-<BusStopDetails>

<AtcoCode>0800COC31523</AtcoCode>

<CommonName>Bus Station</CommonName>

<LocalityName>Newquay</LocalityName>

<ParentLocalityName/>

<Latitude>50.4130339395</Latitude>

<Longitude>-5.0856695446</Longitude>

</BusStopDetails>

这是我转换文件的 Java 程序:

import java.io.*;
import java.util.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;

public class CSV2XML {

// Protected Properties
protected DocumentBuilderFactory domFactory = null;
protected DocumentBuilder domBuilder = null;

// CTOR
public CSV2XML() {
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());
}
}

@SuppressWarnings("resource")
public static void main(String[] args) throws IOException, TransformerException {
ArrayList<String> busStopInfo = new ArrayList<String>(7);
// An array list has been used to store the elements from the csv file. They are stored as strings.
try {

File file = new File("C:\\Users\\liaml\\OneDrive\\Documents\\CSCU9T4 XML assignment\\lrl00002\\stops.csv");
BufferedReader readFile = null;

DocumentBuilderFactory df = DocumentBuilderFactory.newInstance();
DocumentBuilder db;

db = df.newDocumentBuilder();

Document doc = db.newDocument();

// Root element
Element rootElement = doc.createElement("BusStops"); // root element "Busstops created"

doc.appendChild(rootElement);
readFile = new BufferedReader(new FileReader(file));
int line = 0; // Represent the lines in the file, starts at the 0th,
// increments after every line has been tokenised
// for elements line of the file

String information = null;
while ((information = readFile.readLine()) != null) {

String[] tokens = information.split(","); // removes comma until there is no more commas

String[] row = information.split(","); // store elements after the command, length 6 to store headers



if (line == 0) {
for (String column : row) {
busStopInfo.add(column); // This will add column headers from rowValues to busStopInfo ArrayList

}
} else {
Element childElement = doc.createElement("BusStopDetails"); // creates child element details

rootElement.appendChild(childElement);
for (int column = 0; column < busStopInfo.size(); column++) {

String header = busStopInfo.get(column);
String value = null;

if (column < row.length) {
value = row[column];
} else {
value = " ";
}

Element current = doc.createElement(header); // creates element of current header

current.appendChild(doc.createTextNode(value)); // creates placement for value

childElement.appendChild(current); // adds current value of the header into child element details


// Save the document to the disk file
TransformerFactory tranFactory = TransformerFactory.newInstance();
Transformer aTransformer = tranFactory.newTransformer();
aTransformer.setOutputProperty(OutputKeys.INDENT, "yes");
aTransformer.setOutputProperty(OutputKeys.METHOD, "xml");
aTransformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
Source src = new DOMSource(doc);
information = ("C:\\Users\\liaml\\OneDrive\\Documents\\CSCU9T4 XML assignment\\lrl00002\\stops.xml");
Result dest = new StreamResult(new File(information));
aTransformer.transform(src, dest);


if (line >= 0) {
System.out.println("CSV File has been successfully converted to XML File & Stored in Zip Folder "
+ "(" + String.valueOf(line) + " row)");
} else {
System.out.println(
"Error while converting input CSV File " + args[0] + " to output XML File " + args[1] + " ");
}

}
}
line++;
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
}

}
}

最佳答案

<ParentLocalityName/>是一个标签。
</ParentLocalityName>结束标签。
不是同一件事。

空标签 ( see XML spec ) 是开始标签和结束标签的简写。

<ParentLocalityName></ParentLocalityName>
<ParentLocalityName/>

上面两行的意思完全一样,解析后无法区分。

顺便说一句:您的标题是假的:您无法使用 DOM 来解析 CSV 文件。
您正在读取 CSV 文件并使用 DOM写入 XML。

关于java - 如何使用 DOM 正确解析我的 csv 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36413230/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com