简体   繁体   中英

How to find string inside particular string and insert

I have a method that gets XML string and in theory should insert a comment before EVERY particular tag. I wonder how to make it work

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();                  
}

Output of addCommentXML("somereallylongxml", "second", "it's a comment") should be

<?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>

But it apparently doesn't work, since i don't know how to iterate through the string correctly to add before EVERY tagName, not only first, so we get infinite loop. How can i do that?

It can be easily done with JSOUP library. It's a perfect tool to work with HTML/XML.

https://jsoup.org/

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

In your case it will look like this:

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>";
}

Output :

 <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> 

The proposed soliution here is a very straightforward one: splitting the input by the tagname, then joining the pieces together and inserting the comment and tagname in between.

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;
}

pro: no 3rd party lib required
con: this method does not really parse the xml, therefore, sometimes it can produce erronous results (like if the tag is found inside comment...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM