首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用jquery解析带有节点元素的xml

使用jquery解析带有节点元素的xml
EN

Stack Overflow用户
提问于 2014-12-04 08:15:38
回答 1查看 19关注 0票数 1

我有一个如下所示的xml,我正在尝试使用jquery进行解析,并得到类似于我在下面编写的java脚本来解析它的东西。我尝试了下面的代码,但是我能够获得位置,而不是像我预期的那样以下面的格式。

预期结果:

代码语言:javascript
运行
复制
S1:E1:https://location1
S2:E2:https://location1

xml:

代码语言:javascript
运行
复制
<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>  

代码

代码语言:javascript
运行
复制
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() }) ); 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-04 08:22:32

试试看,

代码语言:javascript
运行
复制
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显示结果的更新代码

代码语言:javascript
运行
复制
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....
    }));
});

最新演示

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27289153

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档