使用jQuery解析xml名称空间.需要帮助! [英] Parsing xml namespace with jQuery. Help needed!
问题描述
我是ajax的新手.我已经通过jquery完成了常规的xml解析,但是无法使用命名空间正常工作.我已经在网上搜索过,发现的资源很少.这是stackoverflow中的帖子,但对我不起作用.
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.
这是xml文件的一部分.假设我需要xml数据中的年份.我将如何获得它?
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>
等待您的答复.谢谢!
推荐答案
我建议尽可能使用真正的支持名称空间的XML解析器,尤其是在处理外部服务时.例如,不能保证名称空间前缀会随着时间的推移保持不变.
I recommend 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.
大多数JavaScript DOM解析器将包含getElementsByTagNameNS()
,可让您查找具有实际名称空间的元素.
Most JavaScript DOM parsers will include getElementsByTagNameNS()
, which will let you find elements with the actual namespace.
假设您的数据位于xml_file
中,则该过程可能类似于以下内容.
The process might look something like this, assuming your data was in xml_file
.
var namespace = 'http://aws.example.com/';
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');
这篇关于使用jQuery解析xml名称空间.需要帮助!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!