且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将文本文件转换为XML

更新时间:2023-02-06 16:22:49

使用正则表达式(未经测试:(。 +?)(。+?);(。+?)<>(。+?)<>(。+))或拆分多次:
Use a regular expression (untested: "(.+?) (.+?);(.+?)<>(.+?)<>(.+)") or split multiple times:
  1. 按空格分割以获取ID和剩余部分

  2. 将剩余部分拆分为<>

  3. 从上面拆分第一部分;得到两个名字

  4. 另外两个部分是纸张和任务领域





当不使用正则表达式方法时,第一步的空格分割应该通过在输入字符串中找到第一个空格字符而不是使用 String.Split()。否则,当输入字符串中有更多空格时,剩余部分可能包含多个子字符串。

[/ EDIT]



When not using the regex method the splitting by space from the first step should be done "manually" by locating the first space character in the input string rather than using String.Split(). Otherwise the "remaining part" may consist of multiple sub strings when there are more spaces in the input string.
[/EDIT]


基于此:如何在Java中拆分字符串:字符串使用Regex解决方案拆分多个字符Ravindra babu [ ^ ]我会尝试这样的事情:



Based on this: How to split a string in Java: String Split with multiple characters using Regex solution by Ravindra babu[^] i'd try something like this:

String line = "10_1 a;b<>cd<>ef";
//String delimiters = "[ \\;\\<>]";
String delimiters = "[\\s\\;\\<>]";

String[] result = line.split(delimiters);
String xmlContent = "<Root><ID>" + result[0] + "</ID>";
xmlContent += "<Name><Name1>" + result[1] + "</Name1>";
xmlContent += "<Name2>" + result[2] + "</Name2></Name>";
xmlContent += "<Def>" + result[3] + "</Def>";
xmlContent += "<Ven>" + result[4] + "</Ven></Root>";
System.out.println(xmlContent);





注意:未经测试,但也应该有效!



Note: not tested, but it should work as well!