我正在处理以下xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE CASE SYSTEM "C:\Users\user123\Desktop\demo.dtd">
<INFO>
<NAME>John Smith</NAME>
<BUSINESSNAME>Smith</BUSINESSNAME>
</INFO>
但我不得不忽略
<!DOCTYPE CASE SYSTEM "C:\Users\user123\Desktop\demo.dtd"> because in presence of this parsing is giving error.
这是我的代码:
StringBuilder sb = new StringBuilder();
File xmlFile = new File("C:/Users/Desktop/demo.xml");
BufferedReader br = new BufferedReader(new FileReader(xmlFile));
String line = null;
while((line = br.readLine())!= null){
if(line.indexOf("&") != -1)
{
line = line.replaceAll("&","&");
}
sb.append(line);
}
br.close();
BufferedWriter bw = new BufferedWriter(new FileWriter(xmlFile));
Source xmlInput = new StreamSource(new StringReader(sb.toString()));
StringWriter stringWriter = new StringWriter();
StreamResult xmlOutput = new StreamResult(stringWriter);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformerFactory.setAttribute("indent-number", 2);
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(xmlInput, xmlOutput);
bw.write(xmlOutput.getWriter().toString());
bw.close();
System.out.println("success");
有什么建议么?