我正在尝试解析 xml,以便我可以替换拉丁字符。xml 中的拉丁字符被替换,但 xml 中的 \n 和 \t 被删除。我正在使用 dtd 替换拉丁字符。下面是我用来解析 xml 的代码
DocumentBuilderFactory docFactory = DocumentBuilderFactory
.newInstance();
docFactory.setValidating(false);
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
docBuilder.setEntityResolver(resolver);
String str = "";
try {
Document doc = docBuilder.parse(is, contextPath);
OutputFormat format = new OutputFormat(doc); // Serialize DOM
format.setIndenting(reqIndent);
format.setOmitDocumentType(true);
format.setEncoding(encoding);
StringWriter stringOut = new StringWriter(); // Writer will be a
XMLSerializer serial = new XMLSerializer(stringOut, format);
serial.asDOMSerializer(); // As a DOM Serializer
serial.serialize(doc);
str = stringOut.toString();
} catch (org.xml.sax.SAXParseException saxExp) {
throw saxExp; // asString(is);
} catch (Exception e) {
throw e;
} finally {
if (is != null)
is.close();
}
return str;