Results 1 to 5 of 5
- 09-21-2009, 10:13 PM #1
Member
- Join Date
- Jun 2008
- Posts
- 85
- Rep Power
- 0
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
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.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>
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"); } }But what i actually want is when the tag name is mol i want its type alone.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); } }
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
- 09-22-2009, 06:49 AM #2
Member
- Join Date
- Jun 2008
- Posts
- 56
- Rep Power
- 0
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.
- 09-22-2009, 04:11 PM #3
Member
- Join Date
- Jun 2008
- Posts
- 85
- Rep Power
- 0
Thanks for pointing out.I will do that..that didnt answer my question
- 09-22-2009, 09:32 PM #4
Member
- Join Date
- Mar 2008
- Posts
- 7
- Rep Power
- 0
do you have to use SAX
SAX is generally considered too difficult to use...
have you tried DOM or VTD-XML ?
-
Similar Threads
-
Suggestion: Forum for possible projects
By Singing Boyo in forum Suggestions & FeedbackReplies: 0Last Post: 03-23-2009, 05:10 PM -
suggestion: vbcode & protocols
By angryboy in forum Suggestions & FeedbackReplies: 5Last Post: 01-28-2009, 04:43 PM -
THESIS PROPOSAL suggestion
By ashin in forum New To JavaReplies: 9Last Post: 11-03-2008, 01:26 PM -
Suggestion needed
By java_newbie in forum NetBeansReplies: 5Last Post: 10-22-2008, 12:45 PM -
dynamic auto suggestion
By freddieMaize in forum Advanced JavaReplies: 3Last Post: 07-31-2008, 03:20 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks