简体   繁体   English

XML解析遍历子级别

[英]XML parsing loop through sublevels

Currently I have XML in the following format: 目前,我具有以下格式的XML:

 -<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> . 我想遍历每个<beanRepresentation>并获取<outputBeanId> Right now my code works when there is ONLY ONE <relationships> , but the above XML, has two <relationships> 's. 现在,当只有一个<relationships> ,我的代码可以工作,但是上述XML具有两个<relationships> and I need to go in and get BOTH of the <outputBeanId> 's so I can put them into my function connectPort() . 我需要进入<outputBeanId>的两个域,以便将它们放入我的函数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. 这仅在只有一个<relationships>时起作用,如何在此处添加循环,这样才能使N数量的<relationships>起作用。

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!! 希望这可以帮助!!

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);
       });
    })
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM