Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Confusion with startElement,endElement and characters method

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am relatively new to Java. I am first time using this SAX parser to parse an XML file in my java application.Below is my handler code -

---------------------------------------------------------------------
<code>
package Sample;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.util.*;


public class SAXHandler extends DefaultHandler {

String a1 ;
String a2 ;
String a3;
String a4;

public void characters(char[] ch, int start, int length)
throws SAXException {

try {
a2 = new String(ch,start,length);
}
catch(Exception e) {
System.err.println(e);
}

}

public void startElement(String namespaceURI, String localName,
String qualifiedName, Attributes atts) {
if(qualifiedName.equalsIgnoreCase("Employee"))
{
a1 = atts.getValue("type");
//a2= atts.getValue("name");
System.out.println("Type = " + a1);
//System.out.println("string 2 = " + a2);
}
if (qualifiedName.equalsIgnoreCase("Name")){
System.out.println("name = " + a2);
}
if (qualifiedName.equalsIgnoreCase("Id")){
System.out.println("Id = " + a2);
}
if (qualifiedName.equalsIgnoreCase("Age")){
System.out.println("age = " + a2);
}
}



/*public void endElement(String namespaceURI, String localName,
String qualifiedName) {
try{
if (qualifiedName.equalsIgnoreCase("Name")){
System.out.println("name = " + a2);
}
if (qualifiedName.equalsIgnoreCase("Id")){
System.out.println("Id = " + a2);
}
if (qualifiedName.equalsIgnoreCase("Age")){
System.out.println("age = " + a2);
}

}
catch(Exception e) {
System.err.println(e);
}
}*/

/**
* @param args
*/
/*public static void main(String[] args) {
System.out.println("in main function");
// TODO Auto-generated method stub

}*/

}
</code>
------------------------------------------------------------------------

When I try to get the values of Id,Age and Name in the startElement() method,it prints null values.But when I create a endElement() and get the values in that method,it correctly prints the values.Please can someone explain what is the problem with my code and what is the correct way to use these 3 methods?
 
Marshal
Posts: 28271
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First startElement is called (when the parser sees the start-element tag). It doesn't set the value of variable a2. Next characters is called (when the parser sees the text inside the element). It sets the value of variable a2. Next endElement is called (when the parser sees the end-element tag). Now there is a value for a2 which makes sense.
 
abhinav sinha
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for your reply.This is my employees.xml file -

-----------------------------------------------------------------------
<Personnel>

<Employee type="permanent">
<Name>Seagull</Name>
<Id>3674</Id>
<Age>34</Age>
</Employee>

<Employee type="contract">
<Name>Robin</Name>
<Id>3675</Id>
<Age>25</Age>
</Employee>

<Employee type="permanent">
<Name>Crow</Name>
<Id>3676</Id>
<Age>28</Age>
</Employee>
</Personnel>

________________________________________________________________________

Even if i dont use the endElement method,a am able to get and print the "type" of the employee.According to you,even that should be printed because I dont have and endElement method.Then why only Id,Name and Age are not getting printed? Please explain.
 
Paul Clapham
Marshal
Posts: 28271
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by abhinav sinha:
According to you,even that should be printed because I dont have and endElement method.

I don't see where I mentioned that at all. I only commented about the value of the "a2" variable, which you were only using to get character data. Not attributes.

And please don't post commented-out code. Not only is it a waste of space, it's also very misleading.
 
abhinav sinha
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,

I am pasting my code below again(removing the commented code) -

_________________________________________________________________________
<code>

package Sample;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.util.*;


public class SAXHandler extends DefaultHandler {

String a1 ;
String a2 ;
String a3;
String a4;

public void characters(char[] ch, int start, int length)
throws SAXException {

try {
a2 = new String(ch,start,length);
}
catch(Exception e) {
System.err.println(e);
}

}

public void startElement(String namespaceURI, String localName,
String qualifiedName, Attributes atts) {
if(qualifiedName.equalsIgnoreCase("Employee"))
{
a1 = atts.getValue("type");
System.out.println("Type = " + a1);
if (qualifiedName.equalsIgnoreCase("Name")){
System.out.println("name = " + a2);
}
if (qualifiedName.equalsIgnoreCase("Id")){
System.out.println("Id = " + a2);
}
if (qualifiedName.equalsIgnoreCase("Age")){
System.out.println("age = " + a2);
}
}

}



* @param args
*/
/*public static void main(String[] args) {
System.out.println("in main function");
// TODO Auto-generated method stub

}*/

}

</code>
________________________________________________________________________

The "type" of the employee is getting printed even without the endElement method but why is not the Id,Name and Age not getting populated and printed?Please explain.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

why is not the Id,Name and Age not getting populated and printed?Please explain.



Your characters() code will be called for every Element text content, probably spaces, tabs or line end from earlier Elements will be the content of a2 when you hit the startElement() for the Name element. You want to capture characters() AFTER the Name startElement, not before.

Bill
 
There are no more "hours", it's centi-days. They say it's better, but this tiny ad says it's stupid:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic