【问题标题】:XML hierarchy tree parsingXML层次树解析
【发布时间】:2014-01-24 15:06:33
【问题描述】:

所以我有如上所示的这个XML文件,我想解析字段outputId

-<personRepresentaion>
      <theId>1324</theId>
      <name>John</name>
      <topY>1</topY>
      <leftX>0</leftX>
      <height>10</height>
      <width>12</width>
     -<relationships>
       <inputId>1324</inputId>
       <outputId>1325</outputId>
      </relationships>
    </personRepresentaion>

目前我解析其他字段如下:

var x=xmlDoc.getElementsByTagName("personRepresentaion"); 
for (i=0;i<x.length;i++){
var y =x[i].getElementsByTagName("theId")[0].childNodes[0].nodeValue //this gets me theId

//now I want to get the outputId
var outputId = x[i].getElementsByTagName("outputId")[0].childNodes[0].nodeValue //what is wrong with this?
}

【问题讨论】:

    标签: jquery xml


    【解决方案1】:

    尝试像这样使用 jquery 来做到这一点:

    jQuery(document).ready(function($){
        var _personXML = $('<personRepresentaion><theId>1324</theId><name>John</name><topY>1</topY><leftX>0</leftX><height>10</height><width>12</width><relationships><inputId>1324</inputId><outputId>1325</outputId></relationships></personRepresentaion>');
    
        alert("the id: "+_personXML.find("theId").eq(0).text());    
        _personXML.find("relationships").each(function(){
            alert("Output id: "+$(this).find("outputId").eq(0).text());
        });
    
    });
    

    更新: *演示*http://jsfiddle.net/R74XG/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-16
      • 2011-01-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多