2012-07-20 5 views
0

xml 파일을 사용하려는 Phonegap 응용 프로그램을 수행하고 있습니다. 즉 xml의 특정 노드를 검색하려고합니다. 사용 된 xml 파일은 아래에 나와 있습니다. 여기 xml의 특정 노드 검색

나는이 책을 가진 카테고리의 세부 사항을 반환 책의 범주를 검색하고 난 "웹"카테고리를 검색하면 그 book.ie의 모든 세부 사항을 표시 할

<bookstore> 
    <book category="cooking"> 
    <title lang="en">Everyday Italian</title> 
    <author>Giada De Laurentiis</author> 
    <year>2005</year> 
    <price>30.00</price> 
    </book> 
    <book category="children"> 
    <title lang="en">Harry Potter</title> 
    <author>J K. Rowling</author> 
    <year>2005</year> 
    <price>29.99</price> 
    </book> 
    <book category="web"> 
    <title lang="en">Learning XML</title> 
    <author>Erik T. Ray</author> 
    <year>2003</year> 
    <price>39.95</price> 
</book> 
"웹 ". 사전에

감사합니다 .....

답변

0

을 당신이 어떤 자바 스크립트 라이브러리를 사용하지 않으려면,이 시도 할 수 있습니다 :

<script type="text/javascript"> 
    function loadXMLString(txt) { 
     if (window.DOMParser) { 
      parser = new DOMParser(); 
      xmlDoc = parser.parseFromString(txt, "text/xml"); 
     } else // Internet Explorer 
     { 
      xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); 
      xmlDoc.async = false; 
      xmlDoc.loadXML(txt); 
     } 
     return xmlDoc; 
    } 

    window.onload = function() { 
     text = '<bookstore>' 
       + '<book category="cooking">' 
       + '<title lang="en">Everyday Italian</title>' 
       + '<author>Giada De Laurentiis</author>' 
       + '<year>2005</year>' 
       + '<price>30.00</price>' 
       + '</book>' 
       + '<book category="children">' 
       + '<title lang="en">Harry Potter</title>' 
       + '<author>J K. Rowling</author>' 
       + '<year>2005</year>' 
       + '<price>29.99</price>' 
       + '</book>' 
       + '<book category="web">' 
       + '<title lang="en">Learning XML</title>' 
       + '<author>Erik T. Ray</author>' 
       + '<year>2003</year>' 
       + '<price>39.95</price>' 
       + '</book>' 
       + '</bookstore>'; 

     xmlDoc = loadXMLString(text); 

     x = xmlDoc.getElementsByTagName("book"); 
     for (i = 0; i < x.length; i++) { 
      if (x[i].attributes["category"].value == "web") { 
       var message = ""; 
       for (var j = 0; j < x[i].childNodes.length; j++) { 
        message += x[i].childNodes[j].nodeName + ':' + x[i].childNodes[j].textContent + '\n' 
       } 
       alert(message); 
      } 
     } 
    } 
</script>