Results 1 to 6 of 6
  1. #1
    KarlNorway is offline Member
    Join Date
    Dec 2011
    Posts
    57
    Rep Power
    0

    Default Selecting the best parser method

    Hi

    I have a xml file looking like this (note all info in Attribute)
    Java Code:
    <?xml version="1.0" encoding="utf-8" ?>
    
    
    <Component>
    <Type Name="Sensor">
        <Item ItemNo="31"	Name="PCB 1" Value=""  Config="1"></Item>
        <Item ItemNo="33"	Name="PCB Mini" Value="" Config="2"></Item>
      </Type>
    
      <Type Name="Main">	
        <Item ItemNo="31"	Name="PCB MF Board" Value=""  Config="1"></Item>
        <Item ItemNo="34" 	Name="PCB LF  Board" Value=""  Config="1"></Item>
      </Type>
    </Component>
    In order to parse and sort the different attributes. What would be the best parser?

    I have looked at StAX and DOM but not sure I'm able to read all the attributes?

    Im trying to make a little program that uses a combo to diaplay and select the type name nodes and then show all the attributes in a table, where the user can add different items.


    Hoping for some help.

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

    Default Re: Selecting the best parser method

    Im trying to use the StAX parser but no luck yet.

    Here is the code:
    Java Code:
    if (event.isStartElement()) {
    					StartElement startElement = event.asStartElement();
    					// If we have a item element we create a new item
    					if (startElement.getName().getLocalPart() == (TYPE)) {
    						item = new Item();
    						// We read the attributes from this tag and add the Type Name 
    						// attribute to our object
    						Iterator<Attribute> attributes = startElement.getAttributes();
    						while (attributes.hasNext()) {
    							Attribute attribute = attributes.next();
    							if (attribute.getName().toString().equals(TYPE_NAME)) {
    								item.setType_Name(attribute.getValue());
    							}
    						}
    					}
    					
    					if (event.isStartElement()) {
    //						StartElement startElement2 = event.asStartElement();
    						// If we have a item element we create a new item
    						if (event.asStartElement().getName().getLocalPart() == (ITEM)) {
    //						item = new Item();
    							// We read the attributes from this tag and add the Item
    							// attributes to our object
    							Iterator<Attribute> attributes = startElement.getAttributes();
    							while (attributes.hasNext()) {
    								Attribute attribute = attributes.next();
    								if (attribute.getName().toString().equals(ITEMNO)) {
    									item.setItemNo(attribute.getValue());
    								}
    								if (attribute.getName().toString().equals(NAME)) {
    									item.setName(attribute.getValue());
    								}
    								if (attribute.getName().toString().equals(VALUE)) {
    									item.setValue(attribute.getValue());
    								}
    								if (attribute.getName().toString().equals(CONFIG)) {
    									item.setConfig(attribute.getValue());
    								}
    
    							}
    						}

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default Re: Selecting the best parser method

    Java Code:
    startElement.getName().getLocalPart() == (TYPE)
    and
    event.asStartElement().getName().getLocalPart() == (ITEM)
    Looks to me like you're comparing Strings with == rather than equals().

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

    Default Re: Selecting the best parser method

    @Tolls:
    It didnd help me. Still no output to the console..



    I actually read that DOM is the best way since Im going to add and change the XML file.. More comming ;)
    Last edited by KarlNorway; 01-12-2012 at 11:08 AM.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default Re: Selecting the best parser method

    You'll need to stick some debug in there then to see what's going on.

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

    Default Re: Selecting the best parser method

    Ok so now I can get the attributes in to the console using this code:
    Java Code:
    private void printData(){
    		
    		System.out.println("No of Employees '" + myEmpls.size() + "'.");
    		System.out.println("No of Employees '" + MyTypes.size() + "'.");
    		
    		Iterator it = myEmpls.iterator();
    		while(it.hasNext()) {
    			System.out.println(it.next().toString());
    		}
    		Iterator itT = MyTypes.iterator();
    		while(itT.hasNext()) {
    			System.out.println(itT.next().toString());
    		}
    	}
    Where MyTypes and myEmps are array lists. Can I only "print" out to string array and then use that array to fill out a table?

    Here is my guess.

    There is one place in code where I have this
    Java Code:
    //add it to list
    				MyTypes.add(eT);
    and this
    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");
    		
    		//Create a new Item with the value read from the xml nodes
    		Item e = new Item(ItemNo,Name, Value, Config);
    		
    		return e;
    	}
    Im guessing by changing some of it so that the MyTypes.add(eT); is removed and by changing this
    Java Code:
    Item e = new Item(ItemNo,Name, Value, Config);
             //  TO
    String itemArr [] = {ItemNo, Name, Value, Config};
    and displaying like this
    Java Code:
    System.out.println(itemArr[]);
    It will work.
    Last edited by KarlNorway; 01-12-2012 at 01:55 PM. Reason: Adding and changing

Similar Threads

  1. Selecting Shape
    By lifes46 in forum Java 2D
    Replies: 3
    Last Post: 05-05-2011, 05:30 PM
  2. Selecting a text
    By mathnitin in forum Advanced Java
    Replies: 12
    Last Post: 02-27-2011, 07:04 PM
  3. Selecting every other array
    By Rivy2112 in forum New To Java
    Replies: 10
    Last Post: 10-11-2010, 10:39 PM
  4. JFileChooser example (selecting a directory)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-14-2008, 12:02 PM
  5. selecting a record in database
    By ramachandran in forum New To Java
    Replies: 0
    Last Post: 10-25-2007, 07:06 AM

Posting Permissions

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