我的 java 应用程序加载 XML 文件,然后解析 XML。
我想要的是在创建 SAXBuilder 之前对文件进行搜索/替换。我如何在内存中执行此操作(无需写入文件)?
这是我的代码,以及我设想进行搜索/替换的位置:
private String xmlFile = "D:\\mycomputer\\extract.xml";
File myXMLFile = new File(xmlFile);
// TODO
// REPLACE ALL "<content>" in xmlFile with "<content><![CDATA["
// REPLACE ALL "</content>" with "]]></content>"
SAXBuilder builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser");
document = builder.build(new File(myXMLFile));
最佳答案
将文件读入内存,进行搜索/替换,然后使用 SAXBuilder(StringReader) 方法。
关于java - 如何即时搜索/替换文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8855948/