Results 21 to 29 of 29
- 01-27-2012, 08:29 PM #21
Member
- Join Date
- Dec 2011
- Posts
- 57
- Rep Power
- 0
Re: Split string and joining substrings
I use a DOM parser to get the attributes and they are always present. The xml file is used by other SW to write ID into stuff. Some of the attributes are empty ( like this Value = " ").
This is the code to get the Item attributes
There is a setter getter class called Item also. This is taken from a example I might have to rewrite all the code or at least some of it.Java Code:private void parseDocument(){ //get the root elememt Element docEle = dom.getDocumentElement(); //get a nodelist of <employee> elements NodeList nl = docEle.getElementsByTagName("Item"); if(nl != null && nl.getLength() > 0) { for(int i = 0 ; i < nl.getLength();i++) { //get the employee element Element el = (Element)nl.item(i); //get the Employee object Item e = getItem(el); //add it to list myEmpls.add(e); } } private Item getItem(Element empEl) { String ItemNo = empEl.getAttribute("ItemNo"); String Name = empEl.getAttribute("Name"); String Value = empEl.getAttribute("Value"); String Config = empEl.getAttribute("Config"); Item e = new Item(ItemNo, Name, Value, Config); // this is the way I would like the array(s) to come out and be put in the table return e; }
Hopes this clarify someLast edited by KarlNorway; 01-27-2012 at 08:32 PM.
- 01-27-2012, 08:32 PM #22
Re: Split string and joining substrings
Please explain what your problem is now?
How does your posted code answer the questions I asked in post#20?
- 01-27-2012, 08:42 PM #23
Member
- Join Date
- Dec 2011
- Posts
- 57
- Rep Power
- 0
Re: Split string and joining substrings
Probaly not straight out but Ill try to elaborate.
I get each attribute out of the xml node as a single string using the getter/ setter class.
Then the single strings are added to an Item element array ( I think) and returned to a (object)list
I have tried to get the Item array to the table but without sucsess.
Still the same problem ( Im really a n00b lol)
- 01-27-2012, 08:47 PM #24
Re: Split string and joining substrings
If you get the attributes one by one, then where is the problem putting them into an array?I get each attribute out of the xml node as a single string
Why did you use the split method in the earlier code? How did they get into one String?
- 01-27-2012, 08:49 PM #25
Member
- Join Date
- Dec 2011
- Posts
- 57
- Rep Power
- 0
Re: Split string and joining substrings
This is the way they get into one string:
Here is snippet from where I get the attributes into stringsJava Code:Iterator it = myEmpls.iterator(); while(it.hasNext()) { Item = it.next().toString(); sbi.append(Item);
As far as I know: when i try to make the output from this I get this Array constants can only be used in initializers error in eclipseJava Code:private Item getItem(Element empEl) { String ItemNo = empEl.getAttribute("ItemNo"); String Name = empEl.getAttribute("Name"); String Value = empEl.getAttribute("Value"); String Config = empEl.getAttribute("Config"); Items = {ItemNo, Name, Value, Config}; // Defined as a String [] in top of class Item e = new Item(ItemNo, Name, Value, Config); return e; }
Like i said I will problaly have to rework the code ( its not smart to copy paste code I have figured out)Last edited by KarlNorway; 01-27-2012 at 09:04 PM.
- 01-27-2012, 09:23 PM #26
Member
- Join Date
- Dec 2011
- Posts
- 57
- Rep Power
- 0
Re: Split string and joining substrings
Im really not good at explaining the problem
I changed the Private Item getItem to private String [] getItem The program actually stopped responding. So no luck.
I have to read a bit more about this I think
- 01-27-2012, 10:30 PM #27
Member
- Join Date
- Dec 2011
- Posts
- 57
- Rep Power
- 0
Re: Split string and joining substrings
I am reworking my code and moving code from separate DOMparser class into main class and it seems to work (Yay for me) xD
- 01-27-2012, 10:45 PM #28
Member
- Join Date
- Dec 2011
- Posts
- 57
- Rep Power
- 0
Re: Split string and joining substrings
Good thing Im bald otherwise I would be by now ;)
So now i get a null point error on the table item thing.
Here is the code that makes it fail:
Here is the error outputJava Code:public void printData(){ Iterator it = myItems.iterator(); while(it.hasNext()) { Item = it.next().toString(); System.out.println(Item); // gives this output: 319097,PCB SIO_1,0,1 // splitting it into a array of four items String delimiter = ","; XMLItem = Item.split(delimiter); for(int i =0; i < XMLItem.length ; i++) System.out.println(XMLItem[i]); System.out.println(XMLItem.length); // TableItem items = new TableItem(table, SWT.NONE); // when these tow lines are uncommented I get error. I have tried with this line above the while loop // items.setText(XMLItem); }
From the ine numbers in the error report I have found it to be the when I try to add to or open tableJava Code:java.lang.IllegalArgumentException: Argument cannot be null 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.TableItem.checkNull(TableItem.java:123) at org.eclipse.swt.widgets.TableItem.<init>(TableItem.java:77) at view.Main.printData(Main.java:354) at view.Main.parseXML(Main.java:238) at view.Main.openXML(Main.java:224) at view.Main$2.widgetSelected(Main.java:117) 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:81) at view.Main.main(Main.java:65)
- 01-27-2012, 10:51 PM #29
Similar Threads
-
Split a String with split()--Help
By danilson in forum New To JavaReplies: 7Last Post: 11-19-2010, 04:08 PM -
String split
By soccer_kid_6 in forum New To JavaReplies: 3Last Post: 10-29-2010, 07:51 PM -
Java help - Replace all if string contains multiple substrings
By bobocheez in forum New To JavaReplies: 9Last Post: 08-31-2010, 05:21 AM -
How to split a String using split function
By Java Tip in forum java.langReplies: 4Last Post: 04-17-2009, 08:27 PM -
How to split a String using split function
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:32 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks