11 Replies - 1283 Views - Last Post: 14 July 2010 - 01:02 PM Rate Topic: -----

#1 Matamane   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 111
  • Joined: 12-August 09

Problem with My Parser Code

Posted 13 July 2010 - 02:42 PM

I'm having a few getting my code working right. I am new to this type of coding, so any help would be great.

My problems are several. It all compiles well, but I am not getting any output. From what I understand, the size of the arrays are 0, but I am not sure why. Even when I manually set the array lengths, I still don't get any output, so there appears to be something wrong on the functionality level. The tagPointers array is only storing zeroes, but is working in all other respects.

Here is the code

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import java.net.URL;

public class XmlPullRasterizer
{
    XmlPullParserFactory factory;
    XmlPullParser xpp;
    URL url;
    int[] tagPointers;
    String[] attributePointers;


    public XmlPullRasterizer(String uri) throws Exception
    {
        factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);   
        xpp = factory.newPullParser();
        url = new URL(uri);
        xpp.setInput(url.openStream(), null);
        this.tagPointers = new int[getRelevantTagCount()];
        this.attributePointers = new String[getRelevantAttributeCount()];
    }

    public int getRelevantTagCount() throws Exception
    {
        int eventType = xpp.getEventType();
        int tagCount = 0;
        while (eventType != XmlPullParser.END_DOCUMENT)
        {
            if(isRelevantTag())
            {
                tagCount++;
            }
            eventType = xpp.next();
        }
        return tagCount;
    }

    public int getRelevantAttributeCount() throws Exception
    {
        int eventType = xpp.getEventType();
        int attributeCount = 0;
        while (eventType != XmlPullParser.END_DOCUMENT)
        {
            if(isRelevantTag())
            {
                attributeCount = attributeCount + xpp.getAttributeCount();
            }
            eventType = xpp.next();
        }
        return attributeCount;
    }

    public void setTagPointers() throws Exception
    {
        int eventType = xpp.getEventType();
        int k = 0;
        while (eventType != XmlPullParser.END_DOCUMENT)
        {
            if(eventType == XmlPullParser.START_TAG)
            {
                if(isRelevantTag())
                {
                    if(xpp.getName() == "circle")
                    {
                        tagPointers[k] = 0;
                    }
                    else if(xpp.getName() == "ellipse")
                    {
                        tagPointers[k] = 1;
                    }
                    else if(xpp.getName() == "line")
                    {
                        tagPointers[k] = 2;
                    }
                    else if(xpp.getName() == "polygon")
                    {
                        tagPointers[k] = 3;
                    }
                    else if(xpp.getName() == "polyline")
                    {
                        tagPointers[k] = 4;
                    }
                    else if(xpp.getName() == "rectangle")
                    {
                        tagPointers[k] = 5;
                    }
                    else if(xpp.getName() == "text")
                    {
                        tagPointers[k] = 6;
                    }                    
                    k++;
                }
            }
            eventType = xpp.next();
        }
    }

    public void setAttributePointers() throws Exception
    {
        int eventType = xpp.getEventType();
        int k = 0;
        while (eventType != XmlPullParser.END_DOCUMENT)
        {
            if(eventType == XmlPullParser.START_TAG)
            {
                if(isRelevantTag())
                {
                    int attributeCount = xpp.getAttributeCount();
                    for(int i = 0; i < attributeCount; i++)
                    {
                        attributePointers[k] = xpp.getAttributeValue(i);
                        k++;
                    }
                }
            }
        }
    }
    
    public boolean isRelevantTag() throws Exception
    {
        if(xpp.getName() == "circle")
        {
            return true;
        }
        else if(xpp.getName() == "ellipse")
        {
            return true;
        }
        else if(xpp.getName() == "line")
        {
            return true;
        }
        else if(xpp.getName() == "polygon")
        {
            return true;
        }
        else if(xpp.getName() == "polyline")
        {
            return true;
        }
        else if(xpp.getName() == "rectangle")
        {
            return true;
        }
        else if(xpp.getName() == "text")
        {
            return true;
        }
        else
        {
            return false;
        }
    }
                  
    public static void main(String[] args)throws Exception
    {
        String uri = "file:///C:/Users/Matthew/Documents/NetBeansProjects/SVG/SVGDemo.svg";
        XmlPullRasterizer pullRasterizer = new XmlPullRasterizer(uri);
        pullRasterizer.setAttributePointers();
        pullRasterizer.setTagPointers();
        for(int i = 0; i < pullRasterizer.attributePointers.length; i++)
        {
            System.out.println(pullRasterizer.attributePointers[i]);
        }
    }
}

