使用Java将CSV文件转换为XML文件

使用Java将CSV文件转换为XML文件,java,xml,csv,converter,Java,Xml,Csv,Converter,我正在尝试编写一个java程序,该程序将读取不同公交站点信息的csv文件,并将其转换为xml文件,另存为新的xml文件。我从一位朋友那里得到了一些代码方面的帮助,但我无法理解问题是什么,因为他们忘了在代码中包含注释。如果您能帮助修复代码,我们将不胜感激。代码如下所示 public class converter { protected DocumentBuilderFactory domFactory = null; protected DocumentBuilder domBuilder =

我正在尝试编写一个java程序,该程序将读取不同公交站点信息的csv文件,并将其转换为xml文件,另存为新的xml文件。我从一位朋友那里得到了一些代码方面的帮助,但我无法理解问题是什么,因为他们忘了在代码中包含注释。如果您能帮助修复代码,我们将不胜感激。代码如下所示

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) {

    }
} 
公共类转换器{
受保护的DocumentBuilderFactory domFactory=null;
受保护的DocumentBuilder domBuilder=null;
公共静态void main(字符串[]args){
ArrayList busStopInfo=新的ArrayList(7);
文件=新文件(“stops.csv”);
BufferedReader readFile=null;
试一试{
DocumentBuilderFactory df=DocumentBuilderFactory.newInstance();
DocumentBuilder db=df.newDocumentBuilder();
Document Document=db.newDocument();
Element rootElement=document.createElement(“总线站”);
document.appendChild(rootElement);
readFile=new BufferedReader(new FileReader(file));
内线=0;
字符串信息=null;
而((information=readFile.readLine())!=null){
String[]rowValues=information.split(“,”);
如果(行==0){
用于(字符串列信息:行值){
busStopInfo.add(columnInfo);
}
}否则{
元素childElement=document.createElement(“详细信息”);
appendChild(childElement);
对于(int columnifo=0;columnifo
}

下面是csv文件的摘录

AtcoCode、CommonName、LocationName、ParentLocationName、纬度、经度 0100BRP90336,布里斯托尔市中心中心,布里斯托尔,51.4543379612,-2.5978824115 0170SGA56570,艾比伍德大学北入口,51.50419145,-2.549547265 米德尔斯堡汽车站快捷休息室079073001Z,54.5760020508,-1.2391798779 0800OC31523,纽基汽车站,50.4130339395,-5.0856695446 0800COC56586,坎本汽车站,50.2132677521,-5.2974299693

这是我试图复制的xml文件的模式

<xs:element name="Busstops">



此代码正在生成一个输出,该输出将.csv转换为XML文档(显示在标准输出上)

根据提供的.csv摘录,代码运行良好,并在标准输出上生成xml:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<BusStops>
<details>
<AtcoCode>0100BRP90336</AtcoCode>
<CommonName>The Centre</CommonName>
<LocalityName>Bristol City Centre</LocalityName>
<ParentLocalityName>Bristol</ParentLocalityName>
<Latitude>51.4543379612</Latitude>
<Longitude>-2.5978824115</Longitude>
</details>
...
</BusStops>

将起作用。

代码有什么问题?在什么方面做不到你所期望的?它不是输出和保存文件,我对这种语言没有太多的经验,所以我非常不确定是否有什么遗漏。我运行了代码,但没有任何迹象表明发生了任何事情。您需要提供stops.csv中的几行代码来显示预期的输入。如果您也有一个预期输出的简短示例,这将是最好的need@Granto867欢迎来到SO。你不应该在问题得到回答后就把它毁掉。它应该留在网站上,以帮助人们与类似的问题在未来。我已经改变了它的行提供和运行代码没有改变活动。代码似乎没有任何作用。我是否需要手动创建stops.xml文件,或者代码会为我创建该文件?@Granto867,代码确实有效,并根据sample.csv生成答案中显示的输出。我可能建议输入stops.csv文件的完整路径(例如,
新文件(“c:/tmp/stops.csv”)
。该程序将使用PrintWriter中指定的名称创建一个.xml文件。(可能还应使用完整路径)。与模式相比,.xml作为元素具有“详细信息”而不是“stops”,但这在代码中很容易修复。
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<BusStops>
<details>
<AtcoCode>0100BRP90336</AtcoCode>
<CommonName>The Centre</CommonName>
<LocalityName>Bristol City Centre</LocalityName>
<ParentLocalityName>Bristol</ParentLocalityName>
<Latitude>51.4543379612</Latitude>
<Longitude>-2.5978824115</Longitude>
</details>
...
</BusStops>
PrintWriter writer = new PrintWriter("the-file-name.xml", "UTF-8");
writer.println(output.toString());
writer.close();