Hallo Zusammen,
ich bin neu hier und auch neu im Bereich Java.
Ich habe ein kleines Problem beim einlesen einer XML Datei mit Java bzw. DOM.
Hier mal ein XML Beispiel, welches meinem ähnelt:
[XML]<cdr_data>
<date>2012-06-24</date>
<time>00:00:33</time>
<call_reference_id>012-03-001-060-353-025-391-613</call_reference_id>
<sip_account>4931119981013</sip_account>
<customer_id>12321</customer_id>
<customer_id_addon></customer_id_addon>
<a-number>4523960042</a-number>
<b-number>4953612210572</b-number>
<billing_seconds>31</billing_seconds>
<callduration>31</callduration>
<totalduration>31</totalduration>
<customer_price>0.0000</customer_price>
<reseller_price>0.0063</reseller_price>
<destination_id>1</destination_id>
<routingquality>1</routingquality>
<ipadress>62.220.170.21</ipadress>
<callstatus>ANSWER</callstatus>
<sip_useragent>AVM FRITZ!Box Fon WLAN 7270 v3</sip_useragent>
<prepaid_billed>0</prepaid_billed>
</cdr_data>
<cdr_data>
<date>2012-06-24</date>
<time>00:01:14</time>
<call_reference_id>012-001-001-060-111-025-292-014</call_reference_id>
<sip_account>4932229968552</sip_account>
<customer_id>18062</customer_id>
<customer_id_addon></customer_id_addon>
<a-number>4922232266</a-number>
<b-number>393355917093</b-number>
<billing_seconds>124</billing_seconds>
<callduration>124</callduration>
<totalduration>124</totalduration>
<customer_price>0.8122</customer_price>
<reseller_price>0.5677</reseller_price>
<destination_id>192</destination_id>
<routingquality>1</routingquality>
<ipadress>62.214.120.12</ipadress>
<callstatus>ANSWER</callstatus>
<sip_useragent>AVM FRITZ!Box Fon WLAN 7170 29</sip_useragent>
<prepaid_billed>0</prepaid_billed>
</cdr_data>[/XML]
Wie ihr sehen könnt, fehlt das root-tag ! Ich bekomme die Datei aber genau so und wollte nun Fragen wie ich das anfangs root-tag und end root tag hinzufügen kann. Bzw. ob ich die blöcke zwischen den <cdr_data> tags einzeln auslesen kann und an einen Parser geben kann.
Mein Code sieht momentan so aus:
Dieser funktioniert natürlich nur, wenn ich manuell in der .xml Datei die root-tags hinzufüge.
Hab bis jetzt bei google nichts passendes gefunden und hoffe ihr könnt mir helfen.
Danke
Max
ich bin neu hier und auch neu im Bereich Java.
Ich habe ein kleines Problem beim einlesen einer XML Datei mit Java bzw. DOM.
Hier mal ein XML Beispiel, welches meinem ähnelt:
[XML]<cdr_data>
<date>2012-06-24</date>
<time>00:00:33</time>
<call_reference_id>012-03-001-060-353-025-391-613</call_reference_id>
<sip_account>4931119981013</sip_account>
<customer_id>12321</customer_id>
<customer_id_addon></customer_id_addon>
<a-number>4523960042</a-number>
<b-number>4953612210572</b-number>
<billing_seconds>31</billing_seconds>
<callduration>31</callduration>
<totalduration>31</totalduration>
<customer_price>0.0000</customer_price>
<reseller_price>0.0063</reseller_price>
<destination_id>1</destination_id>
<routingquality>1</routingquality>
<ipadress>62.220.170.21</ipadress>
<callstatus>ANSWER</callstatus>
<sip_useragent>AVM FRITZ!Box Fon WLAN 7270 v3</sip_useragent>
<prepaid_billed>0</prepaid_billed>
</cdr_data>
<cdr_data>
<date>2012-06-24</date>
<time>00:01:14</time>
<call_reference_id>012-001-001-060-111-025-292-014</call_reference_id>
<sip_account>4932229968552</sip_account>
<customer_id>18062</customer_id>
<customer_id_addon></customer_id_addon>
<a-number>4922232266</a-number>
<b-number>393355917093</b-number>
<billing_seconds>124</billing_seconds>
<callduration>124</callduration>
<totalduration>124</totalduration>
<customer_price>0.8122</customer_price>
<reseller_price>0.5677</reseller_price>
<destination_id>192</destination_id>
<routingquality>1</routingquality>
<ipadress>62.214.120.12</ipadress>
<callstatus>ANSWER</callstatus>
<sip_useragent>AVM FRITZ!Box Fon WLAN 7170 29</sip_useragent>
<prepaid_billed>0</prepaid_billed>
</cdr_data>[/XML]
Wie ihr sehen könnt, fehlt das root-tag ! Ich bekomme die Datei aber genau so und wollte nun Fragen wie ich das anfangs root-tag und end root tag hinzufügen kann. Bzw. ob ich die blöcke zwischen den <cdr_data> tags einzeln auslesen kann und an einen Parser geben kann.
Mein Code sieht momentan so aus:
Java:
import java.io.*;
import org.xml.sax.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
public class XMLReader
{
public static void main( String[] argv )
{
try {
// ---- Parse XML file ----
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse( new File("/Users/max/Desktop/2012-06-24.xml") );
// ---- Get list of nodes to given element tag name ----
NodeList ndList = document.getElementsByTagName( "cdr_data");
printNodesFromList( ndList ); // printNodesFromList see below
// ---- Error handling ----
} catch( SAXParseException spe ) {
System.out.println( "\n** Parsing error, line " + spe.getLineNumber()
+ ", uri " + spe.getSystemId() );
System.out.println( " " + spe.getMessage() );
Exception e = ( spe.getException() != null ) ? spe.getException() : spe;
e.printStackTrace();
} catch( SAXException sxe ) {
Exception e = ( sxe.getException() != null ) ? sxe.getException() : sxe;
e.printStackTrace();
} catch( ParserConfigurationException pce ) {
pce.printStackTrace();
} catch( IOException ioe ) {
ioe.printStackTrace();
}
}
public static void printNodeInfos( String sNodeName, Node node )
{
if( node.hasChildNodes() ) {
NodeList nodeList = node.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++){
Node node2 = nodeList.item(i);
if (node2.getNodeName().equals("sip_account")) {
System.out.println("sip_account = " + node2.getTextContent());
}
if (node2.getNodeName().equals("a-number")) {
System.out.println("quellnummer = " + node2.getTextContent());
}
if (node2.getNodeName().equals("b-number")) {
System.out.println("zielnummer = " + node2.getTextContent());
}
if (node2.getNodeName().equals("date")) {
System.out.println("datum = " + node2.getTextContent());
}
if (node2.getNodeName().equals("time")) {
System.out.println("uhrzeit = " + node2.getTextContent());
}
if (node2.getNodeName().equals("billing_seconds")) {
System.out.println("dauer = " + node2.getTextContent());
}
if (node2.getNodeName().equals("destination_id")) {
System.out.println("destID = " + node2.getTextContent());
}
if (node2.getNodeName().equals("customer_price")) {
System.out.println("endkundenVK = " + node2.getTextContent());
}
if (node2.getNodeName().equals("reseller_price")) {
System.out.println("resellerEK = " + node2.getTextContent());
}
}
}
System.out.println( "----------------------\n" );
}
public static void printNodesFromList( NodeList ndList )
{
for( int i=0; i<ndList.getLength(); i++ )
printNodeInfos( "ndList.item("+i+")", ndList.item(i) );
}
}
Hab bis jetzt bei google nichts passendes gefunden und hoffe ihr könnt mir helfen.
Danke
Max