我有一个XML文件。我读取文件并修改了一些内容,现在我想将该xml文件的输出写入excel。你们可以指导我如何做到这一点。我的意思是我需要创建另一个输出xml文件来存储结果,然后需要转换为excel或直接我可以将输出写入excel.Any lead将是有帮助的
这是我的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();
}
}