struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Navjot Singh" <navjo...@net4india.net>
Subject RE: urgent please -- XML document reading/parsing through SAX for the given snippet.
Date Mon, 23 Feb 2004 11:13:55 GMT
create a hashmap object and while you run SAX thru your xml document.

is yourStartElement(), save the tag name currentElement variable.
in characters(), save the characters value to your hash with key as
currentElement.

in the end, your element will filled.
if you multiple such case tags. add them into vector one by one.

but why don't you use digester to traverse your document? any reasons.

hth


>-----Original Message-----
>From: ssrinivasrao@worldbank.org [mailto:ssrinivasrao@worldbank.org]
>Sent: Monday, February 23, 2004 3:37 PM
>To: Struts-User@Jakarta.Apache.Org
>Subject: urgent please -- XML document reading/parsing through SAX for
>the given snippet.
>
>
>Hi,
>
>I need to parse an xml document (xml file) using SAX API.
>
>A portion of the .xml file looks as follows (for displaying 1 row of
>information).
>
><case id="RMA-1234">
>      <assigned-to>Abrao I. Grynglas</assigned-to>
>      <subject>This is the subject of email</subject>
>      <referred-to>Yes</referred-to>
>      <status>Document Open</status>
>      <date>16-Sep-03</date>
>      <priority>High</priority>
></case>
>
>I use the following java source code to do the same.
>*************************************
>class MyHandler extends DefaultHandler
>{
>      private Set Cases = new TreeSet();
>      private String strCase = "";
>      public MyHandler(){}
>      public void startElement(String namespaceURI,String localname,String
>rawname,Attributes atts) throws SAXException
>      {
>            if (localname.equals("case"))
>            {
>                  for (int i = 0; i < atts.getLength(); i++)
>                  {
>                        String attName = atts.getLocalName(i);
>                        if (attName.equals("id"))
>                        strCase = atts.getValue(i);
>                  }
>            }
>      }
>      public void endElement(String namespaceURI,String localname,String
>rawname) throws SAXException
>      {
>            if (localname.equals("case"))
>            {
>                  String thisCase = strCase.toString().trim();
>                  Cases.add(thisCase);
>            }
>      }
>      public void characters(char [] buf,int offset,int len){
>            String partOfCaseName = new String(buf, offset, len);
>      }
>      public Set getCases(){
>            return Cases;
>      }
>}
>*************************************
>
>So the above code returns only the attribute values from the first
>main element
>"case".  I would also like to retrieve (using SAX) the values of
>simple elements
>also.  Can anyone guide me how to achieve this, please ?
>
>Thanks and Regards
>S Srikanth
>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: struts-user-help@jakarta.apache.org
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Mime
View raw message