【发布时间】:2016-07-21 11:00:42
【问题描述】:
我正在尝试编写一个 java 程序,它将读取不同巴士站信息的 csv 文件并将它们转换为 xml 文件并保存为新的 xml 文件。我从朋友那里得到了一些代码的帮助,但我无法理解问题出在哪里,因为他们忘记将 cmets 包含在代码中。任何修复代码的帮助将不胜感激。代码如下所示。
public class converter {
protected DocumentBuilderFactory domFactory = null;
protected DocumentBuilder domBuilder = null;
public static void main(String[] args) {
ArrayList<String> busStopInfo = new ArrayList<String>(7);
File file = new File("stops.csv");
BufferedReader readFile = null;
try {
DocumentBuilderFactory df = DocumentBuilderFactory.newInstance();
DocumentBuilder db = df.newDocumentBuilder();
Document document = db.newDocument();
Element rootElement = document.createElement("BusStops");
document.appendChild(rootElement);
readFile = new BufferedReader(new FileReader(file));
int line = 0;
String information = null;
while ((information = readFile.readLine()) != null) {
String[] rowValues = information.split(",");
if (line == 0) {
for (String columnInfo : rowValues) {
busStopInfo.add(columnInfo);
}
} else {
Element childElement = document.createElement("details");
rootElement.appendChild(childElement);
for (int columnInfo = 0; columnInfo < busStopInfo.size(); columnInfo++) {
String header = busStopInfo.get(columnInfo);
String value = null;
if (columnInfo < rowValues.length) {
value = rowValues[columnInfo];
} else {
value = " ";
}
Element current = document.createElement(header);
current.appendChild(document.createTextNode(value));
childElement.appendChild(current);
System.out.println(value);
}
}
line++;
}
Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tf.setOutputProperty(OutputKeys.INDENT, "yes");
Writer output = new StringWriter();
tf.transform(new DOMSource(document), new StreamResult(output));
System.out.println(output.toString());
} catch (Exception e) {
}
}
}
下面是csv文件的摘录
AtcoCode、CommonName、LocalityName、ParentLocalityName、纬度、经度 0100BRP90336,The Centre,Bristol City Centre,布里斯托,51.4543379612,-2.5978824115 0170SGA56570,UWE Entrance North,Abbey Wood,,51.50419145,-2.549547265 079073001Z,Bus Station Express Lounge,Middlesbrough,,54.5760020508,-1.2391798779 0800COC31523,Bus Station,Newquay,,50.4130339395,-5.0856695446 0800COC56586,Bus Station,Camborne,,50.2132677521,-5.2974299693
这是我要复制的 xml 文件的架构
<xs:element name="Busstops">
<xs:element name="stops" minOccurs="1" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="AtcoCode" type="xs:string" minOccurs="1" maxOccurs="1"/>
<xs:element name="CommonName" type="xs:string" "minOccurs="1" maxOccurs="1"/>
<xs:element name="LocalityName" type="xs:string" "minOccurs="1" maxOccurs="1"/>
<xs:element name="ParentLocalityName" type="xs:string" "minOccurs="0" maxOccurs="1"/>
<xs:element name="Longitude" type="xs:string" "minOccurs="1" maxOccurs="1"/>
<xs:element name="Latitude" type="xs:string" "minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
【问题讨论】:
-
代码有什么问题?哪些方面没有达到您的预期?
-
它没有输出和保存文件,我对这种语言没有太多经验,所以我很不确定是否缺少某些东西。我运行代码,没有任何迹象表明发生了什么事。
-
您需要从stops.csv 中提供几行来显示预期的输入。如果您也有预期输出的简短示例,那将是最好的。
-
编辑添加您需要的内容
-
@Granto867 欢迎来到 SO。你不应该在回答后破坏问题。它应该留在网站上,以帮助将来遇到类似问题的人。