2012-09-26 98 views
0

我需要讀取XML,然後在表格中使用它來填充圖表。這是我的XML使用jQuery解析XML

<?xml version="1.0" encoding="utf-8"?> 
<Export> 
    <Columns> 
    <Dim1>Heures</Dim1> 
    <Metric1>Visites</Metric1> 
    </Columns> 
    <Row Dim1="0h" Metric1="5347" Var1="" Diff1="5347" Rat1="0,0728940874947173" Metric12="0,0728940874947173" Var12="" Diff12="-2147483648" Metric2="116946" Var2="" Diff2="116946" Rat2="0,35501654473149" Metric5="21,8713297175987" Var5="" Diff5="21,8713297175987" /> 
    <Row Dim1="1h" Metric1="3121" Var1="" Diff1="3121" Rat1="0,0425476803948032" Metric12="0,0425476803948032" Var12="" Diff12="-2147483648" Metric2="7831" Var2="" Diff2="7831" Rat2="0,023772805925746" Metric5="2,50913168856136" Var5="" Diff5="2,50913168856136" /> 
    <Row Dim1="2h" Metric1="2362" Var1="" Diff1="2362" Rat1="0,0322004553324336" Metric12="0,0322004553324336" Var12="" Diff12="-2147483648" Metric2="6513" Var2="" Diff2="6513" Rat2="0,0197717130627485" Metric5="2,75740897544454" Var5="" Diff5="2,75740897544454" /> 
    <Row Dim1="3h" Metric1="1591" Var1="" Diff1="1591" Rat1="0,0216896377789593" Metric12="0,0216896377789593" Var12="" Diff12="-2147483648" Metric2="5406" Var2="" Diff2="5406" Rat2="0,0164111593454965" Metric5="3,39786297925833" Var5="" Diff5="3,39786297925833" /> 
    <Row Dim1="4h" Metric1="1692" Var1="" Diff1="1692" Rat1="0,0230665412457568" Metric12="0,0230665412457568" Var12="" Diff12="-2147483648" Metric2="5630" Var2="" Diff2="5630" Rat2="0,0170911629883732" Metric5="3,3274231678487" Var5="" Diff5="3,3274231678487" /> 
    <Row Dim1="5h" Metric1="2206" Var1="" Diff1="2206" Rat1="0,030073752948073" Metric12="0,030073752948073" Var12="" Diff12="-2147483648" Metric2="10108" Var2="" Diff2="10108" Rat2="0,0306851643848092" Metric5="4,58204895738894" Var5="" Diff5="4,58204895738894" /> 
    <Row Dim1="6h" Metric1="4684" Var1="" Diff1="4684" Rat1="0,063855602361185" Metric12="0,063855602361185" Var12="" Diff12="-2147483648" Metric2="16577" Var2="" Diff2="16577" Rat2="0,0503233053034213" Metric5="3,53906917164816" Var5="" Diff5="3,53906917164816" /> 
    <Row Dim1="7h" Metric1="10821" Var1="" Diff1="10821" Rat1="0,147519528853626" Metric12="0,147519528853626" Var12="" Diff12="-2147483648" Metric2="34800" Var2="" Diff2="34800" Rat2="0,105643423089767" Metric5="3,21596894926532" Var5="" Diff5="3,21596894926532" /> 
    <Row Dim1="8h" Metric1="19478" Var1="" Diff1="19478" Rat1="0,265537878478045" Metric12="0,265537878478045" Var12="" Diff12="-2147483648" Metric2="66604" Var2="" Diff2="66604" Rat2="0,202191797456058" Metric5="3,41944758188726" Var5="" Diff5="3,41944758188726" /> 
    <Row Dim1="9h" Metric1="22051" Var1="" Diff1="22051" Rat1="0,300614835112402" Metric12="0,300614835112402" Var12="" Diff12="-2147483648" Metric2="58995" Var2="" Diff2="58995" Rat2="0,179092923712091" Metric5="2,675388871253" Var5="" Diff5="2,675388871253" /> 
</Export> 

那麼這就是我已經盡力了,但沒有任何反應,我只有一個空白頁,有人可以幫我嗎?

<script type="text/javascript"> 
$.get("graph.xml", function(data, textStatus, jqXHR){ 
     var heures, 
      nbr, 
      divItems = $("<div/>", { 
       "class" : "items" 
      }); 

     $(data).find("Export").children().each(function(i, item){ 
      $(item).children().each(function(j , jtem){ 
       heures = $(jtem).attr("Dim1"); 
       nbr = $(jtem).attr("Metric1"); 

       $(divItems) 
        .clone() 
        .html('<a href="' + heures + '">' + nbr + '</a>') 
        .appendTo("#xml"); 
      }); 
     }); 
     </script> 

     </head> 
     <body> 
      <div id="xml"></div> 
      </body> 

謝謝。

回答

2

遍歷每個代替:

var $divItems = $("<div/>", { 
    "class" : "items" 
}); 

$(data).find("Row").each(function(i, item) { 
    var heures = $(item).attr("Dim1"); 
    var nbr = $(item).attr("Metric1"); 
    $divItems.clone() 
     .html('<a href="' + heures + '">' + nbr + '</a>') 
     .appendTo("#xml"); 
}); 

這裏有一個DEMO