我想知道是否有办法拆分xml字符串。
我在这里有字符串:
<?xml version="1.0" encoding="UTF-8"?> <Messages> <Status>Iam good</Status> </Messages>
<?xml version="1.0" encoding="UTF-8"?> <Messages> <Status>Iam good</Status> </Messages>
<?xml version="1.0" encoding="UTF-8"?> <Messages> <Status>Iam NOT good</Status> </Messages>
<?xml version="1.0" encoding="UTF-8"?> <Messages> <Status>Iam good</Status> </Messages>
<?xml version="1.0" encoding="UTF-8"?> <Messages> <Status>Iam good</Status> </Messages>
<?xml version="1.0" encoding="UTF-8"?> <Messages> <Status>Iam NOT good</Status> </Messages>
<?xml version="1.0" encoding="UTF-8"?> <Messages> <Status>Iam good</Status> </Messages>
<?xml version="1.0" encoding="UTF-8"?> <Messages> <Status>Iam good</Status> </Messages>
<?xml version="1.0" encoding="UTF-8"?> <Messages> <Status>Iam NOT good</Status> </Messages>
<?xml version="1.0" encoding="UTF-8"?> <Messages> <Status>Iam good</Status> </Messages>
<?xml version="1.0" encoding="UTF-8"?> <Messages> <Status>Iam NOT good</Status> </Messages>
<?xml version="1.0" encoding="UTF-8"?> <Replies> <Start>Lets go</Start> </Replies>
所以我可以将每个字符串作为数组?就这个
_temp[2] = <?xml version="1.0" encoding="UTF-8"?> <Messages> <Status>Iam NOT good</Status> </Messages>
答案 0 :(得分:1)
您可以使用正则表达式进行拆分
(?<!\A)(?=<\?xml)
在每个<?xml
之前拆分。
为了防止输入开头的空格在分割中成为它们自己的一部分,你需要trim
输入。
Java示例:
String input = " <?xml version=\"1.0\" encoding=\"UTF-8\"?> <Messages> <Status>Iam good</Status> </Messages>\n"
+ " <?xml version=\"1.0\" encoding=\"UTF-8\"?> <Messages> <Status>Iam good</Status> </Messages>\n"
+ " <?xml version=\"1.0\" encoding=\"UTF-8\"?> <Messages> <Status>Iam NOT good</Status> </Messages>\n"
+ " <?xml version=\"1.0\" encoding=\"UTF-8\"?> <Messages> <Status>Iam good</Status> </Messages>\n"
+ " <?xml version=\"1.0\" encoding=\"UTF-8\"?> <Messages> <Status>Iam good</Status> </Messages>\n"
+ " <?xml version=\"1.0\" encoding=\"UTF-8\"?> <Messages> <Status>Iam NOT good</Status> </Messages>\n"
+ " <?xml version=\"1.0\" encoding=\"UTF-8\"?> <Messages> <Status>Iam good</Status> </Messages>\n"
+ " <?xml version=\"1.0\" encoding=\"UTF-8\"?> <Messages> <Status>Iam good</Status> </Messages>\n"
+ " <?xml version=\"1.0\" encoding=\"UTF-8\"?> <Messages> <Status>Iam NOT good</Status> </Messages>\n"
+ " <?xml version=\"1.0\" encoding=\"UTF-8\"?> <Messages> <Status>Iam good</Status> </Messages>\n"
+ " <?xml version=\"1.0\" encoding=\"UTF-8\"?> <Messages> <Status>Iam NOT good</Status> </Messages>\n"
+ " <?xml version=\"1.0\" encoding=\"UTF-8\"?> <Replies> <Start>Lets go</Start> </Replies>";
String[] parts = input.trim().split("(?<!\\A)(?=<\\?xml)");
for (String s : parts) {
System.out.println(s);
System.out.println("-------------------------");
}
注意:这不适用于所有可能的xml,因为<?xml
可以包含在CDATA
元素中,例如<![CDATA[<?xml]]>
。为了防止这种情况,您需要使用xml解析器。
答案 1 :(得分:0)
阅读本文的另一个选择如下
try {
List<String> lines = Files.readAllLines(Paths.get("<XML FILE PATH>"),
Charset.defaultCharset());
System.out.println(lines.size());
for (String line : lines) {
System.out.println("===============================");
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
在运行时,我得到了12行的列表,然后可以使用索引进行处理。