Results 1 to 6 of 6
Thread: Selecting the best parser method
- 01-09-2012, 11:33 AM #1
Member
- Join Date
- Dec 2011
- Posts
- 57
- Rep Power
- 0
Selecting the best parser method
Hi
I have a xml file looking like this (note all info in Attribute)
In order to parse and sort the different attributes. What would be the best parser?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>
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.
- 01-12-2012, 07:51 AM #2
Member
- Join Date
- Dec 2011
- Posts
- 57
- Rep Power
- 0
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()); } } }
- 01-12-2012, 09:48 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,467
- Rep Power
- 16
Re: Selecting the best parser method
Looks to me like you're comparing Strings with == rather than equals().Java Code:startElement.getName().getLocalPart() == (TYPE) and event.asStartElement().getName().getLocalPart() == (ITEM)
- 01-12-2012, 10:59 AM #4
Member
- Join Date
- Dec 2011
- Posts
- 57
- Rep Power
- 0
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.
- 01-12-2012, 12:27 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,467
- Rep Power
- 16
Re: Selecting the best parser method
You'll need to stick some debug in there then to see what's going on.
- 01-12-2012, 01:40 PM #6
Member
- Join Date
- Dec 2011
- Posts
- 57
- Rep Power
- 0
Re: Selecting the best parser method
Ok so now I can get the attributes in to the console using this code:
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?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()); } }
Here is my guess.
There is one place in code where I have thisand thisJava Code://add it to list MyTypes.add(eT);
Im guessing by changing some of it so that the MyTypes.add(eT); is removed and by changing thisJava 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; }and displaying like thisJava Code:Item e = new Item(ItemNo,Name, Value, Config); // TO String itemArr [] = {ItemNo, Name, Value, Config};It will work.Java Code:System.out.println(itemArr[]);
Last edited by KarlNorway; 01-12-2012 at 01:55 PM. Reason: Adding and changing
Similar Threads
-
Selecting Shape
By lifes46 in forum Java 2DReplies: 3Last Post: 05-05-2011, 05:30 PM -
Selecting a text
By mathnitin in forum Advanced JavaReplies: 12Last Post: 02-27-2011, 07:04 PM -
Selecting every other array
By Rivy2112 in forum New To JavaReplies: 10Last Post: 10-11-2010, 10:39 PM -
JFileChooser example (selecting a directory)
By Java Tip in forum Java TipReplies: 1Last Post: 02-03-2009, 01:25 PM -
selecting a record in database
By ramachandran in forum New To JavaReplies: 0Last Post: 10-25-2007, 07:06 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks