Results 1 to 10 of 10
- 12-20-2011, 12:13 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 57
- Rep Power
- 0
Retriving string from private static final class
HI
I want to get the content of a string created from a xml parser.
The parsere operates within this:
Is there a way to get it out so I can use it in other part of my application?Java Code:private static final class SaxHandler extends DefaultHandler {
I have aAnd I want to use the content of ItemNo, Name, Value and Config to populate a table.Java Code:System.out.println("Item " + ItemNo +" " + Name +" " + Value +" "+ Config);
- 12-20-2011, 02:12 PM #2
Re: Retriving string from private static final class
If you want help, you should provide an SSCCE that demonstrates the problem. Just a couple classes with a member and the correct modifiers.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 12-20-2011, 02:21 PM #3
Member
- Join Date
- Dec 2011
- Posts
- 57
- Rep Power
- 0
Re: Retriving string from private static final class
Like this?Java Code:public class Main { protected Shell shlOneWireComponents; private Table table; private Text txtConfig; private Text txtItemText; private Text txtItemNo; private Text txtItemValue; public String [] XMLItem; /** * Launch the application. * @param args */ public static void main(String[] args) { try { Main window = new Main(); window.open(); } catch (Exception e) { e.printStackTrace(); } } /** * Open the window. */ public void open() { Display display = Display.getDefault(); createContents(); shlOneWireComponents.open(); shlOneWireComponents.layout(); while (!shlOneWireComponents.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } // display.dispose(); } } /** * Create contents of the window. */ protected void createContents() { shlOneWireComponents = new Shell(); shlOneWireComponents.setMinimumSize(new Point(450, 450)); shlOneWireComponents.setSize(541, 450); shlOneWireComponents.setText("One Wire components editor"); Button btnSave = new Button(shlOneWireComponents, SWT.NONE); btnSave.setToolTipText("Saving the entries in the textboxes below"); btnSave.setFont(SWTResourceManager.getFont("Arial", 12, SWT.NORMAL)); btnSave.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { addToTable(); } }); btnSave.setBounds(84, 10, 68, 31); btnSave.setText("ADD"); Button btnOpen = new Button(shlOneWireComponents, SWT.PUSH); btnOpen.setToolTipText("Open CnodeComponents.xml file"); btnOpen.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { openXML(); } }); btnOpen.setFont(SWTResourceManager.getFont("Arial", 12, SWT.NORMAL)); btnOpen.setBounds(10, 10, 68, 31); btnOpen.setText("Open"); table = new Table(shlOneWireComponents, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI); table.setFont(SWTResourceManager.getFont("Arial", 12, SWT.NORMAL)); table.setBounds(10, 193, 517, 213); table.setHeaderVisible(true); table.setLinesVisible(true); TableColumn tblclmnItemNo = new TableColumn(table, SWT.NONE); tblclmnItemNo.setWidth(100); tblclmnItemNo.setText("Item No"); TableColumn tblclmnItemText = new TableColumn(table, SWT.NONE); tblclmnItemText.setWidth(215); tblclmnItemText.setText("Item Text"); TableColumn tblclmnItemValue = new TableColumn(table, SWT.NONE); tblclmnItemValue.setWidth(100); tblclmnItemValue.setText("Item Value"); TableColumn tblclmnItemConfig = new TableColumn(table, SWT.NONE); tblclmnItemConfig.setWidth(100); tblclmnItemConfig.setText("Item Config"); } public void openXML(){ try { // creates and returns new instance of SAX-implementation: SAXParserFactory factory = SAXParserFactory.newInstance(); // create SAX-parser... SAXParser parser = factory.newSAXParser(); // .. define our handler: SaxHandler handler = new SaxHandler(); // and parse: parser.parse("CNodeComponents.xml", handler); } catch (Exception ex) { ex.printStackTrace(System.out); } // TableItem item1 = new TableItem(table, SWT.NONE); // item1.setText(XMLItem); } /** * Our own implementation of SAX handler reading * a purchase-order data. */ 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 XMLItem = 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; } } public void valueChanged(Text text) { if (!text.isFocusControl()) return; } }
I wold like the output from( or same as) System.out.println(); to become a string array that i can add as tableItem
- 12-20-2011, 02:35 PM #4
Re: Retriving string from private static final class
If it is private, then there isn't a way to do it without hacking something nasty together with reflection.
- 12-20-2011, 02:47 PM #5
Re: Retriving string from private static final class
That's not really an SSCCE. Are you just asking how to add a getter function?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 12-21-2011, 06:49 AM #6
Member
- Join Date
- Dec 2011
- Posts
- 57
- Rep Power
- 0
Re: Retriving string from private static final class
@Kevin Im not sure. I want the parsed xml strings to "jump" into my table. I know how to add to the table, but not how to get the system output into a string array that I can use..
@quad64bit Will it help if I try to use it as a public instead of private??
Im using SAX parser. The file is quite small. Would it be better to use DOM parser?
Im planning on adding to the opened file later on.Last edited by KarlNorway; 12-21-2011 at 07:10 AM.
- 12-21-2011, 07:34 AM #7
Re: Retriving string from private static final class
Sorry, I mis-read your post, private/public doesn't have anything to do with anything.
If you want to use that specific instance elsewhere, you can do it three ways:
1) You could pass a reference to the SaxHandler instance to another class
2) You could create a getter for the SaxHandler that is public, then assuming the other class in your app can 'see' the current class, you could simply call that getter to get a reference to the SaxHandler
3) You could move the SaxHandler somewhere else (like a global context) and make it public instead of private.
Does that help?
- 12-21-2011, 08:16 AM #8
Member
- Join Date
- Dec 2011
- Posts
- 57
- Rep Power
- 0
Re: Retriving string from private static final class
I think that the getter function is the way to go for me (for mow anyways.)
Oh is the Sax parser able to write to the document or do then need a DOM parser?
- 12-21-2011, 03:08 PM #9
Re: Retriving string from private static final class
I'm not sure about that, I don't know enough about how the sax parser works. Maybe someone else has an idea?
- 12-21-2011, 09:49 PM #10
Member
- Join Date
- Dec 2011
- Posts
- 57
- Rep Power
- 0
Re: Retriving string from private static final class
After reading up om DOM and SAX Parser. I think that the propper way to go on this is to use DOM parser in a couple of "helper files" in seperate class files. I was thinking one to open and sort the xml content and one to handle saving the file and what ever the user inputs to the table..
Similar Threads
-
A private static ArrayList hold an object and static getter ArrayList
By Louis in forum New To JavaReplies: 2Last Post: 11-16-2010, 05:51 PM -
static final int DONE = (1 << 9) - 1; ???
By alacn in forum New To JavaReplies: 2Last Post: 07-26-2010, 05:31 PM -
What is the difference between Inline literal and Static final String in Java.
By devaru2003 in forum Advanced JavaReplies: 3Last Post: 03-26-2010, 07:09 AM -
problem in retriving string data using jsp
By Manas Das in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 02-02-2009, 08:53 AM -
[SOLVED] is final class members are also final ?
By haoberoi in forum New To JavaReplies: 4Last Post: 11-10-2008, 03:01 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks