Java - 空XML节点

时间:2017-07-06 07:15:32

标签: java xml csv

我正在尝试创建一个XML文件,当我运行此代码时,它成功创建了XML文件,但是空节点。我的数据没有解析。这是代码 -

public class xml {

    public static void main(String[] args) {
        List<String> headers = new ArrayList<String>(5);

        File file = new File("C:/Users/Admin/Desktop/data.csv");
        BufferedReader reader = null;

        try {

            DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder domBuilder = domFactory.newDocumentBuilder();

            Document newDoc = domBuilder.newDocument();
            // Root element
            Element rootElement = newDoc.createElement("root");
            newDoc.appendChild(rootElement);

            reader = new BufferedReader(new FileReader(file));
            int line = 0;

            String text = null;
            while ((text = reader.readLine()) != null) {

                StringTokenizer st = new StringTokenizer(text, "?", false);
                String[] rowValues = new String[st.countTokens()];
                int index = 0;
                while (st.hasMoreTokens()) {

                    String next = st.nextToken();
                    rowValues[index++] = next;

                }

                // String[] rowValues = text.split(",");

                if (line == 0) { // Header row
                    for (String col : rowValues) {
                        headers.add(col);
                    }
                } else { // Data row
                    Element rowElement = newDoc.createElement("row");
                    rootElement.appendChild(rowElement);

                    for (int col = 0; col < headers.size(); col++) {
                        String header = headers.get(col);
                        String value = null;

                        if (col < rowValues.length) {
                            value = rowValues[col];
                        } else {
                            // ?? Default value
                            value = " ";
                        }

                        System.out.println(header + " " + value);

                    }
                }
                line++;
            }

            try {

                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(newDoc);
                Result result = new StreamResult(new File("C:/Users/Admin/Desktop/data.xml"));

                aTransformer.transform(src, result);
                System.out.println("File creation successfully!");

            } catch (Exception exp) {
                exp.printStackTrace();
            } finally {
                try {

                } catch (Exception e1) {
                }
                try {

                } catch (Exception e1) {
                }
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }

    }
}

我做错了什么,不能识别错误。

编辑: -

示例csv文件和输出: -

 Symbol,Open,High,Low,Last Traded Price,Change
"NIFTY 50","9,645.90","9,650.65","9,600.95","9,609.30","-5.70"
"RELIANCE","1,390.00","1,414.20","1,389.00","1,407.55","26.50"
"BPCL","647.70","665.00","645.95","660.10","10.75"
"ADANIPORTS","368.50","373.80","368.00","372.25","4.25"
"ONGC","159.50","161.75","159.35","160.80","1.70"

输出: -

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
    <row>"NIFTY 50","9,645.90","9,650.65","9,600.95","9,609.30","-5.70"</row>
    <row>"RELIANCE","1,390.00","1,414.20","1,389.00","1,407.55","26.50"</row>
    <row>"BPCL","647.70","665.00","645.95","660.10","10.75"</row>
    <row>"ADANIPORTS","368.50","373.80","368.00","372.25","4.25"</row>
    <row>"ONGC","159.50","161.75","159.35","160.80","1.70"</row>
</root>

1 个答案:

答案 0 :(得分:0)

您不使用变量value。因此,您应该在行rowElement.setTextContent(value);之前添加一行,例如System.out.println(header + " " + value);