1

Currently I have XML in the following format:

 -<collection>
     -<beanRepresentation>
       <beanRepName>1</beanRepName>
       -<group>
        </group>
       -<relationships>
         <inputBeanId>1</inputBeanId>
         <outputBeanId>2</outputBeanId>
        </relationships>
       -<relationships>
         <inputBeanId>1</inputBeanId>
         <outputBeanId>3</outputBeanId>
        </relationships>
     </beanRepresentation>

     </beanRepresentation>
     <beanRepresentation>

   <collection>

I want to loop through each <beanRepresentation>'s and get the <outputBeanId>. Right now my code works when there is ONLY ONE <relationships>, but the above XML, has two <relationships>'s. and I need to go in and get BOTH of the <outputBeanId>'s so I can put them into my function connectPort().

$(window).load(function(){  
        var $xml = $(xmlDoc);
        $xml.find('beanRepresentation').has('outputBeanId').has('inputBeanId').each(function () {
        var $br = $(this);
        connectPort($br.find('beanRepName').text(), $br.find('outputBeanId').text());
        })
    });

This only works when is there only one <relationships>, how do I add a loop in here so I can get N amount of <relationships> to work.

2
  • There is no beanRepId in your XML but your try to find it in the jquery code.. Commented Jan 30, 2014 at 20:50
  • sorry fixed. mistyped the name Commented Jan 30, 2014 at 20:51

2 Answers 2

1

You will have to make another loop for each relationship

$(window).load(function(){  
    var $xml = $(xmlDoc);
    $xml.find('beanRepresentation').each(function () {
       var $br = $(this),
           relations = $br.find('relationships').has('outputBeanId').has('inputBeanId'),
           beanName = $br.find('beanRepName').text();

       relations.each(function(){
          var outputId = $(this).find('outputBeanId').text();
          connectPort(beanName , outputId);
       });
    })
});
Sign up to request clarification or add additional context in comments.

Comments

1

You can do something like below:

$xml.find('beanRepresentation').each(function() {
      $(this).find('relationships').has('outputBeanId').has('inputBeanId').each(function(){
      // Blah Blah
      });
});

Hope this Helps!!

1 Comment

$(window).load(function(){ var $xml = $(xmlDoc); $xml.find('beanRepresentation').each(function () { $(this).find('relationships').has('outputBeanId').has('inputBeanId').each(function(){ var $br = $(this); connectPort($br.find('beanRepName').text(), $br.find('outputBeanId').text()); }); }); }); not working for some reason.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.