2011-01-11 29 views
0

我的XML文件是:是否可以從包含相似標籤的xml解析數據?

<?xml version="1.0" encoding="UTF-8" ?> 
<root> 
<investors> 
    <investor>Active</investor> 
    <investor>Aggressive</investor> 
    <investor>Conservative</investor> 
    <investor>Day Trader</investor> 
    <investor>Very Active</investor> 
    </investors> 
<events> 
    <event>3 Month Expiry</event> 
    <event>LEAPS</event> 
    <event>Monthlies</event> 
    <event>Monthly Expiries</event> 
    <event>Weeklies</event> 
    <event>Weeklies Expiry</event> 
    </events> 
<prices> 
    <price>0.05</price> 
    <price>0.5</price> 
    <price>1</price> 
    <price>1</price> 
    <price>22</price> 
    <price>100.34</price> 
    </prices> 
    </root> 

我的ExtJS的代碼是:

 Ext.regModel('Card', { 
     fields: ['investor','event','price']  
    }); 

    var store = new Ext.data.Store({ 
     model: 'Card', 
     proxy: { 
      type: 'ajax', 
      url: 'http:/.../initpicker.php', 
      reader: { 
       type: 'xml', 
       record: 'root' 
      } 
     }, 
     listeners: { 
      single: true, 
      datachanged: function(){ 
       var items = []; 
       store.each(function(r){ 
       stocks.push({text: '<span style="color:Blue;font-weight:bolder;font-size:30px;">'+ r.get('price') +'</span>'}); 
       values.push({text: '<span style="font-weight: bold;font-size:25px;">'+ r.get('investor') +'</span>'}); 
       points.push({text: '<span style="font-weight: bold;font-size:25px;">'+ r.get('event') +'</span>'}); 
       }); 
      } 
     }  
    }); 
    store.read(); 

我的問題是,如果我的XML包含像五次我們仍然可以分析數據相同的標籤。 。 。 。 。?

我試過這個代碼,但它只是解析第一個..........................

如果有任何其他方式請建議。 。 。

謝謝。

+0

您可能應該已經更新了您的原始問題。 http://stackoverflow.com/questions/4645294/something-wrong-with-my-xml – Hemlock 2011-01-12 14:39:05

+0

需要一些幫助,以解決以下問題。請幫助 http://stackoverflow.com/questions/10971255/read-nested-xml-to-model-with-hasmany-association-sencha-extjs – buddy 2012-06-10 18:46:52

回答

0

您可以使用ExtJS的解析XML。但xml文件應該在同一個域中

3

這實際上取決於你的記錄看起來像什麼。

第一個投資者元素是否應該與第一個事件和價格元素相關並捆綁到一個記錄中?第二條記錄呢 - 是否包含Aggressive,LEAPS和0.5作爲數據值?如果是這樣,XML確實沒有多大意義。

我不相信Sencha的XmlReader能夠很好地處理這個問題,這就解釋了爲什麼你只能得到第一個記錄。

解決辦法有兩個:

  1. 修改XML正在生產更有意義的的XmlReader
  2. 寫你自己的閱讀器類從現有數據的格式解析和提取記錄你
0

你在用什麼XML?

我假設它是一個網格。此外,您的代碼似乎不符合您的XML中的標籤。你想要在代碼中獲取哪些數據?設置數據對象時,應該從XML中的標籤訪問數據。

我建議你重新思考你的XML的結構。您的標籤不會描述標籤中包含的數據。在某些方面,您似乎錯過了XML的重點。

像這樣的東西應該是你需要填充網格。

<investors> 
<investor> 
    <name>Bob</name> 
    <style>Aggressive</style> 
    <price>0.5</price> 
</investor> 
<investor> 
    <name>Francis</name> 
    <price>150.00</price> 
    </investor> 
</investors> 

我強烈建議你看看這個鏈接: XML Grid Sample from Sencha Webste