2013-08-15 2 views
0

몇 가지 심각한 문제가 있습니다. 나는 비슷한 질문을 많이 발견하고 일부는 잠시 동안 일했다. 그래서 나는 내 자신의 질문을 할 시간이라고 결정했다.XML 정보 얻기

난을 FileReader API를 사용하여 XML 파일을 업로드 한 다음 문자열로에서 읽고 다음 요소를 찾고과 같이 속성입니다 :

reader.onload = function (e) { 
     var Library = new String(e.target.result); 
     if (window.DOMParser) { 
      parser = new DOMParser(); 
      xmlDoc = parser.parseFromString(Library, "text/xml"); 
      $(xmlDoc).find('book').each(function() { 

       Pages = []; 
       $(this).find("page").each(function() { 
        Page = []; 
        try { 
         Page[0] = this.getAttributeNode("number").nodeValue; 
         Page[1] = this.getAttributeNode("words").nodeValue; 
        } 
        catch (err) { 
         console.log(err); 
        } 
        Pages.push(Page); 
       }); 
      }); 
     } 
} 

나는 형식 오류 또는이 중 하나를 받고 계속 :

[09:11:08.523] TypeError: $(...).getAttributeNode is not a function 
+0

다음 중 어느 라인에서 오류가 발생합니까? –

+1

코드에 구문 오류가 많은 것 같습니다. 닫는 괄호')'가 없습니다. –

+0

죄송합니다. 구문 오류가 없습니다. 그게 바로 제가 입력 한 것입니다. 해당 행은 끝에 .nodeValue가있는 행입니다. – Funkyguy

답변

1

당신이 jQuery를 ($(this).each(...)) 다음 낮은 수준의 DOM (.getAttributeNode())를 혼합하는 이유를 이해하지 않습니다. 균질 jQuery로 고정 : http://jsfiddle.net/kzcBE

var xmlMarkup = "<example>\n" + 
       " <book>\n" + 
       " <page number='p1' words='1104' />\n" + 
       " <page number='p2' words='1230' />\n" + 
       " </book>\n" + 
       " <book>\n" + 
       " <page number='p1' words='123' />\n" + 
       " <page number='p2' words='145' />\n" + 
       " </book>\n" + 
       "</example>"; 

var Library = new String(xmlMarkup); 
if (window.DOMParser) { 
    parser = new DOMParser(); 
    xmlDoc = parser.parseFromString(Library, "text/xml"); 
    $(xmlDoc).find('book').each(function() { 
     $(this).find("page").each(function() { 
      var numberAttr = $(this).attr("number"); 
      var wordsAttr = $(this).attr("words"); 
      console.log("page number: " + numberAttr); 
      console.log("word count: " + wordsAttr); 
     }); 
    }); 
} 
+0

고마워요! 매력처럼 작동합니다. – Funkyguy