我想将XML文件拆分为单独的XML。通过读取和标签。我已经使用 StringUtils.substringsBetween 拆分了 XML。但是,我只能从文件中拆分两个XML。第三个 XML 将附加第二个 XML。
程序:
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
public class SBuff {
private BufferedReader br;
public SBuff() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) throws IOException {
String data = "<?xml version="1.0"?>rn" +
"<company>rn" +
" <staff>rn" +
" <firstname>yong</firstname>rn" +
" <lastname>mook kim</lastname>rn" +
" <nickname>mkyong</nickname>rn" +
" <salary>100000</salary>rn" +
" </staff>rn" +
" <staff>rn" +
" <firstname>low</firstname>rn" +
" <lastname>yin fong</lastname>rn" +
" <nickname>fong fong</nickname>rn" +
" <salary>200000</salary>rn" +
" </staff>rn" +
" <staff>rn" +
" <firstname>low</firstname>rn" +
" <lastname>yin fong</lastname>rn" +
" <nickname>fong fong</nickname>rn" +
" <salary>200000</salary>rn" +
" </staff>rn" +
"</company>rn"+
"</xml>";
SBuff s = new SBuff();
s.loadData(data);
}
public void loadData(String stream) throws IOException {
String[] list = StringUtils.substringsBetween(stream,
"<staff">", "</staff>");
StringBuilder stringBuilder = new StringBuilder();
for (String s : list) {
stringBuilder.append("<staff>");
stringBuilder.append(s);
stringBuilder.append("</staff>");
System.out.println("##################################");
System.out.println(stringBuilder.toString());
System.out.println("##################################");
}
}
public void display(String data) {
System.out.println("Buffer Data: " + data);
}
}
输出:
Expected:
##################################
<staff>
<firstname>yong</firstname>
<lastname>mook kim</lastname>
<nickname>mkyong</nickname>
<salary>100000</salary>
</staff>
##################################
<staff>
<firstname>low</firstname>
<lastname>yin fong</lastname>
<nickname>fong fong</nickname>
<salary>200000</salary>
</staff>
##################################
<staff>
<firstname>low</firstname>
<lastname>yin fong</lastname>
<nickname>fong fong</nickname>
<salary>200000</salary>
</staff>
##################################
Actual:
##################################
<staff>
<firstname>yong</firstname>
<lastname>mook kim</lastname>
<nickname>mkyong</nickname>
<salary>100000</salary>
</staff>
##################################
<staff>
<firstname>low</firstname>
<lastname>yin fong</lastname>
<nickname>fong fong</nickname>
<salary>200000</salary>
</staff><staff>
<firstname>low</firstname>
<lastname>yin fong</lastname>
<nickname>fong fong</nickname>
<salary>200000</salary>
</staff>
##################################
我尝试了不同的方法。似乎没有一个锻炼。提前谢谢。
您可以使用像jsoup这样的XML解析器。
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.12.1</version>
</dependency>
解析和拆分:
Document doc = Jsoup.parse(data, "", Parser.xmlParser());
for(Element staffElement: doc.getElementsByTag("staff")) {
System.out.println(staffElement);
System.out.println("##################################");
}
对于实际输出 - 您的 loadData 函数将是
public void loadData(String stream) throws IOException {
String[] list = StringUtils.substringsBetween(stream, "<staff>", "</staff>");
StringBuilder stringBuilder = null;
System.out.println("##################################");
for (String s : list) {
stringBuilder=new StringBuilder();
stringBuilder.append("<staff>");
stringBuilder.append(s);
stringBuilder.append("</staff>");
// System.out.println("##################################");
System.out.println(stringBuilder.toString());
System.out.println("##################################");
}
}