• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem in using SAX parser

 
Pooja Agarwal
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai All,

I have got a problem in using SAX parser.

My XML looks like this:
-----------------------
<authorizer>
<first-name>HP</first-name>
<last-name>Services</last-name>
<phone>800-22-1984</phone>
</authorizer>

<destination>
<first-name>John</first-name>
<last-name>Doe</last-name>
<company>John Doe Enterprises, Inc.</company>
<department>Manufacturing</department>
<phone>800-555-1234</phone>
<address>
<street-one>1654 Peachtree Str</street-one>
<street-two>Suite Y</street-two>
<city>Atlanta</city>
<province>GA</province>
<country>US</country>
<postal-code>30326</postal-code>
</address>
</destination>

my part of SAX parser code is:
------------------------------

public void startElement (String name, AttributeList attrs)
throws SAXException
{
accumulator.setLength(0);
}

public void characters (char buf [], int offset, int len)
throws SAXException
{
accumulator.append(buf, offset, len);
}

public void endElement (String name)
throws SAXException
{
if (name.equals("first-name") )
{
firstname=accumulator.toString().trim();
}
if (name.equals("last-name"))
{
lastname=accumulator.toString().trim();

}
}


My problem is that i have to store the values of first-name and last-name.
but i have that in both
<authorizer> </authorizer> Tag and
<destination> </destination>
I need to retrive authorizer's firstname,lastname and
destination's firstname and lastname.
what i mean is i need to store authorizerFirstName,authorizerLastName
destinationFirstname and destinationLastname.

Pls let me know how to do that.
Thanks in advance.
Pooja.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the XML and Related Technologies forum.

Please continue this discussion there. Thank you
 
tushar jayawant
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
U can solve this problem using one boolean variable which act as flag

boolean inAuthorizer; // member variable


public void startElement (String name, AttributeList attrs)
throws SAXException
{
accumulator.setLength(0);

if(name.equals("authorizer")) {

inauthorizer=true;

} else if(name.equals("destination")) {

inauthorizer=false;

}

}

//now in end Element

public void endElement (String name)
throws SAXException
{

if (inAuthorizer) {
if (name.equals("first-name") )
{


firstname=accumulator.toString().trim();
}
if (name.equals("last-name"))
{
lastname=accumulator.toString().trim();

}
} else {

if (name.equals("first-name") )
{


destinationFirstname =accumulator.toString().trim();
}
if (name.equals("last-name"))
{
destinationLastname=accumulator.toString().trim();

}



}


}

Regard's
Tushar Kandalgaonkar
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic