Results 1 to 2 of 2
  1. #1
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,058
    Rep Power
    5

    Default Determining which choice exists

    I'm using XPath to read a document. One of my nodes contains a choice element with minOccurs="0". The tag can be PushState, PopState, or SetState.

    I've been calling XPath.evaluate(...) for each of those possibilities and testing if the result is null. Is there a more straightforward way to find out which tag, if any, is present?

    Java Code:
    Node transitionNode = (Node) xp.evaluate("PushState", node, XPathConstants.NODE);
    		if(transitionNode != null) {
    			transitionType = TransitionType.PUSH;
    			transitionState = transitionNode.getTextContent();
    		}
    		else {
    			transitionNode = (Node) xp.evaluate("PopState", node, XPathConstants.NODE);
    			if(transitionNode != null) {
    				transitionType = TransitionType.POP;
    				transitionState = null;
    			}
    			else {
    				transitionNode = (Node) xp.evaluate("SetState", node, XPathConstants.NODE);
    				if(transitionNode != null) {
    					transitionType = TransitionType.SET;
    					transitionState = transitionNode.getTextContent();
    				}
    			}
    		}
    Get in the habit of using standard Java naming conventions!

  2. #2
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: Determining which choice exists

    Quote Originally Posted by kjkrum View Post
    I'm using XPath to read a document. One of my nodes contains a choice element with minOccurs="0". The tag can be PushState, PopState, or SetState.

    I've been calling XPath.evaluate(...) for each of those possibilities and testing if the result is null. Is there a more straightforward way to find out which tag, if any, is present?

    Java Code:
    Node transitionNode = (Node) xp.evaluate("PushState", node, XPathConstants.NODE);
    		if(transitionNode != null) {
    			transitionType = TransitionType.PUSH;
    			transitionState = transitionNode.getTextContent();
    		}
    		else {
    			transitionNode = (Node) xp.evaluate("PopState", node, XPathConstants.NODE);
    			if(transitionNode != null) {
    				transitionType = TransitionType.POP;
    				transitionState = null;
    			}
    			else {
    				transitionNode = (Node) xp.evaluate("SetState", node, XPathConstants.NODE);
    				if(transitionNode != null) {
    					transitionType = TransitionType.SET;
    					transitionState = transitionNode.getTextContent();
    				}
    			}
    		}
    It depends on the schema and how much you can assume about the node's position and attributes, etc. If it is guaranteed to be the first or last child node, you could use a predicate, for example. If its position is complex, could you search for a signature attribute if there is a unique one. Either way, you could then retrieve and test the node's name. But if has no identifying signature or position, I guess you'd have to enumerate them by name, as you have done.

Similar Threads

  1. Determining whether a queen can attack another piece
    By sonofshirt in forum New To Java
    Replies: 9
    Last Post: 10-09-2010, 02:23 AM
  2. Determining ActionListener
    By siamino in forum New To Java
    Replies: 12
    Last Post: 05-25-2009, 11:04 PM
  3. Need Help with determining the problem in my Code
    By KillingKiller in forum New To Java
    Replies: 3
    Last Post: 03-27-2009, 12:24 AM
  4. Determining the readiness of another program
    By jmHoekst in forum New To Java
    Replies: 10
    Last Post: 06-19-2008, 08:56 AM
  5. Determining Midi Length
    By Usagi in forum New To Java
    Replies: 0
    Last Post: 12-08-2007, 11:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •