Using SAX parser how to identify values for duplicate tag name.

Discussion in 'Java' started by Sanjeev, Jun 23, 2008.

  1. Sanjeev

    Sanjeev Guest

    Hello Gurus,

    I am using SAX parser for reading XML file.
    Below is the code snippets.

    >>Student.xml File

    <?xml version="1.0" encoding="UTF-8"?>
    <root>
    <student>
    <name>Sanjeev Atvankar</name>
    <class>Fourth Year</class>
    <subject>
    <subjectType>Science</subjectType>
    <subjectValue>Anatomy</subjectValue>
    </subject>
    <subject>
    <subjectType>Language</subjectType>
    <subjectValue>Hindi</subjectValue>
    </subject>
    </student>
    <student>
    . . . .
    . . . .
    </student>
    </root>

    >> StudentVO.java (Java Bean) with following parameters

    private String name;
    private String classRoom;
    private String scienceSubject;
    private String languageSubject;

    >> StudentParser.java

    . . . .
    . . . .
    public StudentParser(){
    studentCollectionVO = new StudentCollectionVO();
    }
    public StudentCollectionVO runExample(String xmlMessage) {
    parseDocument(xmlMessage);
    return studentCollectionVO;
    }
    private void parseDocument(String xmlMessage) {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    try {
    SAXParser sp = spf.newSAXParser();
    sp.parse(new InputSource(new
    ByteArrayInputStream(xmlMessage.getBytes())), this);
    }catch(Exception e) {
    }
    }
    public void startElement(String uri, String localName, String qName,
    Attributes attributes) throws SAXException {
    tempVal = "";
    if(qName.equalsIgnoreCase("student")) {
    studentVO = new StudentVO();
    }
    }
    public void characters(char[] ch, int start, int length)
    throws SAXException {
    tempVal = new String(ch,start,length);
    }
    public void endElement(String uri, String localName, String qName)
    throws SAXException {
    if(qName.equalsIgnoreCase("student")) {
    studentCollectionVO.add(studentVO);
    }else if (qName.equalsIgnoreCase("name")) {
    studentVO.setName(tempVal);
    }else if (qName.equalsIgnoreCase("class")) {
    studentVO.setClassRoom(tempVal);
    }else if (qName.equalsIgnoreCase("subjectValue")) {
    studentVO.setScienceSubject(tempVal);
    }else if (qName.equalsIgnoreCase("subjectValue")) {
    studentVO.setLanguageSubject(tempVal);
    }
    }
    . . . .
    . . . .

    Since each subject is given in following tag format
    <subject>
    <subjectType></subjectType>
    <subjectValue></subjectValue>
    </subject>

    how can identify individual subject.

    In above example Anatomy belongs to Science(subjectType) and
    Hindi belongs to Language(subjectType).

    Can anybody help me.

    Thanking in advance
    Sanjeev
    Sanjeev, Jun 23, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mladen Adamovic
    Replies:
    0
    Views:
    714
    Mladen Adamovic
    Jan 14, 2005
  2. shruds
    Replies:
    1
    Views:
    758
    John C. Bollinger
    Jan 27, 2006
  3. Replies:
    2
    Views:
    411
    Joe Kesselman
    Sep 25, 2006
  4. Replies:
    2
    Views:
    934
    Joseph Kesselman
    Nov 5, 2007
  5. Sanjeev
    Replies:
    9
    Views:
    703
Loading...

Share This Page