简体   繁体   English

使用jQuery遍历XML以获取元素值

[英]Traverse XML with jQuery to get element value

I have some XML that looks like so: 我有一些看起来像这样的XML:

<closure1>
  <topClosure>
    <ellipsoidalHead>
      <standardComponentData>
        <variousElements>
        <idNumber>234567</idNumber>
        <nominalThickness units="in">0.3750</nominalThickness>
      </standardComponentData>
    </ellipsoidalHead>
  </topClosure>
</closure1>
<shell>
  <standardComponentData>
    <various_elements>
    <nominalThickness units="in">0.6250</nominalThickness>
    <idNumber>123456</idNumber>
  </standardComponentData>
</shell>
<nozzle>
  <standardComponentData>
    <various_elements>
    <attachedToidNumber>123456</attachedToidNumber>
  </standardComponentData>
<nozzle>

In my JS code, I already have the <nozzle> element bomNode as a jQuery set, ie 在我的JS代码中,我已经将<nozzle>元素bomNode作为jQuery集,即

var bomNode = $("nozzle");

So, for each nozzle element, I need to 因此,对于每个喷嘴元件,我需要

  1. Get the value of <attachedToidNumber> in the <nozzle> element. 获取<nozzle>元素中的<attachedToidNumber>的值。

  2. Find the element that contains the <idNumber> that matches <attachedToidNumber> ( <shell> in this case). 查找包含与<attachedToidNumber> (在本例中为<shell> )匹配的<idNumber>的元素。

  3. Get the value in the <nominalThickess> element. <nominalThickess>元素中获取值。

As you can see, the depth of the desired <idNumber> element can vary. 如您所见,所需的<idNumber>元素的深度可以变化。 This is also a very small subset of the whole XML structure, so it can be very large. 这也是整个XML结构的很小子集,因此它可能很大。

I've tried something like this: 我已经尝试过这样的事情:

var attachedToElement = bomNode.parents().find('idNumber').text() === attachedToId;

but I get false returned. 但我得到了false回报。 What's the easiest way to get the desired idNumber value? 获得所需的idNumber值的最简单方法是什么? I'm sure it's something simple, but I'm just missing it. 我敢肯定这很简单,但是我只是想念它。

Thanks. 谢谢。

UPDATE: I realized that bomNode is at the top level, I don't need to go up. 更新:我意识到bomNode在最高级别,我不需要上去。 a level. 一个等级。 Doing something like this 做这样的事情

var attachedToElement = bomNode.parents().siblings().find('idNumber')

gives me a list of children elements that have an <idNumber> element. 给我一个具有<idNumber>元素的子元素列表。 So, I need to find the one that has the desired value. 因此,我需要找到具有所需值的那个。 My thought is to use .each() . 我的想法是使用.each() However, that value is defined outside of the .each() function, so I don't have anything to match against. 但是,该值是在.each()函数外部定义的,因此我没有任何匹配的对象。 Once I have the list of matches, what's the easiest way to get the set that has the <idNumber> value I want? 获得匹配列表后,最简单的方法就是获取具有所需的<idNumber>值的集合?

You were right - you missed a simple thing: 您是对的-您错过了一件简单的事情:

shell is not a parent of nozzle . shell不是nozzle的父级。 They are siblings . 他们是siblings Try this: 尝试这个:

var attachedToElement = bomNode.siblings().find('idNumber').text() === attachedToId; 

But this would return true (if true) - not the actual value. 但这将返回true(如果为true)-而不是实际值。

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

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