This post has been edited by Matamane: 13 July 2010 - 02:44 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Problem with My Parser Code

#2 nick2price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 565
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Problem with My Parser Code

Posted 13 July 2010 - 03:00 PM

to make things easier, start doing some debugging. So, start with the setTagPointers method. Underneath each
tagPointers[k] = 0;


print out to the console the shape e.g.
System.out.println("Circle");


This will let you know if it is getting into any of them statements.
Was This Post Helpful? 0
  • +
  • -

#3 Matamane   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 111
  • Joined: 12-August 09

Re: Problem with My Parser Code

Posted 13 July 2010 - 05:01 PM

It's not working.
Was This Post Helpful? 0
  • +
  • -

#4 nick2price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 565
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Problem with My Parser Code

Posted 13 July 2010 - 06:25 PM

Its not working as in nothing is being printed out? Well, that tells you it is never getting into any of the statements. In the same method, add these print statements.

public void setTagPointers() throws Exception{
        int eventType = xpp.getEventType();
        System.out.println(eventType.toString());
	int k = 0;
	
        while (eventType != XmlPullParser.END_DOCUMENT)
	{
            System.out.println("WHILE ENTERED");


And tell me what prints out. Sorry about the code allignment, seems to have gone mad on me.
Was This Post Helpful? 0
  • +
  • -

#5 Matamane   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 111
  • Joined: 12-August 09

Re: Problem with My Parser Code

Posted 13 July 2010 - 06:27 PM

I got it working.

I had to reset the parser.

once it went to END_DOCUMENT the first time, it couldn't go into the next while statements
Was This Post Helpful? 0
  • +
  • -

#6 nick2price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 565
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Problem with My Parser Code

Posted 13 July 2010 - 06:32 PM

kool. Next time when you have an issue and there are a lot of statements involved, try to get in the habit of chucking print statements everywhere. This way, you can see where your program is not getting too, helping you debug things.
Was This Post Helpful? 0
  • +
  • -

#7 m-e-g-a-z   User is offline

  • Winning
  • member icon


Reputation: 497
  • View blog
  • Posts: 1,457
  • Joined: 19-October 09

Re: Problem with My Parser Code

Posted 14 July 2010 - 08:47 AM

You got it working? You do know this is the wrong way of comparing Strings since it compares the Strings memory allocations rather than the actual values.

This below
else if(xpp.getName() == "text")

Should be this
else if(xpp.getName().equals("text"))

Applying the .equals() method to the rest of your code where you are using == to compare Strings.
Was This Post Helpful? 0
  • +
  • -

#8 Matamane   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 111
  • Joined: 12-August 09

Re: Problem with My Parser Code

Posted 14 July 2010 - 12:35 PM

View Postm-e-g-a-z, on 14 July 2010 - 07:47 AM, said:

You got it working? You do know this is the wrong way of comparing Strings since it compares the Strings memory allocations rather than the actual values.

This below
else if(xpp.getName() == "text")

Should be this
else if(xpp.getName().equals("text"))

Applying the .equals() method to the rest of your code where you are using == to compare Strings.


Oddly enough, it is how it is done in the sample application code.
Was This Post Helpful? 0
  • +
  • -

#9 Luckless   User is offline

  • </luck>
  • member icon

Reputation: 294
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: Problem with My Parser Code

Posted 14 July 2010 - 12:55 PM

Mat, I would take his suggestion however. It's best to exercise good coding habit all the time :bigsmile: Though it may work in your particular situation, it isn't for the reasons that you think. You see, the .equals() method compares the string to the string while == compares two object references to see whether or not it is the same instance.
Was This Post Helpful? 0
  • +
  • -

#10 m-e-g-a-z   User is offline

  • Winning
  • member icon


Reputation: 497
  • View blog
  • Posts: 1,457
  • Joined: 19-October 09

Re: Problem with My Parser Code

Posted 14 July 2010 - 12:55 PM

Sorry to say but this example isn't good. You can have a look at this which expands on what I have pointed out. Just because it's an example you got from somewhere dosen't mean it's right.
Was This Post Helpful? 0
  • +
  • -

#11 Luckless   User is offline

  • </luck>
  • member icon

Reputation: 294
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: Problem with My Parser Code

Posted 14 July 2010 - 12:59 PM

sorry bud, but the link isn't working. What isn't right about my statement? That's exactly what it does in the case of Strings.
Was This Post Helpful? 0
  • +
  • -

#12 Matamane   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 111
  • Joined: 12-August 09

Re: Problem with My Parser Code

Posted 14 July 2010 - 01:02 PM

I'm newer to this than I would like to admit, so when able, I use what the developer shows for good measure. But I'll remember from now on.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1