gpt4 book ai didi

java - 将 XML 内容保存到 CSV 文件中

转载 作者:行者123 更新时间:2023-12-01 09:08:31 25 4
gpt4 key购买 nike

我有下面的代码,它包含所有 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]");
} } }}

你能帮我解决一下吗?如果您知道如何解决该问题,那就太好了。

谢谢!

最佳答案

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

首先,打开编写器:

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!

编辑

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

关于java - 将 XML 内容保存到 CSV 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41067746/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com