【问题标题】:How to find string inside particular string and insert如何在特定字符串中查找字符串并插入
【发布时间】:2017-06-18 10:24:49
【问题描述】:

我有一个获取 XML 字符串的方法,理论上应该在每个特定标记之前插入注释。我想知道如何使它工作

public static String addCommentXML(String xmlString, String tagName, String comment) 
{
   StringBuilder sb = new StringBuilder(xmlString);
   for(int i = 0; i < sb.toString().length(); i++)
    {
        if(sb.toString().toLowerCase().contains("<"+tagName+">"))
        {
        sb.insert(sb.toString().indexOf("<"+tagName+">", i) - 1, "<!--"+ comment+"-->"+"\n");
        }
    }
    return sb.toString();                  
}

addCommentXML("somereallylongxml", "second", "it’s a comment") 的输出 应该是

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<first>

<!--it's a comment-->

<second>some string</second>

<!--it's a comment-->

<second>some string</second>

<!--it's a comment-->

<second><![CDATA[need CDATA because of < and >]]></second>

<!--it's a comment-->

<second/>

</first>

但它显然不起作用,因为我不知道如何正确地遍历字符串以在每个 tagName 之前添加,不仅是第一个,所以我们得到无限循环。我该怎么做?

【问题讨论】:

  • 你应该用正则表达式来做到这一点。
  • 但是这里我需要先添加一些东西,而不是替换

标签: java xml string char stringbuilder


【解决方案1】:

使用 JSOUP 库可以轻松完成。它是处理 HTML/XML 的完美工具。

https://jsoup.org/

https://mvnrepository.com/artifact/org.jsoup/jsoup/1.10.3

在你的情况下它看起来像这样:

public static void main(String[] args) {
    String processedXml = addCommentXML(getDocument(), "second", "it's a comment");
    System.out.println(processedXml);
}

private static String addCommentXML(String xmlString, String tagName, String comment) {
    Document document = Jsoup.parse(xmlString);
    document.getElementsByTag(tagName).before("<!--" + comment + "-->");
    return document.toString();
}

private static String getDocument() {
    return "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" +
            "<first>\n" +
            "<second>some string</second>\n" +
            "<second>some string</second>\n" +
            "<second><![CDATA[need CDATA because of < and >]]></second>\n" +
            "<second/>\n" +
            "</first>";
}

输出

<html>
 <head></head>
 <body>
  <first> 
   <!--it's a comment-->
   <second>
    some string
   </second> 
   <!--it's a comment-->
   <second>
    some string
   </second> 
   <!--it's a comment-->
   <second>
    need CDATA because of &lt; and &gt;
   </second> 
   <!--it's a comment-->
   <second /> 
  </first>
 </body>
</html>

【讨论】:

    【解决方案2】:

    这里提出的解决方案是一个非常简单的解决方案:通过标记名拆分输入,然后将各个部分连接在一起并在其间插入注释和标记名。

    public static String addCommentXML(String xmlString, String tagName, String comment)
    {
        String[] parts = xmlString.split("\\Q<" + tagName + ">\\E");
        String output = parts[0];
        for (int i = 1 ; i < parts.length ; i++) {
            output += comment + "<" + tagName + ">" + parts[i];
        }
        return output;
    }
    

    专业人士:不需要 3rd 方库
    缺点:此方法并没有真正解析 xml,因此,有时它会产生错误的结果(例如,如果在评论中找到标签...)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-22
      • 2015-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多