Solved

SAX parser -- if there is two elements with same name in different place in the xml, getting the first one but next one is missing..

Posted on 2004-08-12
4
231 Views
Last Modified: 2013-11-23
Hi,
I have a xml (part of it) as follows:

  <p:FTTPServedInfo ErrorCode="" ErrorDesc="">

                      <p:FTTPFlag>Y</p:FTTPFlag>

                        <!-- Possible Values are 'Y' , 'N' -->

                      <p:isPackageBundled/>

                        <!-- Possible Values are 'Y' , 'N' -->
  </p:FTTPServedInfo>

  <p:FTTPCapableInfo ErrorCode="" ErrorDesc="">
                         <p:FTTPFlag>N</p:FTTPFlag>
                        <!-- Possible Values are 'Y' , 'N' -->
                        <p:AccessType/>
                      <!-- Possible Values are 'G' , 'C' , 'O' -->
</p:FTTPCapableInfo>


Whe I am parsing (using SAX parser in java )it. I am gettting the first  <p:FTTPFlag>Y</p:FTTPFlag> but, the next one is coming as nothing..


The code snippet is as follows:

else if(lName.equalsIgnoreCase(FTTPFLAG)) { // mCurrentFttpServedInfo is an object
            if (mCurrentFttpServedInfo != null)
            {
                mCurrentFttpServedInfo.setFttpFlag(mCharBuf.toString());
            }
        }

 else if(lName.equalsIgnoreCase(PASSCODE)) {
            if (mCurrentAccount != null)
                    mCurrentAccount.setPassCode(mCharBuf.toString());
        }

else if(lName.equalsIgnoreCase(FTTPFLAG)) {  //// mCurrentFttpCapableInfo is a different object then mCurrentFttpServedInfo as specified earlier
           
 if (mCurrentFttpCapableInfo != null)
            {
                mCurrentFttpCapableInfo.setFttpFlag(mCharBuf.toString());
            }
        }


can u pls help..

Thanks

0
Comment
Question by:phantom2005
  • 2
  • 2
4 Comments
 
LVL 3

Expert Comment

by:kumvjuec
ID: 11786975
Use this, your problem will be solved :)

else if(lName.equalsIgnoreCase(FTTPFLAG))
  {
      if (mCurrentFttpServedInfo != null)
      {
            mCurrentFttpServedInfo.setFttpFlag(mCharBuf.toString());
      }
      else if (mCurrentFttpCapableInfo != null)
      {
            mCurrentFttpCapableInfo.setFttpFlag(mCharBuf.toString());
      }
  }

  else if(lName.equalsIgnoreCase(PASSCODE))
  {
      if (mCurrentAccount != null)
            mCurrentAccount.setPassCode(mCharBuf.toString());
  }
0
 
LVL 19

Accepted Solution

by:
drichards earned 500 total points
ID: 11788640
I don't think that will solve the problem.  You will always be entering the first case as you were before.  mCurrentFttpServedInfo will not be null because you hit that node first.  Then you will always enter the 'mCurrentFttpServedInfo != null' case.

You need to have a state variable that indicates whether you are processing the 'FTTPServedInfo' node or the 'FTTPCapableInfo' node. Assume you have a 'nodeState' variable that is 0 initially, you set it to 1 when processing the 'FTTPServedInfo' node and then to 2 when processing the 'FTTPCapableInfo' node (I assume you have handlers for those as well - if not you'll need to add them). Then do:

else if(lName.equalsIgnoreCase(FTTPFLAG))
  {
     switch (nodeState)
     {
     case 1: // don't need to check mCurrentFttpServedInfo != null because you did that before changing nodeState
          mCurrentFttpServedInfo.setFttpFlag(mCharBuf.toString());
          break;
     case 2:
          mCurrentFttpCapableInfo.setFttpFlag(mCharBuf.toString());
          break;
     }
  }
  else if(lName.equalsIgnoreCase(PASSCODE))
  {
     if (mCurrentAccount != null)
          mCurrentAccount.setPassCode(mCharBuf.toString());
  }


Or you could simply use a counter if your XML doc always has just two nodes in that order.  Before parsing document, set counter to 0.  Then:

else if(lName.equalsIgnoreCase(FTTPFLAG))
  {
     if ((counter == 0) && (mCurrentFttpServedInfo != null))
     {
          mCurrentFttpServedInfo.setFttpFlag(mCharBuf.toString());
          ++counter;
     }
     else if ((counter == 1) && (mCurrentFttpCapableInfo != null))
     {
          mCurrentFttpCapableInfo.setFttpFlag(mCharBuf.toString());
          ++counter;
     }
  }
  else if(lName.equalsIgnoreCase(PASSCODE))
  {
     if (mCurrentAccount != null)
          mCurrentAccount.setPassCode(mCharBuf.toString());
  }
0
 
LVL 3

Expert Comment

by:kumvjuec
ID: 11790878
Actually the prefix in the variable ("current") suggested to me that the variable must be null in case it is not current. However, if the serverinfo tag comes always before the capableinfo tag, then you can use this ...

else if(lName.equalsIgnoreCase(FTTPFLAG))
  {
     if (mCurrentFttpCapableInfo != null)
     {
          mCurrentFttpCapableInfo.setFttpFlag(mCharBuf.toString());
     }
     else if (mCurrentFttpServedInfo != null)
     {
          mCurrentFttpServedInfo.setFttpFlag(mCharBuf.toString());
     }
  }

  else if(lName.equalsIgnoreCase(PASSCODE))
  {
     if (mCurrentAccount != null)
          mCurrentAccount.setPassCode(mCharBuf.toString());
  }
0
 
LVL 19

Expert Comment

by:drichards
ID: 11793054
Yes, that would work, but it leaves you dependent on the order of elements.  If you can guarantee that they are always in the same order, that's great, but it's preferable to code such that it will work no matter what order the elements appear.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
This is about my first experience with programming Arduino.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question