jquery解析xml并按元素值分组

发布于 2024-11-17 03:56:22 字数 1549 浏览 0 评论 0原文

我有以下 xml,我使用 jquery 解析它。

然而,我坚持只显示按标题分组的城市和日期。 我怎样才能解析xml,以便最终输出像这样

Test event 1 - oslo(2011/08/11), dallas(2011/11/11)

Test event 2 - new york(...), Seattle( ...)

任何帮助将不胜感激,

谢谢, 特里

<root>
    <item guid="np_108886">
        <title>Test event 1</title>
        <description>lorem ipsum</description>
        <specfields>
            <city>oslo</city>
            <startdate>2011/08/11</startdate>
        </specfields>
    </item>

    <item guid="np_108886">
        <title>Test event 1</title>
        <description>lorem ipsum</description>
        <specfields>
            <city>dallas</city>
            <startdate>2011/11/11</startdate>
        </specfields>
    </item>

    <item guid="np_108886">
        <title>Test event 2</title>
        <description>lorem ipsum</description>
        <specfields>
            <city>new york</city>
            <startdate>2011/09/11</startdate>
        </specfields>
    </item>
    <item guid="np_108886">
        <title>Test event 2</title>
        <description>lorem ipsum</description>
        <specfields>
            <city>seattle</city>
            <startdate>2011/09/11</startdate>
        </specfields>
    </item>
</root>

I have the following xml which I parse using jquery.

However I'm stuck at displaying only the cities and dates grouped by the title.
How can I parse the xml so that the final output would be like this

Test event 1 - oslo(2011/08/11), dallas(2011/11/11)

Test event 2 - new york(...), seattle(...)

Any Help would be appreciated,

Thanks,
Terry

<root>
    <item guid="np_108886">
        <title>Test event 1</title>
        <description>lorem ipsum</description>
        <specfields>
            <city>oslo</city>
            <startdate>2011/08/11</startdate>
        </specfields>
    </item>

    <item guid="np_108886">
        <title>Test event 1</title>
        <description>lorem ipsum</description>
        <specfields>
            <city>dallas</city>
            <startdate>2011/11/11</startdate>
        </specfields>
    </item>

    <item guid="np_108886">
        <title>Test event 2</title>
        <description>lorem ipsum</description>
        <specfields>
            <city>new york</city>
            <startdate>2011/09/11</startdate>
        </specfields>
    </item>
    <item guid="np_108886">
        <title>Test event 2</title>
        <description>lorem ipsum</description>
        <specfields>
            <city>seattle</city>
            <startdate>2011/09/11</startdate>
        </specfields>
    </item>
</root>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吃颗糖壮壮胆 2024-11-24 03:56:22

http://jsfiddle.net/XnmNU/6/ - 工作 jFiddle {编辑子选择器!}

