• 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

problem in using SAX parser

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving this to the XML and Related Technologies forum.

Please continue this discussion there. Thank you
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Put the moon back where you found it! We need it for tides and poetry and stuff. Like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic