Results 1 to 10 of 10
  1. #1
    krishanu is offline Member
    Join Date
    May 2011
    Posts
    20
    Rep Power
    0

    Default XML Parsing using SAXParser

    Hi All,

    Got a problem with XML parsing. Need help

    Have a xml file
    ======================
    <screen>
    <components>
    <component name="Title" class="javax.swing.JTextField">
    <columns>50</columns>
    <x>10</x>
    <y>20</y>
    <width>200</width>
    <height>50</height>
    <text>text</text>
    </component>
    </components>
    </screen>

    ReadXMLSAXTest.java
    ============================
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;

    import org.xml.sax.Attributes;
    import org.xml.sax.SAXException;
    import org.xml.sax.helpers.DefaultHandler;
    import javax.swing.JTextField;
    import java.awt.Component;

    public class ReadXMLFileSAXTest {
    public static void main(String args[]) {
    try {

    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser saxParser = factory.newSAXParser();

    DefaultHandler handler = new DefaultHandler() {
    String parsedValue;
    java.awt.Component component = null;

    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    if(qName.equalsIgnoreCase("component")) {

    //component object = new component();
    //object =c.newInstance();
    //System.out.println(object);
    try {
    //Class c = Class.forName("javax.swing.JTextField");
    Class c = Class.forName(attributes.getValue("class"));
    component = (java.awt.Component)c.newInstance();
    } catch (ClassNotFoundException e) {
    System.out.println(e);
    }catch (InstantiationException e) {
    System.out.println(e);
    }catch (IllegalAccessException e) {
    System.out.println(e);
    }
    System.out.println(component);
    }
    }

    public void characters(char[] ch, int start, int length) throws SAXException {
    parsedValue = new String(ch,start,length);
    }

    public void endElement(String uri, String localName, String qName) throws SAXException {
    javax.swing.JTextField object = null;

    if(component instanceof javax.swing.JTextField)
    {
    object = (javax.swing.JTextField)component;
    } else {
    return;
    }

    if (qName.equalsIgnoreCase("COLUMNS")) {
    object.setColumns(Integer.parseInt(parsedValue));
    }

    if (qName.equalsIgnoreCase("X")) {
    //object.setX(Integer.parseInt(parsedValue));
    }

    if (qName.equalsIgnoreCase("Y")) {
    //object.setY(Integer.parseInt(parsedValue));
    }

    if (qName.equalsIgnoreCase("WIDTH")) {
    //object.setWidth(Integer.parseInt(parsedValue));
    }

    if (qName.equalsIgnoreCase("HEIGHT")) {
    //object.setHeight(Integer.parseInt(parsedValue));
    }
    if (qName.equalsIgnoreCase("TEXT")) {
    object.setText(parsedValue);
    }
    System.out.println(object);
    }
    };

    saxParser.parse("D:\\JavaProgram\\XML\\sample.xml" , handler);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }


    Question is I can not use object.setX(parsedvalue).....i need to implement...setLocation(int x, int y)
    and same for setWidth()..need to use setSize()

    how to implement setLocation () and setSize() in my code ,, so that i can use setX, setY, setWidth, and setHeight

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

    Default

    Get hold of x and y (and width and height for the other one) into local variables and then use setLocation, or setSize.

  3. #3
    krishanu is offline Member
    Join Date
    May 2011
    Posts
    20
    Rep Power
    0

    Default

    Hi Tolls,
    Thanks for your reply....

    as u said....
    just trying to implement....and very new to java....so just need little more help
    Point location = null /* Is that correct?? */
    if (qName.equalsIgnoreCase("X")) {
    int x = object.getX();
    if (qName.equalsIgnoreCase("Y")) {
    int y = object.getY();
    object.setLocation(int x, int y));
    //Wanted to print the value (How to do that???)
    Point location = object.getLocation();
    }
    }
    System.out.println(location);

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

    Default

    What happend to the:
    Java Code:
    Integer.parseInt(parsedValue)
    ?

    Why are you getting the values from the object, when I thought you wanted to set the values?

  5. #5
    krishanu is offline Member
    Join Date
    May 2011
    Posts
    20
    Rep Power
    0

    Default

    I wanted to get the value from the object!!!!
    my XML file :
    <component name="Title" class="javax.swing.JTextField">
    <columns>50</columns>
    <x>10</x>
    <y>20</y>
    <width>200</width>
    <height>50</height>
    <text>text</text>
    </component>
    just trying to understand >> Created object for javax.swing.JTextField...lets name "object"
    if i return that object ..i can see in the console "columns" is to 50, similarly..for x and y i want to get the value.

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

    Default

    No.
    The code you posted in the OP shows you trying to read the data from the XML and set the values in the JTextField.

    So you're setting the values on the object, not getting them.

    Which is presumably why you're using setColumns() in the OP code above.

    I'm now confused because your new code shows you getting the data from the JTextField object (you really need to use better names for your variables)...which makes no sense to me.

  7. #7
    krishanu is offline Member
    Join Date
    May 2011
    Posts
    20
    Rep Power
    0

    Default

    if (qName.equalsIgnoreCase("X")) {
    int x = Integer.parseInt(parsedValue);
    //int x = object.getX();
    if (qName.equalsIgnoreCase("Y")) {
    int y = Integer.parseInt(parsedValue);
    //int y = object.getY();
    object.setLocation(int x,int y); ///but this is giving me error ".class expected"


    is this correct...for setting the value......???

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

    Default

    The structure looks ok.
    If you;re getting an error then you'll have to post the error (and any stack trace) and the relevant code.

  9. #9
    krishanu is offline Member
    Join Date
    May 2011
    Posts
    20
    Rep Power
    0

    Default

    Ok..Here is the part of code..

    public void endElement(String uri, String localName, String qName) throws SAXException {

    javax.swing.JTextField object = null;


    if(component instanceof javax.swing.JTextField)
    {

    object = (javax.swing.JTextField)component;


    } else {
    return;
    }

    if (qName.equalsIgnoreCase("COLUMNS")) {
    object.setColumns(Integer.parseInt(parsedValue));
    }

    if (qName.equalsIgnoreCase("X")) {
    int x = Integer.parseInt(parsedValue);

    if (qName.equalsIgnoreCase("Y")) {

    int y = Integer.parseInt(parsedValue);

    object.setLocation(int x, int y);
    }
    }

    Error I am getting is ::::::

    D:\>javac ReadXMLFileSAXTest.java
    ReadXMLFileSAXTest.java:98: '.class' expected
    object.setLocation(int x, int y);
    ^
    ReadXMLFileSAXTest.java:98: ';' expected
    object.setLocation(int x, int y);
    ^
    ReadXMLFileSAXTest.java:98: ';' expected
    object.setLocation(int x, int y);
    ^
    3 errors


    Sorry I am just new to java...so may be it is simple,,,but i am strugling...

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

    Default

    Please use code tags when posting code.

    Quote Originally Posted by krishanu View Post
    Ok..Here is the part of code..

    Java Code:
    public void endElement(String uri, String localName, String qName) throws SAXException {
    					
    javax.swing.JTextField object = null;
    					
    
    if(component instanceof javax.swing.JTextField)
    {
    						
    object = (javax.swing.JTextField)component;
    
    						
    } else {
    	return;
    }
    				
    if (qName.equalsIgnoreCase("COLUMNS")) {
    		object.setColumns(Integer.parseInt(parsedValue));
    	}
    			 
    if (qName.equalsIgnoreCase("X")) {
    	int x = Integer.parseInt(parsedValue);
    					
    	if (qName.equalsIgnoreCase("Y")) {
    								
    		int y = Integer.parseInt(parsedValue);
    					
    			object.setLocation(int x, int y);
    			}
    	}
    Error I am getting is ::::::

    D:\>javac ReadXMLFileSAXTest.java
    ReadXMLFileSAXTest.java:98: '.class' expected
    object.setLocation(int x, int y);
    ^
    ReadXMLFileSAXTest.java:98: ';' expected
    object.setLocation(int x, int y);
    ^
    ReadXMLFileSAXTest.java:98: ';' expected
    object.setLocation(int x, int y);
    ^
    3 errors


    Sorry I am just new to java...so may be it is simple,,,but i am strugling...
    If you're new to Java then why are you doing an XMLParser?
    That's a bit like being new to driving and deciding the best way to learn is to sign up for a Rally.

    Errors like that are the compiler flailing because your basic code structure is wrong.
    There'll be a bracket missing somewhere, or one too many.
    Possibly a missing semi-colon, but I'd lay good odds on it being an extra bracket.

    Layout your code correctly, matching braces, and you'll see straight away where the problem is.

Similar Threads

  1. Replies: 1
    Last Post: 01-20-2010, 05:52 PM
  2. Parsing
    By Matt Sakko in forum New To Java
    Replies: 14
    Last Post: 03-21-2009, 05:49 PM
  3. xml parsing
    By gaurav65176 in forum XML
    Replies: 5
    Last Post: 11-12-2008, 12:07 PM
  4. Initializing SAXParser
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-14-2007, 07:33 PM

Posting Permissions

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