【问题标题】:Save content from XML into CSV file将 XML 中的内容保存到 CSV 文件中
【发布时间】:2016-12-09 19:44:11
【问题描述】:

我有以下所有 XML 内容的代码。

现在,我在 beginig 上打开了流编写器,但我不知道如何添加到方法中:

bw.write

ReadXML.java

public class ReadXML {

  public static void main(String[] args) {

    try {

    File file = new File("C:\\test.xml");
    File outputFile = new File("C:\\test.csv");

    DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = dBuilder.parse(file);
    BufferedWriter bw = null;
    FileWriter fw = null;

    System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

    if (doc.hasChildNodes()) {
        printNote(doc.getChildNodes());
    }
    } catch (Exception e) {
    System.out.println(e.getMessage());
    }
  }

  private static void printNote(NodeList nodeList) {

    for (int count = 0; count < nodeList.getLength(); count++) {

    Node tempNode = nodeList.item(count);

    if (tempNode.getNodeType() == Node.ELEMENT_NODE) {

        System.out.println("\nNode Name =" + tempNode.getNodeName() + " [OPEN]");
        System.out.println("Node Value =" + tempNode.getTextContent());

        if (tempNode.hasAttributes()) {

            // get attributes names and values
            NamedNodeMap nodeMap = tempNode.getAttributes();

            for (int i = 0; i < nodeMap.getLength(); i++) {
                Node node = nodeMap.item(i);
                System.out.println("attr name : " + node.getNodeName());
                System.out.println("attr value : " + node.getNodeValue());
            }
        }
        if (tempNode.hasChildNodes()) {
            // loop again if has child nodes
            printNote(tempNode.getChildNodes());
        }
        System.out.println("Node Name =" + tempNode.getNodeName() + " [CLOSE]");
    }    }  }}

你能帮帮我吗?如果你知道如何解决这个问题会很棒。

谢谢!

【问题讨论】:

  • “添加到方法”是什么意思?您想将哪些数据写入 CSV 文件?顺便说一句,你的作家没有打开,它是空的。
  • 现在我想保存所有数据,所以 tempNode.getTextContent());,应该添加到 bw

标签: java xml xml-parsing xmldocument filewriter


【解决方案1】:

好的,仍然不确定您的问题到底是什么,但也许这会有所帮助。

首先,打开编写器:

final BufferedWriter w = new BufferedWriter(new FileWriter(outputFile));

然后传给printNote:

printNote(doc.getChildNodes(), w);

相应地修改方法:

private static void printNote(final NodeList nodeList, final BufferedWriter w) throws IOException {
    // ...
}

当您拥有要写入文件的节点时,请执行以下操作:

w.write(node.getTextContent());
w.newLine();

完成后别忘了关闭你的作家!

编辑

关闭 writer 的示例:

  1. 老派

    public static void mainv1(String[] args) {
    
        File file = new File("C:\\test.xml");
        File outputFile = new File("C:\\test.csv");
    
        BufferedWriter bw = null;
    
        try {
            DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            Document doc = dBuilder.parse(file);
    
            System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
    
            // Open in try because FileWriter constructor throws IOException
            bw = new BufferedWriter(new FileWriter(outputFile));
    
            if (doc.hasChildNodes()) {
                printNote(doc.getChildNodes(), bw);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            // Check for null because bw won't be initialized if document parsing failed
            if (bw != null) {
                try {
                    bw.close();
                } catch (final IOException e) {
                    // Log error
                }
            }
        }
    }
    
  2. Java7 及更高版本

    public static void main(String[] args) {
    
        File file = new File("C:\\test.xml");
        File outputFile = new File("C:\\test.csv");
    
        // Since Java7 you can use try-with-resources
        // The finally block closing the writer will be created automatically
        try (BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile))) {
    
            DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            Document doc = dBuilder.parse(file);
    
            System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
    
            if (doc.hasChildNodes()) {
                printNote(doc.getChildNodes(), bw);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
    

【讨论】:

    猜你喜欢
    • 2020-07-17
    • 1970-01-01
    • 2014-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-05
    • 1970-01-01
    相关资源
    最近更新 更多