2012-04-12 4 views
1

내 코드 brands.xml가 포함되어있는 경우 <table name="brands"> 경고의 단일 항목 개체 개체를 표시하지만 내 brands.xml이HTML에 대한 각

<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> 

인이

<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 
    }); 

입니다 내가 포함 할 때 위에 표시된 것처럼 하나 이상의 테이블 이름이 실행되지 않습니다.

+0

당신이 XML을 통과하기 위해 jQuery를 사용하는 특별한 이유가 있나요를 볼 수 있지만 그것을 검색하지? –

+0

jquery에는 물건을 다루는 메소드가 많이 있다고 생각합니다. html pls에서 xml에 액세스하는 더 나은 방법이 있다면 알려주세요. 텍스트를 가져오고 싶지만 일단 XML 노드에서 루프를 돌릴 수는 있습니다. – saum22

답변

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> 

그리고 그렇게 따라 자바 스크립트를 조정이 포장 노드 내에서 선택해야합니다.

+0

안녕하세요, 작동하는 고맙습니다. didnt는 그것에 대해 알고 있었다. – saum22

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/