【发布时间】: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 的变量。我需要做的是在<flange> 元素中找到<idNumber> 值与>attachedToidNumber> 匹配的元素(在本例中为Ellipsoidal Head #1)。 <pressureVessel> 中还有多个其他元素,所以我需要做的是
- 查找以
closure开头的任何元素(例如closure1、closure2 等)。 - 在该元素中,找到一个具有
<standardComponentData>的子元素,然后找到一个与<attachedToidNumber>匹配的<idNumber>值(我当时已将其存储在一个变量中)。 - 获取该元素,以便我可以从中获取其他数据。
那么最有效的方法是什么? <pressureVessel>下面还有其他元素,所以我不想全部解析。
我的想法是做类似的事情
var elName = 'closure';
bomNode.parent().parent().find().has(elName).each(function(index() {
// Do something here
})
但我不知道从这里去哪里,或者我是否走在正确的轨道上。
【问题讨论】:
标签: javascript jquery xml