Parsing xml attributes crash
Hi
Im quite n00bis about java programming. Looking through a lot of tutorials on how to pars xml.
Here is a cut of the xml I want to parse
Code:
<Component>
<Type Name="1">
<Item ItemNo="1" Name="First" Value="1" Config="1"></Item>
</Type>
<Type Name="2">
<Item ItemNo="2" Name="1" Value="3" Config="2"></Item>
<Item ItemNo="3" Name="3" Value="" Config="3"></Item>
</Type>
</Component>
I have found a great tutorial here: simple sax xml parser
But when the xml comes to the last "node end" ( </Component>) I get this error:
Code:
at org.eclipse.swt.SWT.error(SWT.java:4263)
at org.eclipse.swt.SWT.error(SWT.java:4197)
at org.eclipse.swt.SWT.error(SWT.java:4168)
at org.eclipse.swt.widgets.Widget.error(Widget.java:468)
at org.eclipse.swt.widgets.TableItem.setText(TableItem.java:1162)
at view.Main.openXML(Main.java:214)
at view.Main$2.widgetSelected(Main.java:96)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:240)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4165)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3754)
at view.Main.open(Main.java:61)
at view.Main.main(Main.java:46)
and my application closes.
Can anyone help me??
Re: Parsing xml attributes crash
What you have on Main.java:46? Can you show us the code?
Re: Parsing xml attributes crash
I deleted you other identical thread; both threads were moderated and I approved this one so it's visible to everyone now.
kind regards,
Jos
Re: Parsing xml attributes crash
According to the stack trace I don't think the error has something to do with parsing the XML text; a SWT widfget complains; I think it's an error in your gui.
kind regards,
Jos
Re: Parsing xml attributes crash
Well the thing is that I tried to put a string array (that was empty) into a table.
The next thing for me is to figure out how to turn the console output into a string, pref a string array.
From the tutorial I have this code bit:
Code:
private static final class SaxHandler extends DefaultHandler {
// invoked when document-parsing is started:
StringBuffer accumulator = new StringBuffer(); // Accumulate parsed text
public void startDocument() throws SAXException {
System.out.println("Document processing started");
}
// notifies about finish of parsing:
public void endDocument() throws SAXException {
System.out.println("Document processing finished");
}
public void startElement(String uri, String localName,
String qName, Attributes attrs) throws SAXException {
accumulator.setLength(0);
if (qName.equals("Component")) {
} else if (qName.equals("Type")) {
String TypeName = attrs.getValue("Name");
System.out.println("Type "+ TypeName);
} else if (qName.equals("Item")) {
// ItemNo value as String:
String ItemNo = attrs.getValue("ItemNo");
// name as a String:
String Name = attrs.getValue("Name");
// value as a String:
String Value = attrs.getValue("Value");
// config value as a String:
String Config = attrs.getValue("Config");
String XMLtable = ItemNo + Name + Value + Config;
System.out.println("Item " + ItemNo +" " + Name +" " + Value +" "+ Config);
} else {
throw new IllegalArgumentException("Element '" +
qName + "' is not allowed here");
}
}
// we leave element 'qName' without any actions:
public void endElement(String uri, String localName, String qName)
throws SAXException {
// do nothing;
}
}
I cannot get access to the XMLtable string array.
EDIT:
I would also like to have all TypeName strings in a array (or similar) so i can have them i a Combo
Any ide?
Thanks for answering so fast anyways
Re: Parsing xml attributes crash
What is the actual exception.
I can only see the stack trace.
Re: Parsing xml attributes crash
I forgot to add it in I'm sorry here it is..
Code:
java.lang.IllegalArgumentException: Argument cannot be null
these lines caused the exception:
Code:
TableItem item1 = new TableItem(table, SWT.NONE);
item1.setText(XMLItem);
the String array XMLItem is empty..
But as I asked. Is there a way of getting the converted to a string or string array?
Re: Parsing xml attributes crash
You are using qualified names instead of local names, so I take it that you do not have any default namespace declarations in your XML document?
EDIT: Actually, qname and localname should be the same in your case...
You haven't shown us the declaration for XMLItem so how can we help?
1 Attachment(s)
Parsed xml strings to string array
The XMLItem is decleared in the top of the "main" class this way
Code:
Public String [] XMLItem;
The thing is that I want the strings created in the parsed section to become a string array that i can put in my Table as in the picture.
I get all the stuff from the XML into the console view of eclipse. But cant quite figure out how to put it into a string array so that I can put it in the table.
I dont have the code in front of me right now but I know this is how ;)
I dont use it anywhere else than right here..
Maybe this should go into another thread??