我正在阅读大约100个XML文件,而有些文件没有结束标记,所以我收到错误。我希望java忽略这些错误并继续下一个文件。 这是我的代码:
package javaapplication1;
import java.io.File;
import java.io.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import java.io.IOException;
import java.io.*;
import static java.lang.System.*;
import org.xml.sax.SAXParseException;
public class JavaApplication1 {
public static void main(String[] args) throws Exception {
try {
System.out.println("PLEASE ENTER FOLDER NAME ");
System.out.println(" ");
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
String s = bufferRead.readLine();
File dir = new File(s);
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
String absolutePath = dir + File.separator + children[i];
File dir1 = new File(absolutePath);
String filename2 = dir.getAbsolutePath() + File.separator + children[i];
System.out.println(filename2);
Document document1 = documentBuilder.parse(filename2);
NodeList employees1 = document1.getElementsByTagName("connection");
for (int q1 = 0; q1 < employees1.getLength(); q1++) {
Node employee = employees1.item(q1);
// Node nValue = (Node) nlList.item(0);
if (employee == null) {
break;
} else {
NamedNodeMap attribute = employee.getAttributes();
Node nodeAttr = attribute.getNamedItem("server");
nodeAttr.setTextContent("Ma.com:1531");
Node nodeAttr1 = attribute.getNamedItem("service");
Node nodeAttr2 = attribute.getNamedItem("port");
nodeAttr1.setTextContent("TS6");
nodeAttr2.setTextContent("");
Node nodeAttr3 = attribute.getNamedItem("username");
nodeAttr3.setTextContent("I_USER");
}
}
TransformerFactory transformerFactory1 = TransformerFactory.newInstance();
Transformer transformer1 = transformerFactory1.newTransformer();
DOMSource domSource1 = new DOMSource(document1);
StreamResult streamResult1 = new StreamResult(new File(filename2));
transformer1.transform(domSource1, streamResult1);
}
}
catch (Exception sae1)
{
sae1.printStackTrace();
}
//}
}
}