jquery解析xml并按元素值分组
我有以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
http://jsfiddle.net/XnmNU/6/ - 工作 jFiddle {编辑子选择器!}
我的版本将以前的标题与当前的标题进行比较,以确定它是否是骗局,抱歉,我没有时间绝对优化我正在工作的代码:(
输出>
开始
测试活动1——奥斯陆 (2011/08/11)
达拉斯 (2011/11/11)
测试活动2——纽约 (2011/09/11)
西雅图 (2011/09/11)
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)
希望这有帮助。我已经测试并创建了一个js小提琴: http://jsfiddle.net/xDqZK/
基本上一旦你有了如果你在 javscript 中使用 xml,你可以使用 jquerys parseXML() 函数将其获取到 jquery 对象中。进入对象后,您可以搜索不同的节点并使用 $.find() 方法在它们内进行迭代。一件棘手的事情是,如果你想获取节点的值,你需要使用 .text()。我正在使用 .val() 和 .hmtl() 返回未定义。
超文本标记语言
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.
HTML
像这样的东西应该可以工作(未经测试):
其中
#dataDiv
和#dataDiv2
是页面上只有 测试事件 1 或 的 div在其中测试事件 2。Something like this should work (untested):
where
#dataDiv
and#dataDiv2
are divs on the page with just Test event 1 or Test event 2 inside them.