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
226 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now