使用jQuery获取XML中的节点值 [英] Get node value in XML using jQuery
本文介绍了使用jQuery获取XML中的节点值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试解析以下XML:
<AssetImageModel xmlns="http://schemas.datacontract.org/2004/07/ErgonFileService.Model" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<AssetId>00000000-0000-0000-0000-000000000000</AssetId>
<AssetImageId>b74af53a-91e6-83f5-26f9-e1719ad5fd05</AssetImageId>
<ImagePath i:nil="true"/>
<IsDeleted>false</IsDeleted>
<Modified>false</Modified>
<Version>0</Version>
</AssetImageModel>
例如,为了检索AssetImageId
值.使用JS或jQuery实现此目的的最简单方法是什么?
解决方案
如果您有xml文本,请使用$ .parseXML()将其解析为xml对象,然后使用find()来定位元素.
var text = '<AssetImageModel xmlns="http://schemas.datacontract.org/2004/07/ErgonFileService.Model" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><AssetId>00000000-0000-0000-0000-000000000000</AssetId><AssetImageId>b74af53a-91e6-83f5-26f9-e1719ad5fd05</AssetImageId><ImagePath i:nil="true"/><IsDeleted>false</IsDeleted><Modified>false</Modified><Version>0</Version></AssetImageModel>'
var xml = $.parseXML(text);
console.log($(xml).find('AssetImageId').text())
演示:小提琴
如果您使用的是ajax,请设置dataType: 'xml'
,以便您可以将已解析的对象作为成功处理程序中的数据获取
演示:小提琴
I'm trying to parse the following XML:
<AssetImageModel xmlns="http://schemas.datacontract.org/2004/07/ErgonFileService.Model" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<AssetId>00000000-0000-0000-0000-000000000000</AssetId>
<AssetImageId>b74af53a-91e6-83f5-26f9-e1719ad5fd05</AssetImageId>
<ImagePath i:nil="true"/>
<IsDeleted>false</IsDeleted>
<Modified>false</Modified>
<Version>0</Version>
</AssetImageModel>
In order to retrieve for example the AssetImageId
value. What would be the easiest way to achieve this using either JS or jQuery?
解决方案
If you are having a xml text then use $.parseXML() to parse it to a xml object then use find() to locate the element.
var text = '<AssetImageModel xmlns="http://schemas.datacontract.org/2004/07/ErgonFileService.Model" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><AssetId>00000000-0000-0000-0000-000000000000</AssetId><AssetImageId>b74af53a-91e6-83f5-26f9-e1719ad5fd05</AssetImageId><ImagePath i:nil="true"/><IsDeleted>false</IsDeleted><Modified>false</Modified><Version>0</Version></AssetImageModel>'
var xml = $.parseXML(text);
console.log($(xml).find('AssetImageId').text())
Demo: Fiddle
If you are using ajax then set the dataType: 'xml'
so that you can get the parsed object as the data in your success handler
Demo: Fiddle
这篇关于使用jQuery获取XML中的节点值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文