【问题标题】:How to parse and fetch specific Attribute from XML using jQuery.parseXML()?如何使用 jQuery.parseXML() 从 XML 中解析和获取特定属性?
【发布时间】:2015-09-01 06:19:29
【问题描述】:

我有一个类似的 XML 字符串

<?xml version="1.0" encoding="UTF-8"?>
<ProductDetails ProductID="TuscanWengeShoeRack010cfg" ProductcfgPath="http://localhost/media/productattachment/176/TuscanWengeShoeRack010cfg.xml" SKUID="TWS090" />

我需要输出为

var pid = // should be fetched from ProductcfgPath attribute
var sku = // should be fetched from SKUID attribute

alert(pid);  // 176
alert(sku);  // TWS090

【问题讨论】:

  • 这是执行此操作的示例:- api.jquery.com/jQuery.parseXML 另一个链接是 stackoverflow.com/questions/7228141/…
  • @Deepaksaini 已经这样做了,我只得到对象 [Object]
  • @ShadabMehdi 你能展示你的尝试吗..
  • 也许你应该添加 .text() 从节点获取价值
  • @ShadabMehdi 下次您应该写下您尝试过的内容以及错误的确切位置。

标签: jquery xml parsexml


【解决方案1】:

解决了

var rawXmlString = '\<\?xml version = "1.0" encoding = "UTF-8" ?><ProductDetails ProductID="TuscanWengeShoeRack010cfg" ProductcfgPath="http://localhost/media/productattachment/176/TuscanWengeShoeRack010cfg.xml" SKUID="TWS090" />';
var $xmlJSObject = $.parseXML(rawXmlString);
var path = $($xmlJSObject).find('ProductDetails').attr('ProductcfgPath');
var sku = $($xmlJSObject).find('ProductDetails').attr('SKUID');

var numberArray = path.match(/(\d+)/g);
var pid = numberArray[0];

alert(pid);  // 176
alert(sku);  // TWS090

【讨论】:

    猜你喜欢
    • 2013-12-19
    • 1970-01-01
    • 2021-08-29
    • 1970-01-01
    • 2012-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多