Hallo Forummitglieder,
ich habe zu meinem Problem folgenden Beitrag gefunden: http://www.java-forum.org/xml-co/35603-stax-problem.html , jedoch liegt bei mir das Problem scheinbar woanders, da es bei einem Element funktionert und bei dem anderen nicht.
Ich bin gerade dabei eine Software zu schreiben, bei der man aus einer XML eine GUI bauen kann.
Es funktioniert auch alles soweit, nur habe ich ein Problem, beim befüllen eines Textfeldes.
Ich benutze zu befüllen des Textfeldes parse.getElementText() innerhalb des XMLStreamConstants.START_ELEMENT. am besten Zeige ich euch mal einen Ausschnitt aus dem Code:
Bei einem attribut Textfeld geht es nicht, aber in yesno geht es komischerweise.
Beispiel XML:
[XML]
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<protokoll_style version="1" datum="31.12.01">
<seite name="1. Gesamteindruck">
<attribut name="1.1 Grundstimmung" typ="textfeld">Text</attribut>
<yesno name="Fröhlich"></yesno>
</seite>
</protokoll_style>
[/XML]
Als exception bekomme ich, aber nur wenn ich es bei dem attribut Textfeld mache:
Exception in thread "main" java.lang.IllegalStateException: Current state is not among the states START_ELEMENT , ATTRIBUTEvalid for getAttributeCount()
at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.getAttributeCount
Warum ???:L
Viele Grüße Kay
ich habe zu meinem Problem folgenden Beitrag gefunden: http://www.java-forum.org/xml-co/35603-stax-problem.html , jedoch liegt bei mir das Problem scheinbar woanders, da es bei einem Element funktionert und bei dem anderen nicht.
Ich bin gerade dabei eine Software zu schreiben, bei der man aus einer XML eine GUI bauen kann.
Es funktioniert auch alles soweit, nur habe ich ein Problem, beim befüllen eines Textfeldes.
Ich benutze zu befüllen des Textfeldes parse.getElementText() innerhalb des XMLStreamConstants.START_ELEMENT. am besten Zeige ich euch mal einen Ausschnitt aus dem Code:
Code:
while ( parser.hasNext() )
{
//System.out.println( "Event: " + parser.getEventType() );
switch ( parser.getEventType() )
{
case XMLStreamConstants.START_DOCUMENT:
System.out.println( "START_DOKUMENT: " + parser.getVersion() );
break;
case XMLStreamConstants.END_DOCUMENT:
System.out.println( "ENDE_DOKUMENT: " );
parser.close();
break;
case XMLStreamConstants.START_ELEMENT:
spacer.append( " " );
String elementName = parser.getLocalName();
System.out.println( spacer + "START_ELEMENT: " + elementName );
if (elementName.equals("protokoll_style")){
for ( int i = 0; i < parser.getAttributeCount(); i++ ) {
String attributName = parser.getAttributeLocalName( i );
String attributWert = parser.getAttributeValue( i );
if (attributName.equals("version")){
System.out.println( spacer + " Version: "+attributWert);
//+ parser.getAttributeLocalName( i )
//+ " Wert: " + parser.getAttributeValue( i ) );
}
if (attributName.equals("datum")){
System.out.println( spacer + " Version vom: "+attributWert);
}
}
}
else if (elementName.equals("attribut")){
String print ="";
for ( int i = 0; i < parser.getAttributeCount(); i++ ) {
String attributName = parser.getAttributeLocalName(i);
String attributWert = parser.getAttributeValue(i);
if (attributName.equals("typ")){
if (attributWert.equals("textfeld")){
print += "Textfeld mit Namen: ";
al_typ.add("textfeld");
//al_name.add(parser.getAttributeValue(i+1));
al_jPanel.add(null);
al_jTextArea.add(null);
al_jCheckBox.add(null);
al_jRadioButton.add(null);
al_jText.add(new JTextField());
//Text bestimmen falls vorhanden
if (parser.isStartElement()){
String text = parser.getElementText();
if (!text.equals("")){
al_jText.get(al_jText.size()-1).setText(text); //GEHT NICHT
}
}
}
else if (attributWert.equals("label")){
al_typ.add("label");
//al_name.add(parser.getAttributeValue(i+1));
al_jPanel.add(null);
al_jText.add(null);
al_jCheckBox.add(null);
al_jRadioButton.add(null);
al_jTextArea.add(null);
}
else if (attributName.equals("name")){
print += attributWert;
al_name.add(parser.getAttributeValue(i));
}
}
System.out.println( spacer + print);
}
else if (elementName.equals("yesno")){
/*Hier ist zu beachten, dass immer gleich zwei felder gefüllt
* werden müssen, da auf einmal 2 RadioButtons hinzukommen.
*/
if (parser.getAttributeLocalName( 0 ).equals("name")){
String frage = parser.getAttributeValue( 0 );
System.out.println( spacer + " yesno");
al_typ.add("yesno");
al_typ.add(null); //Muss auch abgefangen werden
al_name.add(frage);
al_name.add(null);
al_jPanel.add(null);
al_jPanel.add(null);
//al_jText.add(new JTextField(parser.getAttributeValue(1)));
al_jRadioButton.add(new JRadioButton( "Ja" )); //Vorletztes
al_jRadioButton.add(new JRadioButton( "Nein" )); //Letztes
al_jText.add(new JTextField());
//Textfeld befüllen falls vorhanden
String textYN = parser.getElementText();
String[] inhalt;
if (!textYN.equals("")){
inhalt = textYN.split(";;"); //Stream splitten
if (inhalt.length > 0){
if(inhalt.length > 1){//Abfangen wenn kein Text
textYN = inhalt[1];
}else{textYN="";}
if (inhalt[0].equals("yes")){
al_jRadioButton.get(al_jRadioButton.size()-2).setSelected(true); //Vorletztes Element
}
else if(inhalt[0].equals("no")){
al_jRadioButton.get(al_jRadioButton.size()-1).setSelected(true);
}
else{//Abfangen wenn nur Text
if (inhalt.length == 1){
textYN = inhalt[0];
}
}
//al_jText.get(al_jText.size()-1).setText(inhalt[1]); //GEHT
al_jText.get(al_jText.size()-1).setText(textYN);
}
}
}
al_jText.add(null);
al_jTextArea.add(null);
al_jTextArea.add(null);
al_jCheckBox.add(null);
al_jCheckBox.add(null);
}
else{
for ( int i = 0; i < parser.getAttributeCount(); i++ )
System.out.println( spacer + " Attribut: "
+ parser.getAttributeLocalName( i )
+ " Wert: " + parser.getAttributeValue( i ) );
}
break;
case XMLStreamConstants.END_ELEMENT:
System.out.println( spacer + "END_ELEMENT: " + parser.getLocalName() );
spacer.delete( (spacer.length() - 2), spacer.length() );
break;
default:
break;
}
parser.next();
}
Bei einem attribut Textfeld geht es nicht, aber in yesno geht es komischerweise.
Beispiel XML:
[XML]
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<protokoll_style version="1" datum="31.12.01">
<seite name="1. Gesamteindruck">
<attribut name="1.1 Grundstimmung" typ="textfeld">Text</attribut>
<yesno name="Fröhlich"></yesno>
</seite>
</protokoll_style>
[/XML]
Als exception bekomme ich, aber nur wenn ich es bei dem attribut Textfeld mache:
Exception in thread "main" java.lang.IllegalStateException: Current state is not among the states START_ELEMENT , ATTRIBUTEvalid for getAttributeCount()
at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.getAttributeCount
Warum ???:L
Viele Grüße Kay