首先,我为一个工作正常的XML编写了代码。现在,我正在尝试转换多个文件,这会导致错误。我把代码放在多个文件中。 诸如未报告的异常ParserConfigurationException,SAXException之类的错误;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.w3c.dom.Document;
class Xml2Csv {
public static void main(String args[]) throws IOException {
// Path of folder where files are located
String folder_path = "C:\\Users\\SPARK\\Desktop\\javaf\\flir";
// creating new folder
File flir = new File(folder_path);
File[] file_array = flir.listFiles();
for (int i = 0; i < file_array.length; i++)
{
if (file_array[i].isFile())
{
//xsl stylesheet
File stylesheet = new File("C:\\Users\\SPARK\\Desktop\\javaf\\style.xsl");
File xmlSource = new File(folder_path + "\\" + file_array[i]);
String file_name = file_array[i].getName();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(xmlSource);
StreamSource stylesource = new StreamSource(stylesheet);
Transformer transformer = TransformerFactory.newInstance()
.newTransformer(stylesource);
Source source = new DOMSource(document);
Result outputTarget = new StreamResult(new File(file_name + ".csv"));
transformer.transform(source, outputTarget);
}
}
}
}