使用jQuery查找XML节点并使用其值 [英] Find an XML Node Using jQuery And Use Its Values

查看:119
本文介绍了使用jQuery查找XML节点并使用其值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < Test> 
< Unit>
<标题>测试1< /标题>
<日期> 01/07/2011< / Date>
< Content format =html><![CDATA [Here goes some content]]>< / Content>
< / Unit>

<单位>
< Title>测试新的XML< / Title>
<日期> 27/06/2011< / Date>
< Content format =html><![CDATA [Here goes some content]]>< / Content>
< / Unit>
<! - 很多像这样的东西 - >
< / Test>

我想使用jQuery在XML文档中搜索<标题> 给出并得到它的整个< Unit> ,所以我可以使用这样的值:

 函数append(title,date,content){
$(#Unit)。append(< h1 id ='title'> ;< b>+ title +< / b>< / h1>< h2 id ='date'>+ date +< / h2>< p>+ content +< ; / p>中);

$ / code>

我怎样才能做到这一点?



PS:我一直在使用这个作为我读取XML的基础 这是你要找的东西吗? 点击此处(jsFiddle链接)



在该代码你必须得到的XML和存储在一个变量,像我这样做。代码可能不是你正在寻找的,但它可能是一个很好的起点。您可以试试这个(与jsFiddle链接相同的代码,但只是使用警报而不是将数据附加到DOM)

  var xml =< Test>< Unit>< Title> Test 1< / Title>< Date> 01/07/2011< / Date>< Content format ='html'>一些内容< / Content>< / Unit>< Unit>< Title>测试新XML< / Title>< Date> 27/06/2011< / Date>< Content format ='html'>更多内容< / Content>< / Unit>< / Test> 
$ b $(xml).find(Unit)。each(function(){
if($(this).find(Title)。length> 0){
var unitData = $(this).text();
alert(unitData);
}
});

这应该会给你两个提示。


I'm using a XML file like this:

<Test>
    <Unit>
        <Title>Test 1</Title>
        <Date>01/07/2011</Date>
        <Content format="html"><![CDATA[Here goes some content]]></Content>
    </Unit>

    <Unit>
        <Title>Testing New XML</Title>
        <Date>27/06/2011</Date>
        <Content format="html"><![CDATA[Here goes some content]]></Content>
    </Unit>
    <!-- A lot of stuff like this -->
</Test>

I want to use jQuery to search on the XML document for a <Title> given and get its entire <Unit>, so I can work with the values like this:

function append(title, date, content) {
    $("#Unit").append("<h1 id='title'><b>" + title + "</b></h1><h2 id='date'>" + date + "</h2><p>" + content + "</p>");
}

How can I do this?

PS: I've been using this as the base of my XML reading

解决方案

Is this what you're looking for? Click Here (jsFiddle link)

In that code you'll have to get the XML and store it in a variable first like i did. The code may not be exactly what you're looking for but it may be a good starting point. Play with the code and let me know if this is what you're looking for or not.

You could also try this (same code as jsFiddle link but just using alerts instead of appending the data to the DOM)

var xml = "<Test><Unit><Title>Test 1</Title><Date>01/07/2011</Date><Content format='html'>some content here</Content></Unit><Unit><Title>Testing New XML</Title><Date>27/06/2011</Date><Content format='html'>some more content here</Content></Unit></Test>";

$(xml).find("Unit").each(function(){
    if($(this).find("Title").length > 0){
        var unitData = $(this).text();
        alert(unitData);
    }
});

That should give you two alerts.

这篇关于使用jQuery查找XML节点并使用其值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