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

时间:2016-12-09 19:44:12

标签: java xml xml-parsing xmldocument filewriter

我的代码低于所有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]");
    }    }  }}
你能帮帮我吗?如果你知道如何解决这个问题会很棒。

谢谢!

1 个答案:

答案 0 :(得分: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();

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

修改

关闭作者的示例:

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