views:

174

answers:

2

Hi again friends... The problem again is that though i have succesfully implemented a SAX parser in my code... It is behaving wierdly. It jus skips the enteries after the & and goes to the next entry. Just wanted to know whether this is the typical working of SAX parser or m i implementing it wrongly???

I have implemented org.xml.sax.ContentHandler and have provided the following coding inside...

`

public void characters(char[] ch, int start, int length)
         {
             if(lastName.equals("id"))
             {
                 String id = String.copyValueOf(ch, start, length);
                 CustomList.idvector.add(id);
             }
             else if(lastName.equals("subcategory"))
             {
                String subcategory = String.copyValueOf(ch, start, length);
                 CustomList.subcategoryvector.add(subcategory);
             }
             else if(lastName.equals("photo"))
             {
                 String photo = String.copyValueOf(ch, start, length);
                 CustomList.photovector.add(photo);
             }
             else if(lastName.equals("name"))
             {
                 String name = String.copyValueOf(ch, start, length);
                 CustomList.namevector.add(name);
             }
         }

`

There are elements with tags ,,,... and m taking those info into a vector... is this correct???

Now again problem is that i cant parse special character like "$" and such... is there any way we can catch these characters??

+1  A: 

Not quite sure exactly what you're seeing, if this doesn't help deal with your problem maybe you could provide some sample input and output.

& is an XML entity reference and means &.

By default, SAX will do the conversion for you, so if your source XML says hello&goodbye you should see hello&goodbye.

It may also be that SAX is breaking up the calls to the characters() method because of the entity reference. You may need to concatenate multiple calls to characters() together whilst still inside the same tag.

Brabster
Thanx Brabster... m going to try ur solution... i'll provide the input/output...
JaVadid
But 1 more doubt... it is foolish of me asking it but does space create any problems??
JaVadid
JaVadid
Apologies, wasn't around my computer. Glad you sorted your problem out.
Brabster
A: 

This worked 4 me guys... Thanx Brabster...

public void characters(char[] ch, int start, int length)
             {
                 if(lastName.equals("id") && (lastName != lastElementTraversed))
                 {
                     String s = new String(ch, start, length);
                     if (appendedid == null) 
                     {
                         appendedid = new StringBuffer(s);
                     } else {
                          appendedid.append(s);
                     }

                 }
            else if(lastName.equals("subcategory")&& (lastName !=lastElementTraversed))
                 {
                     String s = new String(ch, start, length);
                     if (appendedsub == null) {
                         appendedsub = new StringBuffer(s);
                      } else {
                          appendedsub.append(s);
                      }
                     //lastElementTraversed = lastName;
                 }
            else if(lastName.equals("photo")&& (lastName != lastElementTraversed))
                 {
                     String s = new String(ch, start, length);
                      if (appendedphoto == null) {
                          appendedphoto = new StringBuffer(s);
                      } else {
                          appendedphoto.append(s);
                      }
                      //lastElementTraversed = lastName;
                 }
            else if(lastName.equals("name") && (lastName != lastElementTraversed))
                 {
                     String s = new String(ch, start, length);
                      if (appendedname == null) {
                          appendedname = new StringBuffer(s);
                      } else {
                          appendedname.append(s);
                      } 
                      //lastElementTraversed = lastName;
                 }
             }
    public void startElement(String uri, String localName, String qName, Attributes atts)
            {
                lastName = localName;
                appendedid=null;
                appendedsub=null;
                appendedphoto=null;
                appendedname=null;

            }

             public void endElement(String uri, String localName, String qName)
             {
                 lastElementTraversed = localName;
                 if(localName.equals("category"))
                    {
                        appendedid=null;
                        appendedsub=null;
                        appendedphoto=null;
                        appendedname=null;

                    }

                     if(appendedid!=null)
                     CustomList.idvector.add(appendedid.toString());
                     if(appendedsub!=null)
                     CustomList.subcategoryvector.add(appendedsub.toString());
                     if(appendedphoto!=null)
                     CustomList.photovector.add(appendedphoto.toString());
                     if(appendedname!=null)
                     CustomList.namevector.add(appendedname.toString());

             }
        }
JaVadid
Well actually m checking a lot of things coz the XML file my client has is really messy... forget the things u don understand... i guess u'll understand most of it... :)
JaVadid
hi guys the problem again is that i cant catch the special characters like "$" and bullets n all... is there a way out in SAX parsing or do we need to shift to some other parsing method... does JSON fix this???
JaVadid