使用jquery解析XML节点元素

问题描述:

我有一个像下面的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() })); 

尝试像,

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