2014-12-04 171 views
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() })); 

回答

1

嘗試像,

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'); // will return all location elements  
$location.each(function(){ 
    container.append($("<p/>", { "text" : $(this).text() })); 
}); 

Demo

更新後的代碼,以顯示與ID導致

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'); // will return all location elements  
$location.each(function() { 
    container.append($("<p/>", { 
     "text": 
      $(this).closest('State').find('id').text() + ':' + // get State ID like S1,S2 
      $(this).closest('City').find(':first-child').prop('tagName') + ':' + // get City's location tag like E1,E2 
      $(this).text() // get the location text like location1,location2.... 
    })); 
}); 

Updated Demo

+0

謝謝,但我想從那裏這個位置從如才知道:S1:E1:https://開頭LOCATION1 S2: E2:https:// location4 – user2656713 2014-12-04 08:25:42

+0

@ user2656713看到我更新的答案,您需要使XML格式化以獲得location2和location4的正確結果,目前它顯示'undefined'。當位置標記不存在時,您可以使用「if-condition」。 – 2014-12-04 08:35:48

+0

很酷謝謝你,這是笏我看着救了我的一天謝謝! – user2656713 2014-12-04 08:42:53