Results 1 to 5 of 5
  1. #1
    Ms.Ranjan is offline Member
    Join Date
    Jun 2008
    Posts
    85
    Rep Power
    0

    Default Need suggestion in parsing xml using sax

    Hi I am using sax parser to parse an xml file in java.Here is the xml file
    sample.xml
    Java Code:
    <?xml version="1.0"?>   
    <Name_XML>   
    <Created>2008_11_07 11:41::53</Created>   
    <ontology>   
    <LabelType name="edge-type" id="3">   
          <LabelValueList>   
            <LabelValue name="agent" id="22" parent_idref="19" />   
          </LabelValueList>   
    </LabelType>   
    </ontology>   
    <Model>   
        <Mole>   
          <Mol type="compound" id="26734">   
            <Name type="EC" long_name="Consortium" value="2.7.1.112" />   
            <Name type="LL" long_name="EzGeny" value="2241" />   
            <Name type="OF" long_name="symbol" value="some" />   
          </Mol>   
        </Mole>    
    </Model>   
    </Name_XML>
    I parsed the code and i could get all its element name and its attribute values.But I am interested in getting the details of the Mol and Name tags.Here is the code that I am using it do it.
    view plaincopy to clipboardprint?
    Java Code:
    import javax.xml.parsers.SAXParser;   
    import javax.xml.parsers.SAXParserFactory;   
    import org.xml.sax.XMLReader;   
    public class psaxparser {   
        public static void main(String args[])throws Exception   
        {   
        //create Handler   
        OutHandler handler=new OutHandler();   
      
        //create parser   
        SAXParserFactory spf=SAXParserFactory.newInstance();   
        XMLReader parser=null;   
        SAXParser saxParser=spf.newSAXParser();   
        parser=saxParser.getXMLReader();   
      
        //assign the handler to the parser   
        parser.setContentHandler(handler);   
      
        //parse the document   
        parser.parse("sample.xml");   
      
    }   
    }
    Java Code:
    import org.xml.sax.Attributes;   
    import org.xml.sax.Locator;   
    import org.xml.sax.SAXException;   
    import org.xml.sax.helpers.DefaultHandler;   
      
      
    public class OutHandler extends DefaultHandler {//SAX call this method when it encounters an element   
        public void startElement(String strNamespaceURI,String strLocalName,String strQName,Attributes al)throws SAXException    
        {   
               
               
        if(strQName=="Mol" || strQName=="Name" )   
        {   
            System.out.println("startElement:" +strQName);   
            String mainval=al.getValue("name_type");           
            for(int i=0;i<al.getLength();i++)   
                {                          
                String attname=al.getQName(i);     
                System.out.println("attname: "+attname+"   attvalue: "+al.getValue(i));            
            }   
        }   
        System.out.println("---------------------------------------------------------");   
        }   
        //SAX calls this method to pass in character data stored in between the start and end tags   
        public void characters(char[] a,int s,int l)throws SAXException   
        {   
        //System.out.println("characters:"+new String(a,s,l));   
        }   
      
        //SAX calls this method when the end-tag for an element is encountered   
        public void endElement(String strNamespaceURI,String strLocalName,String strQName)throws SAXException   
        {   
           
        //System.out.println("endElement:/" +strQName);   
        }   
    }
    But what i actually want is when the tag name is mol i want its type alone.
    then if the type is "compound" then i want only the attributes long_name="EzGeny" and value="2241" and dont want the rest.
    How do i got about doing this,any suggestions please

  2. #2
    devunion is offline Member
    Join Date
    Jun 2008
    Posts
    56
    Rep Power
    0

    Default

    NEVER compare strings with == operator. You always should use equals.
    PHP Code:
    "Mol".eqals(strQName) || "Name".equals(strQName)
    
    // SOMETIMES NOT THE SAME TO 
    strQName.eqals("Mol") || strQName.equals("Name")
    Try Controls4J - Advanced Swing Components.

  3. #3
    Ms.Ranjan is offline Member
    Join Date
    Jun 2008
    Posts
    85
    Rep Power
    0

    Default

    Thanks for pointing out.I will do that..that didnt answer my question

  4. #4
    Jimmy Zhang is offline Member
    Join Date
    Mar 2008
    Posts
    7
    Rep Power
    0

    Default do you have to use SAX

    SAX is generally considered too difficult to use...
    have you tried DOM or VTD-XML ?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Jimmy Zhang View Post
    SAX is generally considered too difficult to use...
    have you tried DOM or VTD-XML ?
    Can you back this up? So you suggest using DOM to parse a very large XML file, one that may cause the JVM to run out of memory?

Similar Threads

  1. Suggestion: Forum for possible projects
    By Singing Boyo in forum Suggestions & Feedback
    Replies: 0
    Last Post: 03-23-2009, 06:10 PM
  2. suggestion: vbcode & protocols
    By angryboy in forum Suggestions & Feedback
    Replies: 5
    Last Post: 01-28-2009, 05:43 PM
  3. THESIS PROPOSAL suggestion
    By ashin in forum New To Java
    Replies: 9
    Last Post: 11-03-2008, 02:26 PM
  4. Suggestion needed
    By java_newbie in forum NetBeans
    Replies: 5
    Last Post: 10-22-2008, 01:45 PM
  5. dynamic auto suggestion
    By freddieMaize in forum Advanced Java
    Replies: 3
    Last Post: 07-31-2008, 04:20 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
  •