This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes problem in using SAX parser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "problem in using SAX parser" Watch "problem in using SAX parser" New topic
Author

problem in using SAX parser

Pooja Agarwal
Ranch Hand

Joined: May 19, 2004
Posts: 37
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

Joined: Aug 26, 2001
Posts: 7610
Moving this to the XML and Related Technologies forum.

Please continue this discussion there. Thank you


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
tushar jayawant
Greenhorn

Joined: Aug 20, 2003
Posts: 19
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem in using SAX parser
 
Similar Threads
problems with JAXP
Problem with SAXParser when using with servlets.
saxParser
SAX parser problem
Retreiving the values of enddocument(SAX Parser) method from mainmethod.