我的版本将以前的标题与当前的标题进行比较,以确定它是否是骗局,抱歉,我没有时间绝对优化我正在工作的代码:(

输出>
开始
测试活动1——奥斯陆 (2011/08/11)
达拉斯 (2011/11/11)
测试活动2——纽约 (2011/09/11)
西雅图 (2011/09/11)

$("document").ready(function(){
    var xml = "<root>     <item guid='np_108886'>         <title>Test event 1</title>         <description>lorem ipsum</description>         <specfields>             <city>oslo</city>             <startdate>2011/08/11</startdate>         </specfields>     </item>      <item guid='np_108886'>         <title>Test event 1</title>         <description>lorem ipsum</description>         <specfields>             <city>dallas</city>             <startdate>2011/11/11</startdate>         </specfields>     </item>      <item guid='np_108886'>         <title>Test event 2</title>         <description>lorem ipsum</description>         <specfields>             <city>new york</city>             <startdate>2011/09/11</startdate>         </specfields>     </item>     <item guid='np_108886'>         <title>Test event 2</title>         <description>lorem ipsum</description>         <specfields>             <city>seattle</city>             <startdate>2011/09/11</startdate>         </specfields>     </item> </root>",     xmlDoc = $.parseXML( xml ); xml = $(xmlDoc);

    try
  {
  $(xml).find('[nodeName=item]').each(function(){    if ($(this).index() != 0) {
      possitionTitle = $(this).prev();
     if ($(this).children('title').text() != possitionTitle.children('title').text() ) {
                $("div").append("</li><li>"+$(this).children('title').text()+" -- "+$(this).children("specfields").children("city").text()+" ("+$(this).children("specfields").children("startdate").text()+")");
                } else {
                    $("div").append($(this).children("specfields").children("city").text()+" ("+$(this).children("specfields").children("startdate").text()+")");
                }
        } else {
            $("div").append("Start<li>"+$(this).children('title').text()+" -- "+$(this).children("specfields").children("city").text()+" ("+$(this).children("specfields").children("startdate").text()+")");
        }
        $("div").append("</li>");

    });

  }
catch(err)
  {
  alert(err);
  }

});
<div></div>

http://jsfiddle.net/XnmNU/6/ - working jFiddle {edit child selectors!}

My version compares the previouse title against the current to decern if its a dupe or not, sorry i do not have time to absolutly optimise the code i am at work :(

outputs>
Start
Test event 1 -- oslo (2011/08/11)
dallas (2011/11/11)
Test event 2 -- new york (2011/09/11)
seattle (2011/09/11)

$("document").ready(function(){
    var xml = "<root>     <item guid='np_108886'>         <title>Test event 1</title>         <description>lorem ipsum</description>         <specfields>             <city>oslo</city>             <startdate>2011/08/11</startdate>         </specfields>     </item>      <item guid='np_108886'>         <title>Test event 1</title>         <description>lorem ipsum</description>         <specfields>             <city>dallas</city>             <startdate>2011/11/11</startdate>         </specfields>     </item>      <item guid='np_108886'>         <title>Test event 2</title>         <description>lorem ipsum</description>         <specfields>             <city>new york</city>             <startdate>2011/09/11</startdate>         </specfields>     </item>     <item guid='np_108886'>         <title>Test event 2</title>         <description>lorem ipsum</description>         <specfields>             <city>seattle</city>             <startdate>2011/09/11</startdate>         </specfields>     </item> </root>",     xmlDoc = $.parseXML( xml ); xml = $(xmlDoc);

    try
  {
  $(xml).find('[nodeName=item]').each(function(){    if ($(this).index() != 0) {
      possitionTitle = $(this).prev();
     if ($(this).children('title').text() != possitionTitle.children('title').text() ) {
                $("div").append("</li><li>"+$(this).children('title').text()+" -- "+$(this).children("specfields").children("city").text()+" ("+$(this).children("specfields").children("startdate").text()+")");
                } else {
                    $("div").append($(this).children("specfields").children("city").text()+" ("+$(this).children("specfields").children("startdate").text()+")");
                }
        } else {
            $("div").append("Start<li>"+$(this).children('title').text()+" -- "+$(this).children("specfields").children("city").text()+" ("+$(this).children("specfields").children("startdate").text()+")");
        }
        $("div").append("</li>");

    });

  }
catch(err)
  {
  alert(err);
  }

});
<div></div>
无声静候 2024-11-24 03:56:22

希望这有帮助。我已经测试并创建了一个js小提琴: http://jsfiddle.net/xDqZK/

基本上一旦你有了如果你在 javscript 中使用 xml,你可以使用 jquerys parseXML() 函数将其获取到 jquery 对象中。进入对象后,您可以搜索不同的节点并使用 $.find() 方法在它们内进行迭代。一件棘手的事情是,如果你想获取节点的值,你需要使用 .text()。我正在使用 .val() 和 .hmtl() 返回未定义。

var strXml ="<root><item guid=\"np_108886\"><title>Test event 1<\/title><description>lorem ipsum<\/description><specfields><city>oslo<\/city><startdate>2011\/08\/11<\/startdate><\/specfields><\/item><item guid=\"np_108886\"><title>Test event 1<\/title><description>lorem ipsum<\/description><specfields><city>dallas<\/city><startdate>2011\/11\/11<\/startdate><\/specfields><\/item><item guid=\"np_108886\"><title>Test event 2<\/title><description>lorem ipsum<\/description><specfields><city>new york<\/city><startdate>2011\/09\/11<\/startdate><\/specfields><\/item><item guid=\"np_108886\"><title>Test event 2<\/title><description>lorem ipsum<\/description><specfields><city>seattle<\/city><startdate>2011\/09\/11<\/startdate><\/specfields><\/item><\/root>";
var xml = $.parseXML(strXml);

$("#test").html((parse(xml,"Test event 1")));

function parse(xml, strEvent)
{
    var eventStr = "",$xml = $(xml);
    $xml.find("item").each(function(){
        if($(this).find("title").text().toLowerCase() == strEvent.toLowerCase()){
           var childNode = $(this).find("specfields")
           eventStr += childNode.find("city").text();
           eventStr += " (" + childNode.find("startdate").text() + ") ";
        }
    });
    return eventStr;
}

超文本标记语言

<div id="test></div>

Hope this helps. I have tested and created a js fiddle: http://jsfiddle.net/xDqZK/

Basically once you have the xml in javscript you can use jquerys parseXML() function to get it into a jquery object. Once in the object you can search for different nodes and iterate within them using the $.find() method. One tricky thing, if you want to get the value of a node you need to use .text(). I was using .val() and .hmtl() which returned undefined.

var strXml ="<root><item guid=\"np_108886\"><title>Test event 1<\/title><description>lorem ipsum<\/description><specfields><city>oslo<\/city><startdate>2011\/08\/11<\/startdate><\/specfields><\/item><item guid=\"np_108886\"><title>Test event 1<\/title><description>lorem ipsum<\/description><specfields><city>dallas<\/city><startdate>2011\/11\/11<\/startdate><\/specfields><\/item><item guid=\"np_108886\"><title>Test event 2<\/title><description>lorem ipsum<\/description><specfields><city>new york<\/city><startdate>2011\/09\/11<\/startdate><\/specfields><\/item><item guid=\"np_108886\"><title>Test event 2<\/title><description>lorem ipsum<\/description><specfields><city>seattle<\/city><startdate>2011\/09\/11<\/startdate><\/specfields><\/item><\/root>";
var xml = $.parseXML(strXml);

$("#test").html((parse(xml,"Test event 1")));

function parse(xml, strEvent)
{
    var eventStr = "",$xml = $(xml);
    $xml.find("item").each(function(){
        if($(this).find("title").text().toLowerCase() == strEvent.toLowerCase()){
           var childNode = $(this).find("specfields")
           eventStr += childNode.find("city").text();
           eventStr += " (" + childNode.find("startdate").text() + ") ";
        }
    });
    return eventStr;
}

HTML

<div id="test></div>
゛时过境迁 2024-11-24 03:56:22

像这样的东西应该可以工作(未经测试):

$(document).ready(function()
{
  $.ajax({
    type: "GET",
    url: "YOUR XML",
    dataType: "xml",
    success: parseXml
  });
});

function parseXml(xml){
    $(xml).find("title").filter(function(){
                $(this).text() == "Test event 1";
         }).each(function(){
             $("#dataDiv").append($(this).find("city").text();
             $("#dataDiv").append("(" + $(this).find("startdate").text() + "),");
     });

     $(xml).find("title").filter(function(){
                $(this).text() == "Test event 2";
         }).each(function(){
             $("#dataDiv").append($(this).find("city").text();
             $("#dataDiv").append("(" + $(this).find("startdate").text() + "),");
     });  

}

其中 #dataDiv#dataDiv2 是页面上只有 测试事件 1的 div在其中测试事件 2。

Something like this should work (untested):

$(document).ready(function()
{
  $.ajax({
    type: "GET",
    url: "YOUR XML",
    dataType: "xml",
    success: parseXml
  });
});

function parseXml(xml){
    $(xml).find("title").filter(function(){
                $(this).text() == "Test event 1";
         }).each(function(){
             $("#dataDiv").append($(this).find("city").text();
             $("#dataDiv").append("(" + $(this).find("startdate").text() + "),");
     });

     $(xml).find("title").filter(function(){
                $(this).text() == "Test event 2";
         }).each(function(){
             $("#dataDiv").append($(this).find("city").text();
             $("#dataDiv").append("(" + $(this).find("startdate").text() + "),");
     });  

}

where #dataDiv and #dataDiv2 are divs on the page with just Test event 1 or Test event 2 inside them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文