Hallo, ich möchte gern die Reihenfolge der Einträge aus der xml Datei einhalten. Momentan passiert das nicht. Wer kann mir helfen?
Die Ausgabe momentan sieht so aus:
Java:
<?xml version="1.0" encoding="UTF-8"?>
<testResults version="1.2">
<httpSample ts="1276602591589" lb="Funktion_1" rc="500" rm="Internal Server Error" sc="1" ec="1">
<responseData class="java.lang.String"><?xml version='1.0' encoding='UTF-8'?><Wichtig1</responseData>
</httpSample>
<sample ts="1276602591670" lb="Ausgabe" rc="200" rm="OK" sc="1" ec="0">
<responseData class="java.lang.String">Funktion_1 - Ergebnis: ID: null!!! 1</responseData>
</sample>
<httpSample ts="1276602592674" lb="Funktion_2" rc="500" rm="Internal Server Error" sc="1" ec="1">
<responseData class="java.lang.String"><?xml version='1.0' encoding='UTF-8'?><Wichtig2</responseData>
</httpSample>
<sample ts="1276602592754" lb="Ausgabe" rc="200" rm="OK" sc="1" ec="0">
<responseData class="java.lang.String">Funktion_2 - Ergebnis: ID: null!!! 2</responseData>
</sample>
<httpSample ts="1276602593869" lb="Funktion_4" rc="500" rm="Internal Server Error" sc="1" ec="1">
<responseData class="java.lang.String"><?xml version='1.0' encoding='UTF-8'?><Wichtig3</responseData>
</httpSample>
<sample ts="1276602593943" lb="Ausgabe" rc="200" rm="OK" sc="1" ec="0">
<responseData class="java.lang.String">Funktion_3 - Ergebnis: ID: null!!! 3</responseData>
</sample>
<httpSample ts="1276602595036" lb="Funktion_5" rc="500" rm="Internal Server Error" sc="1" ec="1">
<responseData class="java.lang.String"><?xml version='1.0' encoding='UTF-8'?><Wichtig4</responseData>
</httpSample>
<sample ts="1276602595121" lb="Ausgabe" rc="200" rm="OK" sc="1" ec="0">
<responseData class="java.lang.String">Funktion_4 - Ergebnis: ID: null!!! 4</responseData>
</sample>
<sample ts="1276602595121" lb="Ausgabe" rc="200" rm="OK" sc="1" ec="0">
<responseData class="java.lang.String">Funktion_5 - Ergebnis: ID: null!!! 5</responseData>
</sample>
<sample ts="1276602595121" lb="Ausgabe" rc="200" rm="OK" sc="1" ec="0">
<responseData class="java.lang.String">Funktion_6 - Ergebnis: ID: null!!! 6</responseData>
</sample>
</testResults>
Java:
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class Zeitstempel_neu {
static String fileString = "/tmp/test.xml";
public static void nodeElements(NodeList nodeLst){
for (int s = 0; s < nodeLst.getLength(); s++) {
Node fstNode = nodeLst.item(s);
if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
Element fstElmnt = (Element) fstNode;
NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("responseData");
Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
NodeList fstNm = fstNmElmnt.getChildNodes();
String zeitstempel = fstNode.getAttributes().item(5).getNodeValue();
String funktion = fstNode.getAttributes().item(1).getNodeValue();
String fehlerCode = fstNode.getAttributes().item(3).getNodeValue();
String fehlerNachricht = fstNode.getAttributes().item(2).getNodeValue();
String proben = fstNode.getAttributes().item(4).getNodeValue();
String fehler = fstNode.getAttributes().item(0).getNodeValue();
//System.out.println("TagName:" + fstNode.getNodeName());
if((fstNm.item(0)) == null){
System.out.println(s+" "+zeitstempel + ","+ funktion +","+ fehlerCode +","+ fehlerNachricht + ","+ proben + "," + fehler + ","+ "null");
}else{
System.out.println(s+" "+zeitstempel + ","+ funktion +","+ fehlerCode +","+ fehlerNachricht + ","+ proben + "," + fehler + ","+ ((Node) fstNm.item(0)).getNodeValue());
}
}
}
}
public static void parseXml(){
Document doc = null;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
File file = new File(fileString);
doc = db.parse(file);
doc.getDocumentElement().normalize();
//System.out.println("Root element " + doc.getDocumentElement().getNodeName());
NodeList nodeLst = doc.getElementsByTagName("httpSample");
//System.out.println("Information of all samples1");
nodeElements(nodeLst);
NodeList nodeLst1 = doc.getElementsByTagName("sample");
//System.out.println("Information of all samples2");
nodeElements(nodeLst1);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
parseXml();
}
}
Die Ausgabe momentan sieht so aus:
Java:
0 1276602591589,Funktion_1,Internal Server Error,500,1,1,<?xml version='1.0' encoding='UTF-8'?><Wichtig1
1 1276602592674,Funktion_2,Internal Server Error,500,1,1,<?xml version='1.0' encoding='UTF-8'?><Wichtig2
2 1276602593869,Funktion_4,Internal Server Error,500,1,1,<?xml version='1.0' encoding='UTF-8'?><Wichtig3
3 1276602595036,Funktion_5,Internal Server Error,500,1,1,<?xml version='1.0' encoding='UTF-8'?><Wichtig4
0 1276602591670,Ausgabe,OK,200,1,0,Funktion_1 - Ergebnis: ID: null!!! 1
1 1276602592754,Ausgabe,OK,200,1,0,Funktion_2 - Ergebnis: ID: null!!! 2
2 1276602593943,Ausgabe,OK,200,1,0,Funktion_3 - Ergebnis: ID: null!!! 3
3 1276602595121,Ausgabe,OK,200,1,0,Funktion_4 - Ergebnis: ID: null!!! 4
4 1276602595121,Ausgabe,OK,200,1,0,Funktion_5 - Ergebnis: ID: null!!! 5
5 1276602595121,Ausgabe,OK,200,1,0,Funktion_6 - Ergebnis: ID: null!!! 6
Zuletzt bearbeitet: