SAXParser problem?

Status
Nicht offen für weitere Antworten.

pid82

Aktives Mitglied
Hallo an alle habe mich in dieser Woche mit dem Parsen von XMLDateien beschaeftigt.
Leider bin ich auf ein Problem zugestossen.


//Die Datei XMLObjectHandler
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.*;

public class XMLObjectHandler extends DefaultHandler {

private static int readingNothing = 0;
private static int readingLiteratur = 1;
private static int readingName = 2;
private static int readingFachbereich = 3;
private static int readingFach = 4;

private int currentActivity = readingNothing;

private XMLObject xmlObject = null;


public XMLObjectHandler() {
super();
xmlObject = new XMLObject();
}

public XMLObject getXMLObject() {
return xmlObject;
}

public void start(String uri, String localName, String qname, Attributes attributes) {

if(qname.equalsIgnoreCase("name")) {
currentActivity = readingName;
}else if(qname.equalsIgnoreCase("fachbereich")) {
currentActivity = readingFachbereich;
}else if(qname.equalsIgnoreCase("literatur")) {
currentActivity = readingLiteratur;
}else if(qname.equalsIgnoreCase("fach")) {
currentActivity = readingFach;
}
}

public void character(char[] ch, int start, int length) {
String value = new String(ch, start, length);

if(currentActivity == readingName) {
xmlObject.setName(value);
}else if(currentActivity == readingFachbereich) {
xmlObject.setFachbereich(value);
}else if(currentActivity == readingLiteratur) {
xmlObject.setLiteratur(value);
}else if(currentActivity == readingFach) {
xmlObject.setFach(value);
}
}


}

//Die Datei XMLParser

import javax.xml.parsers.*;
import org.xml.sax.*;
import java.io.File;

public class XMLParser {

private XMLObject xmlObject = null;


public XMLParser() {
xmlObject = new XMLObject();
}


/**
* @see SAXParser#parse
* @param fileName der Name der XMLDatei
* @exception IllegalArgumentException wenn das File Objekt null ist
* @exception IOException wenn ein I/O Fehler auftritt
* @exception SaxException wenn waehrend dem Arbeiten ein SAX Fehler auftritt
* @return es wird ein ausgefuelltes XMLObject zurueckgegeben
*
*/
public XMLObject parseXMLDocument(String fileName) {

File input = new File(fileName);
SAXParserFactory factory = SAXParserFactory.newInstance();
XMLObjectHandler xmlObjectHandler = new XMLObjectHandler();

try {
SAXParser sax = factory.newSAXParser();
sax.parse(input, xmlObjectHandler);
}catch(IllegalArgumentException iae) {
//throw new IllegalArgumentException();
}catch(SAXException se) {
//throw new SAXException();
}catch(Exception e) {

}

return (xmlObjectHandler.getXMLObject());
}



}

Wenn ich jetzt von meiner main methode die in einer anderen Datei ist ein Object der Klasse XMLParser erzeuge
und der Funktion parserXMLDocument einen Dateinamen übergebe verlange ich ein Objekt der Klasse XMLObject
das normalerweise mit dem Inhalt der XMLDatei ausgefüllt sein muss. Aber hier ist genau das Problem die Elemente meiners Objektes aus der Klasse XMLObject haben alle den Wert null?

Was habe ich falsch gemacht?
 

Bleiglanz

Gesperrter Benutzer
wie sieht denn dein xml aus?

BTW kannst du das so nicht machen, weil Textinhalt zerstückelt werden könnte [d.h. es könnten mehrere character Events kommen]

=> du musst den Text "sammeln" und erst in endElement speichern
 
R

Roar

Gast
sieht so eigentlich eine saubere sax implementierung aus? find ich ja nich so übersichtlich, gibts na sauberere möglichkeiten für sax?
 
Status
Nicht offen für weitere Antworten.
Ähnliche Java Themen
  Titel Forum Antworten Datum
8 SAXParser Problem, startElement wird nicht ausgeführt XML & JSON 2
J SAXParser Klasse XML & JSON 12
M Multithreaded SAXParser XML & JSON 8
L XMLDatei mit SAXParser prüfen XML & JSON 3
mdee SAXParser XML & JSON 2
R SaxParser und Sonderzeichen "&" XML & JSON 3
E Saxparser Exection bei Jasperreports XML & JSON 11
O XPATH Problem - Anfänger XML & JSON 4
so_ein_Komischer Problem mit Clean and Build XML & JSON 1
P JAXB-Problem XML & JSON 1
W Problem mit dem mit XML sortierung XML & JSON 2
S Jaxb Unmarshalling Problem XML & JSON 4
E einfaches Problem XML + XSD + jedit XML & JSON 2
S Xslt Problem XML & JSON 2
B JasperReport Problem in Runtime XML & JSON 2
S Problem with JAXB unmarshalling classes that have the same name in @XmlRootElement XML & JSON 2
R Problem bei: XML und XSL zu HTML XML & JSON 2
M Read / write Problem beim ByteStrom XML & JSON 2
M XML write Problem zweiter Ansatz XML & JSON 3
M XML read Problem XML & JSON 4
M XML write Problem XML & JSON 2
whitenexx Problem beim parsen von Facebook XML XML & JSON 3
M XML Unicode Problem XML & JSON 2
S XJC --> Java-Objects compile Problem XML & JSON 4
F XPath-Problem mit DOM4J XML & JSON 8
B JPA + JAXB Mapping Problem XML & JSON 2
T XPath Problem: finden einer Node nach Attributswert XML & JSON 2
G Problem beim schreiben von XML in eine File XML & JSON 2
S Encoding Problem XML & JSON 7
K JAXB und Maps -> Marshalling-Problem XML & JSON 6
B Problem beim löschen von ChildNodes aus einem XML-DOM XML & JSON 3
E JDOM - Problem beim Zusammenfügen zweier Dateien XML & JSON 2
M JExcelAPI (JXL) Encoding Problem XML & JSON 11
S DOM Parsen Problem mit HTML Sonderzeichen XML & JSON 4
A aus xml --> html Problem XML & JSON 3
Y stax Problem XML & JSON 3
slawaweis Problem mit XSLT (wahrscheinlich ein Bug in Java 6) XML & JSON 16
T Problem beim Parsen von Attribut xmlns="urn:com:test&qu XML & JSON 6
P XPath Problem XML & JSON 2
J Problem beim XML-Lesen XML & JSON 2
M Problem mit FOP in Java Programm XML & JSON 2
S Problem mit XPath XML & JSON 4
J Problem mit compile einer XSD XML & JSON 3
N jdom problem beim lesen von child elementen XML & JSON 5
N problem bei xml lesen mit jdom XML & JSON 2
A XPath Problem XML & JSON 2
W JDOM element ändern funzt nich :( [problem gelöst] XML & JSON 3
G Problem mit XML-Schema Validierung mit Java XML & JSON 12
B jdom: getChildren() problem XML & JSON 4
H XSL-FO Problem mit If XML & JSON 2
loadbrain XPath Problem XML & JSON 2
T addContent / Problem mit Variable XML & JSON 2
F Problem mit JAXB Unmarshaller XML & JSON 2
F JDOM und XPath - Problem mit Namespace ohne Prefix XML & JSON 5
M Java und XSLT: Performanz-Problem XML & JSON 5
X JDOM SAXBuilder Validationschema - Problem XML & JSON 8
G Problem mit getContent XML & JSON 4
K stax problem XML & JSON 2
S Problem mit SAX XML & JSON 6
A Problem mit JasperReport XML & JSON 6
G DOCTYPE Problem beim Transformer/TransformerFactory etc. XML & JSON 13
C XSD Problem XML & JSON 16
R Problem bei Erstellung von XML(JDOM) XML & JSON 3
R Problem mit SAX-Parser characters() XML & JSON 7
M XPath Problem im Zusammenhang mit document() XML & JSON 2
P Problem beim erstellen eines neuen Elements (JDOM) XML & JSON 5
Z Problem mit getNodeValue() und setNodeValue() in DOM XML & JSON 6
H JAXB CUSTOMIZATION PROBLEM XML & JSON 2
M XPATH und RSS (Problem namespaces) XML & JSON 7
S Problem beim Erstellen eines pdfs XML & JSON 3
V Problem mit xsd XML & JSON 2
P XML mit hilfe von JDOM abspeichern macht Problem XML & JSON 6
G Problem mit addContent() XML & JSON 4
B DTD Problem - Reihenfolge der Einträge XML & JSON 2
R Problem beim Auslesen von Attributen XML & JSON 4
K Problem mit ant/java web services XML & JSON 4
K xml Datei mit JDOM erzeugen, Problem Namespaces XML & JSON 1
P Problem mit XML und DOM XML & JSON 2

Ähnliche Java Themen

Neue Themen


Oben