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

问题描述 投票:0回答:1

我正在尝试编写一个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) {

    }
} 

}

下面是csv文件的摘录

AtcoCode,CommonName,LocalityName,ParentLocalityName,Latitude,Longitude 0100BRP90336,The Center,Bristol City Center,Bristol,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>

java xml csv converter
1个回答
0
投票

此代码生成一个输出,将读取的.csv转换为XML文档(显示在stdout上)。

根据提供的.csv摘录,代码运行正常,并在stdout上生成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>

如果不是想在stdout上显示它而是将其写入文件,你可以简单地将stdout重定向到文件(最简单的方法)。

否则,更改System.out.println(output.toString())以写入文件。就像是:

PrintWriter writer = new PrintWriter("the-file-name.xml", "UTF-8");
writer.println(output.toString());
writer.close();

将工作。

© www.soinside.com 2019 - 2024. All rights reserved.