我有一个如下所示的xml,我正在尝试使用jquery进行解析,并得到类似于我在下面编写的java脚本来解析它的东西。我尝试了下面的代码,但是我能够获得位置,而不是像我预期的那样以下面的格式。
预期结果:
S1:E1:https://location1
S2:E2:https://location1xml:
<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() }) ); 发布于 2014-12-04 08:22:32
试试看,
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() }) ); 
});演示
使用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....
    }));
});最新演示
https://stackoverflow.com/questions/27289153
复制相似问题