javascript - Parsing xml namespace with jQuery. Help needed! - Stack Overflow

i am new with ajax. I have done normal xml parsing via jquery but can not get the xml with namespace wo

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 badges
Add a ment  | 

2 Answers 2

Reset to default 6

I 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745442865a4627909.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信