我设法让文件输出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();
}
}
}
答案 0 :(得分:1)
</ParentLocalityName>
是空标记
<ParentLocalityName></ParentLocalityName>
<ParentLocalityName/>
是 end 标记
不一样。
空标记(see XML spec)是开始标记和结束标记的简写。
Authorization
以上两行的含义完全相同,一旦解析就无法区分。
BTW:你的头衔是假的:你不能使用 DOM 解析 CSV文件。 您正在阅读CSV文件并使用DOM 编写 XML。