2017-06-18 3 views
0

J'ai une méthode qui obtient une chaîne XML et, en théorie, doit insérer un commentaire avant CHAQUE étiquette particulière. Je me demande comment faire fonctionnerComment trouver une chaîne à l'intérieur d'une chaîne particulière et insérer

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

Sortie de addCommentXML("somereallylongxml", "second", "it’s a comment") devrait être

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

Mais il ne semble pas le problème, car je ne sais pas comment itérer à travers la chaîne correctement à ajouter avant CHAQUE tagName, pas seulement en premier, donc on obtient une boucle infinie. Comment puis je faire ça?

+0

vous devriez le faire avec une expression régulière. –

+0

Mais ici, je dois ajouter quelque chose avant de ne pas remplacer –

Répondre

0

Le soliution proposé est ici très simple un: diviser l'entrée par le tagname, rejoignant ensuite les morceaux ensemble et insérer le commentaire et tagname entre les deux.

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: pas 3ème partie lib nécessaire
con: cette méthode n'a pas vraiment le parser XML, donc, parfois, il peut produire des résultats erronous (comme si l'étiquette se trouve à l'intérieur de commentaire ...)

1

Cela peut être facilement fait avec la librairie JSOUP. C'est un outil parfait pour travailler avec HTML/XML.

https://jsoup.org/

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

Dans votre cas il ressemblera à ceci:

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

Sortie:

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