jquery解析xml和按元素值分组 [英] jquery parse xml and group by element value
问题描述
我有以下xml,我使用jquery解析。
然而,我坚持只显示按标题分组的城市和日期。
我该如何解析xml,以便最终输出结果如下所示:测试事件1 - oslo(2011/08/11),dallas(2011) / 11/11)
测试赛事2 - 纽约(...),西雅图(...)
任何帮助将不胜感激,
谢谢,
Terry
<根>
< item guid =np_108886>
< title>测试事件1< / title>
< description> lorem ipsum< / description>
< specfields>
< city> oslo< / city>
< startdate> 2011/08/11< / startdate>
< / specfields>
< / item>
< item guid =np_108886>
< title>测试事件1< / title>
< description> lorem ipsum< / description>
< specfields>
< city> dallas< / city>
< startdate> 2011/11/11< / startdate>
< / specfields>
< / item>
< item guid =np_108886>
< title>测试事件2< / title>
< description> lorem ipsum< / description>
< specfields>
< city>纽约< / city>
< startdate> 2011/09/11< / startdate>
< / specfields>
< / item>
< item guid =np_108886>
< title>测试事件2< / title>
< description> lorem ipsum< / description>
< specfields>
< city>西雅图< / city>
< startdate> 2011/09/11< / startdate>
< / specfields>
< / item>
< / root>
http://jsfiddle.net/XnmNU/6/ - 工作jFiddle {编辑子选择器!}
我的版本比较previouse标题和当前版本,看看它是否是愚蠢的,抱歉,我没有时间绝对地优化我在工作中的代码:($ / b
$ b
输出>
开始
测试赛事1 - 奥斯陆(2011/08/11)
达拉斯(2011/11/11)
测试赛事2 - 纽约(2011/09/11)
西雅图(2011/09/11)
$(document)。 ready>< title>< title>< title ; specfields>< city> oslo< / city>< startdate> 2011/08/11< / startdate>< / specfields>< / item>< item guid ='np_108886'> < title>测试事件1< / title> < description> lorem ipsum< / description> < specfields> <城市>达拉斯< /城市> < STARTDATE>十一分之二千〇一十一/ 11版; / STARTDATE> < / specfields> < /项目> < item guid ='np_108886'> < title>测试事件2< / title> < description> lorem ipsum< / description> < specfields> < city>纽约< / city> < STARTDATE> 2011/09/11版; / STARTDATE> < / specfields> < /项目> < item guid ='np_108886'> < title>测试事件2< / title> < description> lorem ipsum< / description> < specfields> <城市>西雅图< /城市> < STARTDATE> 2011/09/11版; / STARTDATE> < / specfields> < /项目> < / root>,xmlDoc = $ .parseXML(xml); xml = $(xmlDoc);
尝试
{
$(xml).find(' ($(this).index()!= 0){
possitionTitle = $(this).prev();
if($ (this).children('title')。text()!= possitionTitle.children('title')。text()){
$(div)。append(< / li>< ()+(this).children('title')。text()+ - + $(this).children(specfields)。children(city).text()+ (+ $(this).children(specfields)。children(startdate)。text()+));
} else {
$(div)。append ((specfields)。children(city)。text()+(+ $(this).children(specfields)。children(startdate).text() (开始< li>+ $(this).children('title')。);
}
} else {
$(div text()+ - + $(this).children(specfields)。children(city)。text()+(+ $(this).children(specfields)。children ST artdate )文本()+));
}
$(div)。append(< / li>);
});
}
catch(err)
{
alert(err);
}
});
< div>< / div>
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>
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>
这篇关于jquery解析xml和按元素值分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!