将XML元素值获取到HTML元素

时间:2018-04-09 09:05:23

标签: javascript jquery

我有这样的XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0">
    <channel>
        <title>title of XML</title>
        <description>description of XML</description>
        <ttl>720</ttl>
        <item>
            <title>title_01</title>
            <description>value of item 01</description>
        </item>
        <item>
            <title>title_02</title>
            <description>value of item 02</description>
        </item>
        .
        .
        .
        .
        <item>
            <title>title_XX</title>
            <description>value of item XX</description>
        </item>


    </channel>
</rss>

我需要从元素到HTML元素获得价值。从TITLE具有特定唯一值的元素ITEM中,将DESCRIPTION值放入HTML元素:

<body>
    <div class="cont">

    <div>
    <span id="some_id01">Here put the DESCRIPTION value from the XML ITEM which contains the specific TITLE value</span>
    <span id="some_id02"></span>
    </div>

    <div>
    <span id="some_id03"></span>
    <span id="some_id04"></span>
    </div>

    <div>
    <span id="some_idXX"></span>
        </div>

    </div>

    </body>

Something like this。但是没有循环,只有固定数量的HTML元素。任何人都可以帮助我找到最佳代码,XML中最多可以有100个ITEM元素。

非常感谢

2 个答案:

答案 0 :(得分:0)

只是想提一下,希望有所帮助:

df = df.set_index(['routes']).set_index('demand days', drop=False, append=True)
#get values of routes
a = df.index.levels[0]
#get minimal and maximal days
b = range(min(df.index.levels[1]), max(df.index.levels[1]) + 1)
#create MulitIndex
mux = pd.MultiIndex.from_product([a, b],names=('routes','calendar days'))
#reinex
df = df.reindex(mux).reset_index()

答案 1 :(得分:0)

似乎有效:

$(document).ready(function() {
    //feed to parse
    var feed = "pondelitest.xml";

    $.ajax(feed, {
        accepts:{
            xml:"application/rss+xml"
        },
        dataType:"xml",
        success:function(data) {



            $(data).find("title").each(function () { 
                if($(this).text() == 'jidlo01' ) {
                        var v01 = $(this).next();
                        $("#jidlo01").append($.trim(v01.text()));
                        }
                if($(this).text() == 'jidlo02' ) {
                        var v01 = $(this).next();
                        $("#jidlo02").append($.trim(v01.text()));
                        }                       
            });
        // remove empty items
        $('span.title').each(function() {
            if ($(this).is(':empty')) $(this).parent().remove();
});

        }  
    });

});