gpt4 book ai didi

java - 如何将 XML 文件的输出写入 Excel 文件?

转载 作者:太空宇宙 更新时间:2023-11-04 11:22:10 25 4
gpt4 key购买 nike

我有一个 XML 文件。我读取了该文件并修改了一些内容,现在我想将该 xml 文件的输出写入 Excel 中。你们能指导我如何做到这一点吗?我的意思是我需要创建另一个输出 xml 文件来存储结果,然后需要转换为 excel,或者直接我可以将输出写入 excel。任何线索都会有帮助

这是我的 Java 代码

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;

public class App {

static File[] sourceDir = null;
static File[] targetDir = null;
static String currDir = System.getProperty("user.dir");
static int count=0;
public static void main(String argv[]) {

sourceDir = readDirectory(currDir + "\\Source");
System.out.println("sourceDir::::::::"+sourceDir);
targetDir = readDirectory(currDir + "\\Target");

try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
//File fXmlFile = new File("D:\\Users\\gpadarth\\Desktop\\jyoti\\xml\\Log_MoveWorkers_467_1.xml");
for(int i=0;i<sourceDir.length;i++){
Document doc = dBuilder.parse(sourceDir[i]);
System.out.println("doc::::"+doc);
//optional, but recommended
//read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
doc.getDocumentElement().normalize();

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

NodeList nList = doc.getElementsByTagName("Worker");

System.out.println("----------------------------");

for (int temp = 0; temp < nList.getLength(); temp++) {

Node nNode = nList.item(temp);

//System.out.println("\nCurrent Element :" + nNode.getNodeName());

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

Element eElement = (Element) nNode;

if( eElement.getElementsByTagName("Status").item(0).getTextContent().equals("Fail")){
System.out.println("Employee_ID : " + eElement.getElementsByTagName("Employee_ID").item(0).getTextContent());
System.out.println("Position_ID : " + eElement.getElementsByTagName("Position_ID").item(0).getTextContent());
System.out.println("Status : " + eElement.getElementsByTagName("Status").item(0).getTextContent());
System.out.println("Fault_String : " + eElement.getElementsByTagName("Fault_String").item(0).getTextContent());
System.out.println("\n\n::::::::::::::::::::::::::::::::::::::::::::::::::");
}}
}
count++;
System.out.println("File "+count+ " Started ");
} }catch (Exception e) {
e.printStackTrace();
}
}

private static File[] readDirectory(String dirToRead){
File folder = new File(dirToRead);
return folder.listFiles();
}


}

最佳答案

您应该使用apache poi读取或生成Excel文件

关于java - 如何将 XML 文件的输出写入 Excel 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44780248/

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