我想将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\"?>\r\n" +
"<company>\r\n" +
" <staff>\r\n" +
" <firstname>yong</firstname>\r\n" +
" <lastname>mook kim</lastname>\r\n" +
" <nickname>mkyong</nickname>\r\n" +
" <salary>100000</salary>\r\n" +
" </staff>\r\n" +
" <staff>\r\n" +
" <firstname>low</firstname>\r\n" +
" <lastname>yin fong</lastname>\r\n" +
" <nickname>fong fong</nickname>\r\n" +
" <salary>200000</salary>\r\n" +
" </staff>\r\n" +
" <staff>\r\n" +
" <firstname>low</firstname>\r\n" +
" <lastname>yin fong</lastname>\r\n" +
" <nickname>fong fong</nickname>\r\n" +
" <salary>200000</salary>\r\n" +
" </staff>\r\n" +
"</company>\r\n"+
"</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>
##################################
我尝试了不同的方法。似乎没有锻炼。 预先感谢。
答案 0 :(得分:1)
您可以使用jsoup之类的XML解析器。
<mapping wildcard="false" map-null="false">
<class-a>XXXX.XXXX.acord_pc_1_22.external.XXXX.NameInfoType</class-a>
<class-b>XXXX.iaa7_1.XXXX.ccss.XXXX.Stuff</class-b>
<class-c>XXXX.XXXX.acord_pc_1_22.external.XXXX.PhoneInfoType</class-c>
<field>
<a>personName.GivenName.value</a>
<b>quote.prospectName.firstName</b>
</field>
<field>
<a>personName.Surname.value</a>
<b>quote.prospectname.lastName</b>
</field>
<field custom-converter="XXXX.is.XXXX.converter.ListToListConverter" custom-converter-param="C40">
<a>personName.OtherGivenName</a>
<b>quote.prospectname.middleNames</b>
</field>
<field>
<c>phoneNumber.value</c>
<b>quote.prospectName.phone</b>
</field>
</mapping>
解析和拆分:
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.12.1</version>
</dependency>
答案 1 :(得分:0)
对于实际输出-您的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("##################################");
}
}