2016-12-16 67 views
-5

我正在尋找在java中這需要一個XML字符串,和所有標籤(而不是他們的內容)轉換爲駱駝情況下,最簡單的方法,如正則表達式的XML標記鍵轉換成駱駝

<HeaderFirst> 
    <HeaderTwo> 
     <ID>id1</ID> 
     <TimeStamp>2016-11-04T02:46:34Z</TimeStamp> 
     <DetailedDescription> 
      <![CDATA[di]]> 
     </DetailedDescription> 
    </HeaderTwo> 
</HeaderFirst> 

會轉換爲

<headerFirst> 
    <headerTwo> 
     <id>id1</id> 
     <timeStamp>2016-11-04T02:46:34Z</timeStamp> 
     <detailedDescription> 
      <![CDATA[di]]> 
     </detailedDescription> 
    </headerTwo> 
</headerFirst> 
+0

查找模式。也許如果檢查是否substring [1]是「/」。 – kar

回答

-2

嘗試是這樣的:

public void tagToCamelCase(String input){ 
    char[] inputArray = input.toCharArray(); 
     for (int i = 0; i < inputArray.length-2; i++){ 
      if (inputArray[i] == '<'){ 
       if(inputArray[i+1]!= '/') 
        inputArray[i+1] = Character.toLowerCase(inputArray[i+1]); 
       else 
        inputArray[i+2] = Character.toLowerCase(inputArray[i+2]); 
      } 
     } 
     System.out.println(new String(inputArray)); 
} 

注:標籤ID會標識和未標識。希望這可以幫助。

-2

這是基於對「>」字符分割字符串,然後在三種不同的情況處理令牌的解決方案:CDATA,開放標籤,和結束標記

下面的代碼應該工作(見下面的程序輸出)。然而,標籤「ID」存在問題 - 我們如何知道它的駱駝案件應該是「id」而不是「iD」?這需要一本詞典來捕捉這些知識。所以下面的例程convert()有兩種模式 - useDictionary爲true或false。看看下面的解決方案是否滿足您的要求。

要使用「useDictionary」模式,您還需要維護一個合適的字典(程序中稱爲「dict」的散列表,現在只有一個字典中的條目「ID」應該是駱駝式的「 ID」)。 注意,字典可以憋足了增量 - 你只需要在特殊情況下添加到字典中(例如,「ID」的駝峯是「ID」不「ID」)

import java.util.HashMap; 
import java.util.Map; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

public class CamelCase { 
    private static Map<String, String> dict = new HashMap<>(); 

    static { 
     dict.put("ID", "id"); 
    } 

    public static void main(String[] args) { 
     String input = "<HeaderFirst> " 
       + "\n <HeaderTwo>" 
       + "\n  <ID>id1</ID>" 
       + "\n   <TimeStamp>2016-11-04T02:46:34Z</TimeStamp>" 
       + "\n   <DetailedDescription>" 
       + "\n   <![CDATA[di]]>" 
       + "\n   </DetailedDescription>" 
       + "\n </HeaderTwo> " 
       + "\n</HeaderFirst>"; 
     System.out.println("===== output without using a dictionary ====="); 
     System.out.println(convert(input, false /* useDictionary */)); 
     System.out.println("===== output using a dictionary ====="); 
     System.out.println(convert(input, true /* useDictionary */)); 
    } 

    private static String convert(String input, boolean useDictionary) { 
     String splitter = ">"; 
     String[] tokens = input.split(splitter); 
     StringBuilder sb = new StringBuilder(); 
     Pattern cdataPattern = Pattern.compile("([^<]*)<!\\[CDATA\\[([^\\]]*)\\]\\]"); 
     Pattern oTagPattern = Pattern.compile("([^<]*)<(\\w+)"); 
     Pattern cTagPattern = Pattern.compile("([^<]*)</(\\w+)"); 

     String prefix; 
     String tag; 
     String newTag; 
     for (String token : tokens) { 
      Matcher cdataMatcher = cdataPattern.matcher(token); 
      Matcher oTagMatcher = oTagPattern.matcher(token); 
      Matcher cTagMatcher = cTagPattern.matcher(token); 
      if (cdataMatcher.find()) {  // CDATA - do not change 
       sb.append(token); 
      } else if (oTagMatcher.find()) {// open tag - change first char to lower case 
       prefix = oTagMatcher.group(1); 
       tag  = oTagMatcher.group(2); 
       newTag = camelCaseOneTag(tag, useDictionary); 
       sb.append(prefix + "<" + newTag); 
      } else if (cTagMatcher.find()) {// close tag - change first char to lower case 
       prefix = cTagMatcher.group(1); 
       tag  = cTagMatcher.group(2); 
       newTag = camelCaseOneTag(tag, useDictionary); 
       sb.append(prefix + "<" + newTag); 
      } 
      sb.append(splitter); 
     } 
     return sb.toString(); 
    } 

    private static String camelCaseOneTag(String tag, boolean useDictionary) { 
     String newTag; 
     if (useDictionary 
       && dict.containsKey(tag)) { 
      newTag = dict.get(tag); 
     } else { 
      newTag = tag.substring(0, 1).toLowerCase() 
        + tag.substring(1); 
     } 
     return newTag;  
    } 
} 

該程序的輸出是這樣的:匹配角括號中的字符串,如「<[a-zA-Z]+>」,並從那裏SUBSTRING查找和替換取決於起始或結束標記的第二或第三的字符串

===== output without using a dictionary ===== 
<headerFirst> 
    <headerTwo> 
     <iD>id1<iD> 
     <timeStamp>2016-11-04T02:46:34Z<timeStamp> 
     <detailedDescription> 
      <![CDATA[di]]> 
     <detailedDescription> 
    <headerTwo> 
<headerFirst> 
===== output using a dictionary ===== 
<headerFirst> 
    <headerTwo> 
     <id>id1<id> 
     <timeStamp>2016-11-04T02:46:34Z<timeStamp> 
     <detailedDescription> 
      <![CDATA[di]]> 
     <detailedDescription> 
    <headerTwo> 
<headerFirst> 
+0

爲什麼要投票? – leeyuiwah