How to parse these kinda XML using SAX?

Put your problem here if it does not fit any of the other categories.

How to parse these kinda XML using SAX?

Postby vaishumol » Wed Jun 30, 2010 12:31 pm

Hi friends,
Can anyone help me how to parse the following kinda xml Tags?
Im not getting proper output. And do not know where is my mistake. Please help me.

This is my sample.xml code :


Code: Select all
    <response>
    <lf:request_type>make</lf:request_type>
    <lf:response_code>0</lf:response_code>
    −
    <lf:result type="make" count="94">
    −
    <lf:item id="1">
    <lf:attr name="make">AC</lf:attr>
    <lf:attr name="listing_count">7</lf:attr>
    </lf:item>
    −
    <lf:item id="2">
    <lf:attr name="make">ACURA</lf:attr>
    <lf:attr name="listing_count">32567</lf:attr>
    </lf:item>
    </lf:result>
    </response>




And her is my XML parser code :

Code: Select all
    public class MakeXMLHandler extends DefaultHandler {
       
       
       //test tags see if we have to parse the whole document
       //or only the part we want ???
       
       private boolean result = false;
       private boolean item = false;
       private boolean id = false;
       private boolean attr = false;
       private boolean name = false;
       private boolean maker = false;
       private boolean count = false;   
       
       private ParsedDataSet peds = new ParsedDataSet();
       
       public ParsedDataSet getParsedData(){
          return this.peds;
       }
       
       @Override
       public void startDocument() throws SAXException {
          this.peds = new ParsedDataSet();
       }
       
       @Override
       public void endDocument() throws SAXException {
          //nothing to do here
       }
       
       /*
        * Gets be called on opening tags like
        * <tag>
        * Can provide attribute(s), when xml was like:
        * <tag attribute="attributeValue">*/
        public void startElement(String namespaceURI, String localName,
                             String qName, Attributes atts)throws SAXException{
         
           if(localName.equals("result")){
              this.result = true;
             
           }else if(localName.equals("item")){
              this.item = true;
              this.id = true;         
           }else if(localName.equals("attr")){
              String makerattr = atts.getValue("name");
              if (makerattr.equals("make")) {
                 this.maker = true;
              }else if(makerattr.equals("listing_count")){
                 this.count = true;
              }
             
           }
         
         
        }
       
        //Called on closing tags like </tag>
        @Override
        public void endElement(String namespaceURI, String localName, String qName) throws SAXException{
         
         
           if(localName.equals("result")){
              this.result = false;
           }else if(localName.equals("item")){
              this.item = false;
             
           }else if(localName.equals("attr")){
              this.attr = false;
           }
         
         
        }
       
        /** Gets be called on the following structure:
          * <tag>characters</tag> */
         @Override
        public void characters(char ch[], int start, int length) {
           
            if(this.item){
             
               peds.setId(new String(ch, start, length));
            }
            if(this.maker) {
              // peds.setMaker(new String(ch, start, length));
            }
            if(this.count) {
               peds.setListCount(new String(ch, start, length));
            }
       
        }

    }



Here is my getter setter method code :

Code: Select all
    public class ParsedDataSet {
        private String result = null;
        private String id=null;
        private String attr=null;
        private String maker=null;
        private String count=null;
       

        public String getResult() {
             return result;
        }
        public void setResult(String result) {
             this.result = result;
        }
        public void setId(String id){
           
           this.id=id;
           System.out.println("Id : " + this.id);
        }
       
        public String getAttr(){
           return attr;
        }
        public void setMaker(String maker){
           this.maker = maker;
           System.out.println("maker : " + this.maker);
        }
        public void setListCount(String count){
           this.count=count;
           
        }
           
       
        public String toString(){
           System.out.println("To string :" + this.maker);
           
           return this.maker;
                       
        }
    }



And im getting the following output
Code: Select all
System.out(): Id :
: INFO/System.out(): Id :
: INFO/System.out(): Id : AC
: INFO/System.out(): maker : AC
: INFO/System.out(): Id :
: INFO/System.out(): maker :
: INFO/System.out(): Id :
: INFO/System.out(): maker :
: INFO/System.out(): Id : 7
: INFO/System.out(): maker : 7
: INFO/System.out(): Id :
: INFO/System.out(): maker :
: INFO/System.out(): Id :
: INFO/System.out(): maker :
: INFO/System.out(): maker :
: INFO/System.out(): maker :
: INFO/System.out(): Id :
: INFO/System.out(): maker :
: INFO/System.out(): Id :
: INFO/System.out(): maker :
: INFO/System.out(): Id : ACURA
: INFO/System.out(): maker : ACURA
: INFO/System.out(): Id :
: INFO/System.out(): maker :
: INFO/System.out(): Id :
: INFO/System.out(): maker :
: INFO/System.out(): Id : 32567
: INFO/System.out(): maker : 32567
: INFO/System.out(): Id :
: INFO/System.out(): maker :
: INFO/System.out(): Id :
: INFO/System.out(): maker :
: INFO/System.out(): maker :
: INFO/System.out(): maker :
: INFO/System.out(): To string :null

Please help me friends .
vaishumol
Experienced Developer
Experienced Developer
 
Posts: 74
Joined: Sun Jan 03, 2010 6:33 am

Top

Re: How to parse these kinda XML using SAX?

Postby blundell » Thu Jul 01, 2010 2:52 pm

Only looked quickly.

Your nearly there!

The quick problem I THINK i may have seen is, your trying to get attribute data (id="YOU WANT THIS") but your looking at the child data id="">YOUR GETTING THIS<

Tired atm I may be completely wrong, but sure you can figure it out ;-)
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: How to parse these kinda XML using SAX?

Postby vaishumol » Thu Jul 01, 2010 4:38 pm

Thanks for your reply :-)
I fixed the issue :-)
vaishumol
Experienced Developer
Experienced Developer
 
Posts: 74
Joined: Sun Jan 03, 2010 6:33 am

Re: How to parse these kinda XML using SAX?

Postby blundell » Thu Jul 01, 2010 6:32 pm

Care to share the result/answer for anyone else finding this thread?
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 18 guests