Page 2 of 2 FirstFirst 12
Results 21 to 29 of 29
  1. #21
    KarlNorway is offline Member
    Join Date
    Dec 2011
    Posts
    57
    Rep Power
    0

    Default 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
    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;
    	}
    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.

    Hopes this clarify some
    Last edited by KarlNorway; 01-27-2012 at 08:32 PM.

  2. #22
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default 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?

  3. #23
    KarlNorway is offline Member
    Join Date
    Dec 2011
    Posts
    57
    Rep Power
    0

    Default 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)

  4. #24
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: Split string and joining substrings

    I get each attribute out of the xml node as a single string
    If you get the attributes one by one, then where is the problem putting them into an array?
    Why did you use the split method in the earlier code? How did they get into one String?

  5. #25
    KarlNorway is offline Member
    Join Date
    Dec 2011
    Posts
    57
    Rep Power
    0

    Default Re: Split string and joining substrings

    This is the way they get into one string:
    Java Code:
    Iterator it = myEmpls.iterator();
    		while(it.hasNext()) {
    			Item = it.next().toString();
    			sbi.append(Item);
    Here is snippet from where I get the attributes into strings
    Java 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;
    	}
    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 eclipse




    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.

  6. #26
    KarlNorway is offline Member
    Join Date
    Dec 2011
    Posts
    57
    Rep Power
    0

    Default 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

  7. #27
    KarlNorway is offline Member
    Join Date
    Dec 2011
    Posts
    57
    Rep Power
    0

    Default 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

  8. #28
    KarlNorway is offline Member
    Join Date
    Dec 2011
    Posts
    57
    Rep Power
    0

    Default 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:
    Java 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);  
    			}
    Here is the error output
    Java 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)
    From the ine numbers in the error report I have found it to be the when I try to add to or open table

  9. #29
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: Split string and joining substrings

    What variable is null? What value does table have?

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Split a String with split()--Help
    By danilson in forum New To Java
    Replies: 7
    Last Post: 11-19-2010, 04:08 PM
  2. String split
    By soccer_kid_6 in forum New To Java
    Replies: 3
    Last Post: 10-29-2010, 07:51 PM
  3. Replies: 9
    Last Post: 08-31-2010, 05:21 AM
  4. How to split a String using split function
    By Java Tip in forum java.lang
    Replies: 4
    Last Post: 04-17-2009, 08:27 PM
  5. How to split a String using split function
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 09:32 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •