admin管理员组文章数量:1430083
i am new with ajax. I have done normal xml parsing via jquery but can not get the xml with namespace working. I have searched the web and there is very few resources i found. Here is a post in stackoverflow but it is not working for me.
jQuery XML parsing with namespaces
Here is the part of the xml file. suppose i need the year number from the xml data. How i will get it?
<aws:sunset>
<aws:year number="2011" />
<aws:month number="3" text="March" abbrv="Mar" />
<aws:day number="27" text="Sunday" abbrv="Sun" />
<aws:hour number="7" hour-24="19" />
<aws:minute number="10" />
<aws:second number="28" />
<aws:am-pm abbrv="PM" />
<aws:time-zone offset="-5" text="Central Daylight Time (USA)" abbrv="CDT" />
</aws:sunset>
Waiting for your reply. Thanks!
i am new with ajax. I have done normal xml parsing via jquery but can not get the xml with namespace working. I have searched the web and there is very few resources i found. Here is a post in stackoverflow but it is not working for me.
jQuery XML parsing with namespaces
Here is the part of the xml file. suppose i need the year number from the xml data. How i will get it?
<aws:sunset>
<aws:year number="2011" />
<aws:month number="3" text="March" abbrv="Mar" />
<aws:day number="27" text="Sunday" abbrv="Sun" />
<aws:hour number="7" hour-24="19" />
<aws:minute number="10" />
<aws:second number="28" />
<aws:am-pm abbrv="PM" />
<aws:time-zone offset="-5" text="Central Daylight Time (USA)" abbrv="CDT" />
</aws:sunset>
Waiting for your reply. Thanks!
Share Improve this question edited May 23, 2017 at 11:59 CommunityBot 11 silver badge asked Mar 28, 2011 at 18:31 SisirSisir 2,8047 gold badges55 silver badges86 bronze badges2 Answers
Reset to default 6I remend using a real namespace-aware XML parser if at all possible, especially when dealing with external services. There is no guarantee that the namespace prefix will remain constant over time, for example.
Most JavaScript DOM parsers will include getElementsByTagNameNS()
, which will let you find elements with the actual namespace.
The process might look something like this, assuming your data was in xml_file
.
var namespace = 'http://aws.example./';
var parser = new DOMParser(); // Webkit, IE has its own
var xml = parser.parseFromString(xml_file, "text/xml");
var year = xml.getElementsByTagNameNS(namespace, 'year')[0]; // returns the first aws:year element
var year_value = year.getAttribute('number');
You can parse the DOM when you use double backslash to escape the colon
ex.
$("aws\\:year").attr('number')
edit:
the solution above does not work with webkit browsers. better solution
$("[nodeName=aws:year]").attr('number')
didn't check this out though.
本文标签: javascriptParsing xml namespace with jQuery Help neededStack Overflow
版权声明:本文标题:javascript - Parsing xml namespace with jQuery. Help needed! - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745442864a2658525.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论