将 XML 中的内容保存到 CSV 文件中

Save content from XML into CSV file

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

现在,我在beginig上打开了stream writer,但是我不知道如何添加到方法中:

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]");
    }    }  }}

你能帮我一下吗?如果你知道如何解决这个问题就太好了。

谢谢!

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

首先打开编写器:

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();

完成后别忘了关闭您的编写器!

编辑

关闭编写器示例:

  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());
        }
    }