我正在尝试将一个巨大的.xml文件转换为.csv文件,以下是我用来读取xml并将文件写入csv的代码。
public class XMLToCSVMappings {
private static XPathFactory xPathFactory = null;
private static DocumentBuilderFactory domFactory = null;
public static void main(String args[]) {
domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
xPathFactory = XPathFactory.newInstance();
TransformerFactory.newInstance();
ReadXML();
}
public static void ReadXML() {
System.out.println("In ReadXML method");
File xmlFile = new File("C:\\Users\\maya\\Desktop\\AllObjects.xml");
try {
InputStream fis = new FileInputStream(xmlFile);
if (fis != null) {
Document xmlDoc = getDocFromXMLString(fis);
HashMap<String, String> propertiesKeypair = readPropertyFile();
FileWriter writer = new FileWriter("C:\\Users\\maya\\Desktop\\AllObjects.csv");
writer.append("Key");
writer.append(',');
writer.append("Value");
writer.append('\n');
for (Map.Entry<String, String> entry : propertiesKeypair .entrySet()) {
System.out.println("Key : " + entry.getKey() + "Xpath value is::"+ getElementValue(entry.getValue(), xmlDoc));
writer.append(entry.getKey());
writer.append(',');
writer.append(getElementValue(entry.getValue(), xmlDoc));
writer.append('\n');
}
writer.flush();
writer.close();
System.out.println("ResultMap Updated. CSV File is being generated...");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e)
{
e.printStackTrace();
}
}
public static Document getDocFromXMLString(InputStream xml)
throws Exception {
DocumentBuilder builder;
Document doc;
try {
builder = domFactory.newDocumentBuilder();
doc = builder.parse(xml);
}
catch (Exception exception) {
throw exception;
}
finally {
}
return doc;
}
public static String getElementValue(final String xpathExpression,
final Document doc) {
String textValue = null;
try {
XPath xpath = xPathFactory.newXPath();
textValue = xpath.evaluate(xpathExpression, doc);
}
catch (final XPathExpressionException xpathException) {
xpathException.printStackTrace();
}
return textValue;
}
public static HashMap<String, String> readPropertyFile() {
System.out.println("In readPropertyFile method");
Properties prop = new Properties();
InputStream input;
HashMap<String, String> Propvals = new HashMap<String, String>();
try {
input = XMLToCSVMappings.class.getResourceAsStream("JustProperties.properties");
System.out.println("before load");
prop.load(input);
System.out.println("Property File Loaded Succesfully");
Set<String> propertyNames = prop.stringPropertyNames();
for (String Property : propertyNames) {
Propvals.put(Property, prop.getProperty(Property));
}
System.out.println("HashMap generated::" + Propvals);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
return Propvals;
}
}
我看到了这个错误。我已将所需的jar文件添加到类路径中。
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/bcel/generic/InstructionConstants
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)