1
我有一個像下面的XML我想解析使用jquery,並得到像我寫下面的Java腳本來解析它。我已經嘗試了下面的代碼,但我能夠得到的位置不是採用以下格式,如我所料。使用jquery解析XML節點元素
預期結果:
S1:E1:https://location1
S2:E2:https://location1
XML:
<Country>
<State>
<id>S1</id>
<City>
<E1>
<location>https://location1</location>
</E1>
</City>
<County>
<E2>
<location>https://location2</location>
</E2>
</County>
</State>
<State>
<id>S2</id>
<City>
<E1>
<location>https://location3</location>
</E1>
</City>
<County>
<E2>
<location>https://location4</location>
</E2>
</County>
</State>
</Country>
代碼
var container = $(document.body);
var xml = '<Country> <State> <id>S1</id> <City> <E1> <location>https://location1</location> </E1> </City> <County> <E2> <location>https://location2</location> </E2> </County> </State> <State> <id>S2</id> <City> <E1> <location>https://location3</location> </E1> </City> <County> <E2> <location>https://location4</location> </E2> </County> </State> </Country>',
xmlDoc = $.parseXML(xml),
$xml = $(xmlDoc),
$location = $xml.find('location');
container.append($("<p/>", { "text" : $location.text() }));
謝謝,但我想從那裏這個位置從如才知道:S1:E1:https://開頭LOCATION1 S2: E2:https:// location4 – user2656713 2014-12-04 08:25:42
@ user2656713看到我更新的答案,您需要使XML格式化以獲得location2和location4的正確結果,目前它顯示'undefined'。當位置標記不存在時,您可以使用「if-condition」。 – 2014-12-04 08:35:48
很酷謝謝你,這是笏我看着救了我的一天謝謝! – user2656713 2014-12-04 08:42:53