java - 如何从 xml java 中搜索并删除某些值

标签 java xml xml-parsing

我有一个 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/

相关文章:

javascript - 使用 XML 根据用户定义的选项填充 HTML 页面

python - xml.etree.ElementTree 获取节点深度

javascript - getElementsByTagName 'undefined' 在 Firefox 和 Chrome 中?

c# - 如何在 UML 中表示索引或参数化属性?

Java Arrays.equals() 为二维数组返回 false

java - HashMap和LinkedHashMap中插入数据后如何计算内存中的大小?

java - 在 Java 中,如何检查输入是否为数字?

xml - 如何在 XSL 中将一个表垂直拆分为两个表?

python - 向 XML 文件添加内嵌注释

java - 如何编码字符串列表