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 * but it is not working for me.
我是ajax新手。我已经通过jquery完成了正常的xml解析,但无法使用名称空间工作的xml。我搜索了网络,找到的资源非常少。这是*上的一个帖子,但是对我没用。
jQuery XML parsing with namespaces
使用名称空间进行jQuery XML解析。
Here is the part of the xml file. suppose i need the year number from the xml data. How i will get it?
这是xml文件的一部分。假设我需要来自xml数据的年号。我怎么得到它?
<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!
等待你的回复。谢谢!
2 个解决方案
#1
6
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.
如果可能的话,我建议使用真正的名称空间感知XML解析器,特别是在处理外部服务时。例如,不能保证名称空间前缀随时间而保持不变。
Most JavaScript DOM parsers will include getElementsByTagNameNS()
, which will let you find elements with the actual namespace.
大多数JavaScript DOM解析器将包括getElementsByTagNameNS(),它将让您找到具有实际名称空间的元素。
The process might look something like this, assuming your data was in xml_file
.
假设您的数据在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');
#2
0
You can parse the DOM when you use double backslash to escape the colon
当您使用双反斜杠来转义冒号时,您可以解析DOM
ex.
前女友。
$("aws\\:year").attr('number')
edit:
编辑:
the solution above does not work with webkit browsers. better solution
上面的解决方案不适用于webkit浏览器。更好的解决方案
$("[nodeName=aws:year]").attr('number')
didn't check this out though.
不过我没看出来。
#1
6
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.
如果可能的话,我建议使用真正的名称空间感知XML解析器,特别是在处理外部服务时。例如,不能保证名称空间前缀随时间而保持不变。
Most JavaScript DOM parsers will include getElementsByTagNameNS()
, which will let you find elements with the actual namespace.
大多数JavaScript DOM解析器将包括getElementsByTagNameNS(),它将让您找到具有实际名称空间的元素。
The process might look something like this, assuming your data was in xml_file
.
假设您的数据在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');
#2
0
You can parse the DOM when you use double backslash to escape the colon
当您使用双反斜杠来转义冒号时,您可以解析DOM
ex.
前女友。
$("aws\\:year").attr('number')
edit:
编辑:
the solution above does not work with webkit browsers. better solution
上面的解决方案不适用于webkit浏览器。更好的解决方案
$("[nodeName=aws:year]").attr('number')
didn't check this out though.
不过我没看出来。