String str="交足應向接收該信件<Page>1</Page>者計其少交<B>之數</B>照收加<Nation>66</Nation>倍 其各項不能代寄之<I>物特爲詳列</I>於後 一除信函以外其各<Page>1</Page>項物件若未經交足<Nation>67</Nation>寄資或先付若干及其物件有未遵前章辦理而欲照章受其<B>減少寄資</B>";

  //<text>交足應向接收該信件</text><Page>1</Page><text>者計其少交</text><B>之數</B><text>照收加</text><Nation>66</Nation>
  /**说明:把标签保留下来,和标签内容保留下来,放到List集合中
   * text 交足應向接收該信件
   * Page 1
   * text 者計其少交
   * B    之數
   * text 照收加
   * Nation 66
   */

解决方案 »

  1.   

    什么意思?
    是做成这样:
    <text>交足應向接收該信件</text><Page>1</Page><text>者計其少交</text><B>之數</B><text>照收加</text><Nation>66</Nation>
      

  2.   

    是把str字符串分解成一个list集合,然后,再生成XML格式的数据! /**说明:把标签保留下来,和标签内容保留下来,放到List集合中
           * text 交足應向接收該信件
           * Page 1
           * text 者計其少交
           * B    之數
           * text 照收加
           * Nation 66
           */
      

  3.   


    public static List<String> getInfo(String str){

    List<String> result = new ArrayList<String>();
    int firstIndex = str.indexOf("<");
    int secondIndex = str.indexOf(">", firstIndex);
    if(firstIndex < 0){

    if(str.length() > 0){
    result.add("text=" + str);
    }
    return result;
    }

    String key1 = str.substring(firstIndex + 1, secondIndex);
    int endIndex = str.indexOf("</" + key1 + ">", secondIndex);

    if(endIndex > 0){
    if(firstIndex > 0){
    result.add("text=" + str.substring(0, firstIndex));
    }

    result.add(key1 + "=" + str.substring(secondIndex + 1, endIndex));
    str = str.substring(endIndex + key1.length() + 3);
    }else{

    result.add("text=" + str.substring(0, firstIndex) + 1);
    }

    result.addAll(getInfo(str));
    return result;
    }这样就能生成List集合了,不过方法比较粗略,str混乱的话会有意外