数値文字参照 ( など) を含む XML ドキュメントを解析するときに問題が発生します。私が直面している問題は、ドキュメントが解析されるときに & が & ; に置き換えられることです。(; の前にスペースを入れない) ため、解析されたドキュメントには & ;#xA0; が含まれます。これが起こらないようにするにはどうすればよいですか? を使用してみxmlDoc.setExpandEntityReferences(false)
ましたが、何も変わらないようです。
ドキュメントを解析するための私のコードは次のとおりです。
public static Document getXmlDoc(File xmlFile) throws ParserConfigurationException, SAXExeption, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setIgnoringElementContentWhitespace(true);
factory.setExpandEntityReferences(false);
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(xmlFile);
}
どんな助けでも大歓迎です。
編集:
上記のコードから解析された XML は変更され、ファイルに書き戻されます。これを行うコードは以下のとおりです。
public static File saveXmlDoc(Document xmlDocument, String outputToDir, String outputFilename) throws IOException {
String outputDir = outputToDir;
if (!outputDir.endWith(File.separator)) outputDir += File.separator;
if (!new FIle(outputDir).exists()) new File(outputDir).mkdir();
File xmlFile = new File(outputDir + outputFilename);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "no");
StreamResult saveResult = new StreamResult(outputDir + outputFilename);
DOMSource source = new DOMSource(xmlDocument);
transformer.transform(source, saveResult);
return xmlFile;
}
編集2:
の誤字を修正しましたfactory.setIgnoringElementContentWhitespace(true);
。
編集 3 - 私の解決策:
私の評判は低すぎて自分の質問に答えることができないため、これをすべて修正するために使用した解決策を次に示します。
この問題を解決するために変更した機能は次のとおりです。
XML ドキュメントを取得するには:
public static Document getXmlDoc(File xmlFile) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setIgnoringElementContentWhitespace(true);
factory.setExpandEntityReferences(false);
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(xmlFile);
}
XML ドキュメントを保存するには:
public static File saveXmlDoc(Document xmlDocument, String outputToDir, String outputFilename) throws Exception {
readNodesForHexConversion(xmlDocument.getChildNodes());
String xml = getXmlAsString(xmlDocument);
// write the xml out to a file
Exception writeError = null;
File xmlFile = null;
FileOutputStream fos = null;
try {
if (!new File(outputToDir).exists()) new File(outputToDir).mkdir();
xmlFile = new File(outputToDir + outputFilename);
if (!xmlFile.exists()) xmlFile.createNewFile();
fos = new FileOutputStream(xmlFile);
byte[] xmlBytes = xml.getBytes("UTF-8");
fos.write(xmlBytes);
fos.flush();
} catch (Exception ex) {
ex.printStackTrace();
writeError = ex;
} finally {
if (fos != null) fos.close();
if (writeError != null) throw writeError;
}
return xmlFile;
}
XML ドキュメントを文字列に変換するには:
public static String getXmlAsString(Document xmlDocument) throws TransformerFactoryConfigurationError, TransformerException {
DOMSource domSource = new DOMSource(xmlDocument);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
Transformer transformer;
transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(domSource, result);
return writer.toString();
}