我正在解析XML並希望以易於閱讀的方式存儲值。js從JSP解析XML,存儲值
我得到mapShapes和mapPoints(所以x和y座標)。一個(x,y)對形成一個點,一個點的集合形成一個形狀。
下面是一些示例XML,我想解析:
<?xml version='1.0'?>
<mapQuery id='10918014'>
<mapShape id='429436'>
<mapPoint id='4259799'>
<x>-81.61508</x>
<y>41.52184</y>
</mapPoint>
<mapPoint id='4259800'>
<x>-81.61537</x>
<y>41.52181</y>
</mapPoint>
<mapPoint id='4259801'>
<x>-81.61538</x>
<y>41.522198</y>
</mapPoint>
<mapPoint id='4259802'>
<x>-81.61516</x>
<y>41.52222</y>
</mapPoint>
<mapPoint id='4259803'>
<x>-81.61508</x>
<y>41.52184</y>
</mapPoint>
</mapShape>
</mapQuery>
我想結束與像
shapes[0].point[0].x[0] = first x point
shapes[0].point[0].y[0] = first y point
的陣列(該例子中的XML僅具有本一種形狀,但可能有幾個)。
在此先感謝您的幫助,並提出了一個簡單的問題=)
下面是一些骨架代碼:
shapeXmlHandler : function(xml){
$(xml).find("mapShape").each(function(){
$(this).find("mapPoint").each(function() {
console.log('mapPoint: '+$(this).attr('id'));
console.log('x :'+$(this).find("x").text());
console.log('y :'+$(this).find("y").text());
});
});
}
你有什麼問題?看起來不錯(除了我認爲你實際上想'形狀[0] .point [0] .x = first x point'等等) – 2011-06-08 18:59:10
好的呼叫。我不確定如何完成目標,但我非常接近。我真的不知道什麼是最好的方法是將xml數據放入數組中,因此我一直在尋求建議 – sova 2011-06-08 19:02:49