如何在特定字符串中查找字符串并插入
How to find string inside particular string and insert
我有一个获取 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 之前添加,而不仅仅是首先,所以我们得到无限循环。我该怎么做?
使用 JSOUP 库可以轻松完成。它是与 HTML/XML.
一起工作的完美工具
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 < and >
</second>
<!--it's a comment-->
<second />
</first>
</body>
</html>
此处建议的解决方案非常简单:按标记名拆分输入,然后将各部分连接在一起并在其间插入注释和标记名。
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;
}
专业版:不需要第 3 方库
缺点:这个方法并没有真正解析 xml,因此,有时它会产生错误的结果(比如如果在评论中找到标签......)
我有一个获取 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 之前添加,而不仅仅是首先,所以我们得到无限循环。我该怎么做?
使用 JSOUP 库可以轻松完成。它是与 HTML/XML.
一起工作的完美工具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 < and >
</second>
<!--it's a comment-->
<second />
</first>
</body>
</html>
此处建议的解决方案非常简单:按标记名拆分输入,然后将各部分连接在一起并在其间插入注释和标记名。
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;
}
专业版:不需要第 3 方库
缺点:这个方法并没有真正解析 xml,因此,有时它会产生错误的结果(比如如果在评论中找到标签......)