我有一个 xml abc.xml
<soapenv:Envelope>
<soapenv:Header/>
<soapenv:Body>
<mes:SomeRq>
<RqID>?</RqID>
<MsgRqHdr>
....
</mes:SomeRq>
</soapenv:Body>
</soapenv:Envelope>
有没有办法让我从这个 xml 中搜索 mes:
并将其替换为 ins:
提前致谢。
public static void findreplcae(String strFilePath) throws IOException {
String currentString = "mes:";
String changedString = "ins:";
BufferedReader reader = new BufferedReader(new FileReader(strFilePath));
StringBuffer currentLine = new StringBuffer();
String currentLineIn;
while ((currentLineIn = reader.readLine()) != null) {
System.out.println(currentLineIn);
boolean bool = false;
String trimmedLine = currentLineIn.trim();
System.out.println(trimmedLine);
if (trimmedLine.contains(currentString)) {
trimmedLine.replace(currentString, changedString);
bool = true;
if (bool != true) {
currentLine = currentLine.append(currentLineIn + System.getProperty("line.separator"));
}
}
reader.close();
BufferedWriter writer = new BufferedWriter(new FileWriter(strFilePath));
writer.write(currentLine.toString());
writer.close();
}
}
最佳答案
将其解析为文本文件并不是一个好主意。 DocumentBuilder.parse要解析文件,请调用 getDocumentElement()
并检查 getPrefix
。如果匹配,则替换为 setPrefix()
。请注意,如果尚未完成,您必须注册前缀。
检查this page获取教程。
关于java - 如何从 xml java 中搜索并删除某些值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34893866/