此段字符:String str="
谢谢!
[Page]1[/Page]
谢谢!谢谢!
<Notation>65</Notation>
谢谢!谢谢!谢谢!
<Notation>66</Notation>
谢谢!谢谢!谢谢!谢谢!
<Notation>67</Notation>
谢谢!谢谢!谢谢!谢谢!谢谢!
<Notation>68</Notation>
谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!
<Notation>69</Notation>
谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!
[Page]1[/Page]
谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!
"<!--就是把上面的字符串处理成类似于此XML格式的,谢谢大家 -->
<Paragraph>
          <Text>谢谢!</Text>
          <Break style="Page">1</Break>
          <Text>谢谢!谢谢!</Text>
          <Linker Style="Notation" Destination="65" /> 
          <Text>谢谢!谢谢!谢谢</Text>
          <Linker Style="Notation" Destination="66" /> 
          <Text>谢谢!谢谢!谢谢!谢谢!</Text>
          <Linker Style="Notation" Destination="67" /> 
          <Text>谢谢!谢谢!谢谢!谢谢!谢谢!</Text>
          <Linker Style="Notation" Destination="68" /> 
          <Text>谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!</Text>
          <Linker Style="Notation" Destination="69" /> 
          <Text>谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!</Text>
          <Break style="Page">1</Break>
          <Text>谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!</Text>
</Paragraph>

解决方案 »

  1.   

    谢谢!关键是我不知道如何把上面的字符串如何处理????dom4j会用?请指教!
      

  2.   

    关键问题不是在输出XML,而是在你要梳理清楚究竟原始字符串结构有哪些特征可以被处理。假定你现在的数据都是一行一行的(如果是放在文件中,就直接Scaner.nextLine),那么:        String str= "谢谢!\n[Page]1[/Page]\n谢谢!谢谢!\n<Notation>65</Notation>\n 谢谢!谢谢!谢谢!\n<Notation>66</Notation>\n 谢谢!谢谢!谢谢!谢谢!\n <Notation>67</Notation>\n 谢谢!谢谢!谢谢!谢谢!谢谢!\n <Notation>68</Notation>\n 谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!\n <Notation>69</Notation>\n 谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!\n [Page]1[/Page]\n 谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!";
            String[] lines = str.split("[\n]");
            StringBuffer sb = new StringBuffer("<Paragraph>");
            for (String s : lines) {
                if (s.indexOf("[Page]")>=0){
                    s = s.replaceAll("\\[", "<").replaceAll("\\]", ">");
                    sb.append(s);
                } else if (s.indexOf("<Notation>")>=0) {
                    sb.append(s);
                } else {
                    sb.append("<Text>").append(s).append("</Text>");
                }
            }
            sb.append("</Paragraph>");
            System.out.println(sb);结果:<Paragraph><Text>谢谢!</Text><Page>1</Page><Text>谢谢!谢谢!</Text><Notation>65</Notation><Text> 谢谢!谢谢!谢谢!</Text><Notation>66</Notation><Text> 谢谢!谢谢!谢谢!谢谢!</Text> <Notation>67</Notation><Text> 谢谢!谢谢!谢谢!谢谢!谢谢!</Text> <Notation>68</Notation><Text> 谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!</Text> <Notation>69</Notation><Text> 谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!</Text> <Page>1</Page><Text> 谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!</Text></Paragraph>
      

  3.   

    asdasdasdsadasdasd