2012-04-12 60 views
1

我的HTML代碼是這樣的通過從jQuery的XML節點遍歷每個

<script type="text/javascript" src="js/jquery.js"></script> 
<script type="text/javascript"> 
if (window.XMLHttpRequest) 
{ 
    xmlhttp=new XMLHttpRequest(); 
} else { 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
} 

xmlhttp.open("GET","brands.xml",false); 
xmlhttp.send(); 
theXmlDoc=xmlhttp.responseXML; 
function fillForm(){ 
    $(theXmlDoc).find('table[name=brands]').each(function(){ 
     alert($(this));//doesn't fire when brands.xml contains more than one entry of <table name="brands"> else shows Object object 
    }); 

我brands.xml是

<table name="brands"> 
     <column name="BrandID">1</column> 
     <column name="BrandName">AX</column> 
     <column name="CompanyInfo">FDC</column> 
     <column name="Composition">Cap</column> 
</table> 
<table name="brands"> 
     <column name="BrandID">2</column> 
     <column name="BrandName">UP</column> 
     <column name="CompanyInfo">Tor</column> 
     <column name="Composition">Asp</column> 
</table> 

brands.xml包含<table name="brands">警報的單一條目顯示Object對象,但是當我有上面顯示的多個表名不會被執行。

+0

有你使用jQuery遍歷XML具體原因,但不能找回? – 2012-04-12 12:26:53

+0

我認爲jQuery有很多的方法來處理的東西,如果有更好的選擇,從HTML訪問XML請告訴我,我想要檢索的文本,但我只能這樣做,一旦我可以從XML節點環 – saum22 2012-04-12 12:39:59

回答

2

你的XML將需要由單個節點包裹:

<tables> 
    <table name="brands"> 
     <column name="BrandID">1</column> 
     <column name="BrandName">AX</column> 
     <column name="CompanyInfo">FDC</column> 
     <column name="Composition">Cap</column> 
    </table> 
    <table name="brands"> 
     <column name="BrandID">2</column> 
     <column name="BrandName">UP</column> 
     <column name="CompanyInfo">Tor</column> 
     <column name="Composition">Asp</column> 
    </table> 
</tables> 

你還需要相應地調整你的JavaScript所以選擇這個包裹節點內。

+0

嘿其工作感謝名單,不知道這一點 – saum22 2012-04-12 12:32:02

1

需要指定表的節點之上的單個根節點。

<root-node> 
<table name="brands"> 
     <column name="BrandID">1</column> 
     <column name="BrandName">AX</column> 
     <column name="CompanyInfo">FDC</column> 
     <column name="Composition">Cap</column> 
    </table> 
    <table name="brands"> 
     <column name="BrandID">2</column> 
     <column name="BrandName">UP</column> 
     <column name="CompanyInfo">Tor</column> 
     <column name="Composition">Asp</column> 
    </table> 
</root-node> 

看到教程http://webhole.net/2009/12/16/how-to-read-xml-with-javascript/