【发布时间】: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