如何对 xml 中的 xml 元素进行排序,例如输入 xml 就像这样 Person A B Employee A B Person C D Employee C D
现在我想对 xml 进行排序以输出 Person A B Person C D Employee A B Employee C D
也就是说,所有以 Person 开头的节点都应该先出现,然后是 Employye 节点......
现在正在尝试这样,
BufferedReader docInput = new BufferedReader(new InputStreamReader(inputStream));
BufferedWriter docOutput = new BufferedWriter(new OutputStreamWriter(outputStream));
StringBuffer insb = new StringBuffer();
String line = "", segment="";
String beginSegment = "";
Vector a=new Vector();
Vector b=new Vector();
Properties prop = System.getProperties();
String lineSeparator = prop.getProperty("line.separator");
int start=0,pos=0;
try
{
while ((line = docInput.readLine()) != null)
{
line.replace(lineSeparator,"");
insb.append(line);
}
pos=insb.indexOf(":Recipient");
pos=pos-15;
pos=insb.indexOf("<",pos);
beginSegment=insb.substring(0,pos);
System.out.println(" Begin "+beginSegment);
insb=insb.replace(0,pos,"");
while(insb.indexOf("Recipient>")>0)
{
pos=insb.indexOf("Recipient>");
segment=insb.substring(0,pos+10);
a.add(segment);
segment="";
insb=insb.replace(0,pos+10,"");
}
for(int y=0;y<b.size();y++)
if(b.get(y).toString().contains("Employee>"))
{
a.add(b.get(y).toString());
b.remove(y);
}
for(int y=0;y<a.size();y++)
b.add(a.get(y).toString());
b.add(0,beginSegment);
b.add(insb.toString());
for(int y=0;y<b.size();y++)
docOutput.write(b.get(y).toString());
docOutput.close();
docInput.close();
}