【问题标题】:Find XML element with specific value with jQuery使用 jQuery 查找具有特定值的 XML 元素
【发布时间】:2017-10-12 05:14:33
【问题描述】:

我有一些具有以下结构的 XML:

<pressureVessel>
  <closure1>
    <topClosure>
      <ellipsoidalHead>
        <standardComponentData>
          <identifier>Ellipsoidal Head #1</identifier>
          <idNumber>1111</idNumber>
          ...
        </standardComponentData>
      </ellipsoidalHead>
    </topClosure>
  </closure1>
  <closure2>
    <bottomClosure>
      <ellipsoidalHead>
        <standardComponentData>
          <identifier>Ellipsoidal Head #2</identifier>
          <idNumber>2222</idNumber>
          ...
        </standardComponentData>
      </ellipsoidalHead>
    </topClosure>
  </closure1>
  <shell>
    <cylinder>
      <standardComponentData>
        <identifier>Cylinder #1</identifier>
        <idNumber>3333</idNumber>
        ...
      </standardComponentData>
      ....
     </cylinder>
    <cylinder>
      <standardComponentData>
        <identifier>Cylinder #2</identifier>
        <idNumber>4444</idNumber>
        ...
      </standardComponentData>
      ....
     </cylinder>
    <cylinder>
      <standardComponentData>
        <identifier>Cylinder #3</identifier>
        <idNumber>5555</idNumber>
        ...
      </standardComponentData>
      ....
    </cylinder>
    <flange>
      <standardComponentData>
        <identifier>Top Head Flange #1</identifier>
        <idNumber>6666</idNumber>
        <attachedToidNumber>1111</attachedToidNumber>
        ...
      </standardComponentData>
    </flange>
  </shell>
<pressureVessel>

我在解析这个 XML 的一些 javascript 中,我将法兰元素作为一个名为 bomNode 的变量。我需要做的是在&lt;flange&gt; 元素中找到&lt;idNumber&gt; 值与&gt;attachedToidNumber&gt; 匹配的元素(在本例中为Ellipsoidal Head #1)。 &lt;pressureVessel&gt; 中还有多个其他元素,所以我需要做的是

  1. 查找以closure 开头的任何元素(例如closure1、closure2 等)。
  2. 在该元素中,找到一个具有&lt;standardComponentData&gt; 的子元素,然后找到一个与&lt;attachedToidNumber&gt; 匹配的&lt;idNumber&gt; 值(我当时已将其存储在一个变量中)。
  3. 获取该元素,以便我可以从中获取其他数据。

那么最有效的方法是什么? &lt;pressureVessel&gt;下面还有其他元素,所以我不想全部解析。

我的想法是做类似的事情

var elName = 'closure';
bomNode.parent().parent().find().has(elName).each(function(index() {
  // Do something here
})

但我不知道从这里去哪里,或者我是否走在正确的轨道上。

【问题讨论】:

    标签: javascript jquery xml


    【解决方案1】:

    您可以使用.filter() 来匹配&lt;idNumber&gt; 元素,其中.textContent 等于&lt;attachedToIdNumber&gt; .textContent

    $(xml).find("standardComponentData idNumber").filter(function(i, el) {
      return $(el).parents().filter(function(i, el) { 
               return /closure/i.test(el.tagName)  }).is("*") 
             && el.textContent === xml.find("attachedToidNumber").text()
    })
    

    【讨论】:

      【解决方案2】:

      根据我对您的用例的最佳理解和稍微更完整(但仍在猜测)的 XML 文件,我对其进行了拍摄。

      1. 我创建了一个以closure 开头的所有可用节点的数组。
      2. 从那里,用包含&lt;standardComponentData&gt; 节点的任何元素填充另一个数组(称为attachableComponents)。
      3. 然后,需要创建一个函数来查找这些元素中的任何一个。
      4. &lt;flang&gt;&lt;attachedToidNumber&gt; 的值传递给该函数会从该attachableComponents 数组中提取任何匹配的节点。

      将其呈现出来以供视觉参考的示例 JS Bin:

      http://jsbin.com/hovovel/1/edit?html,js,output

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-06
        • 2016-11-13
        • 2013-02-13
        • 1970-01-01
        • 2013-02-04
        • 1970-01-01
        • 1970-01-01
        • 2010-12-02
        相关资源
        最近更新 更多