2

I have an xml structure like below

<cr:TRFCoraxData instrumentId="8590925624" organizationId="4296241518">
    <cr:Dividends>
        <cr:ExDate>2017-02-27T00:00:00+00:00</cr:ExDate>
        <cr:PeriodEndDate>2017-03-31T00:00:00+00:00</cr:PeriodEndDate>
        <cr:PeriodDuration>P3M</cr:PeriodDuration>
    </cr:Dividends>


    <cr:AdjustmentFactors>
        <cr:ExDate>2222-05-21T00:00:00+00:00</cr:ExDate>
        <cr:AdjustmentFactor>0.50000</cr:AdjustmentFactor>
    </cr:AdjustmentFactors>

    </cr:TRFCoraxData>

So i have to element cr:ExDate with same name in Kand AdjustmentFactors tag.

Now i have pojo classes for both and then i have start and end element tag .

In my end element tag i have below condition like below

 if (element.equals("cr:ExDate")) {
            dividend.setExDate(tmpValue);
        }else if (element.equals("cr:DividendEventId")) {
            dividend.setDividendEventId(tmpValue);
        }else if (element.equals("cr:AnnouncementDate")) {
            dividend.setAnnouncementDate(tmpValue);
        }
else if (element.equals("cr:ExDate")) {
            adjustmentFactorObj.setExDate(tmpValue);
        }else if (element.equals("cr:AdjustmentFactor")) {
            adjustmentFactorObj.setAdjustmentFactor(tmpValue);
        }

Clearly for "cr:ExDate" element if condition satisfies and i am not able to get and set in adjustmentFactorObj for "cr:ExDate" value.

Please suggest me how can i solve this problem

2 Answers2

1

You can try something like this:

boolean isInDivident;  // it is a field

...

if (element.equals("cr:Dividends")) {
  isInDivident = true;
} else if ((element.equals("cr:AdjustmentFactors")) {
  isDivident = false;
} else if (element.equals("cr:ExDate") && isInDivident)  {
...
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
0

Writing SAX applications is not easy. Many people are lured into it by the promise of better performance, but you will only get better performance if you are highly skilled in the art. Most people who end up asking questions about SAX on SO probably should be using a different tool for the job. (But you haven't described the task, so we can't judge that.)

In any case, when you use SAX, it's your responsibility to keep track of context. For most situations, a stack holding element names is sufficient: push an element name to the stack in the startElement event, pop it in the endElement event. That's sufficient in this case to determine, when an ExDate arrives, what its parent element is.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164