Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Using SAX parser how to identify values for duplicate tag name. (http://www.velocityreviews.com/forums/t621777-using-sax-parser-how-to-identify-values-for-duplicate-tag-name.html)

Sanjeev 06-23-2008 10:09 AM

Using SAX parser how to identify values for duplicate tag name.
 
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


All times are GMT. The time now is 04:41 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.