2012-02-07 2 views
2

내 URL에 변수를 설정했는데 다음 페이지에서이 변수가 내 XML의 기사 제목이됩니다. 나는이 기사에서 내용의 나머지 부분을 얻으 려 고 노력했지만 실패했다. XML의 레이아웃은 다음과 같다. 내가 18/05/1987를 검색 할 때javascript를 사용하여 특정 제목의 내용을 XML로 가져 오는 방법은 무엇입니까?

<title> 
     <artist> 
     Nirvana 
     </artist> 
     <description> 
     Nirvana in concert 
     </description> 
     <date> 
     18/05/1987 
     </date> 
    </title> 
    <title> 
    <artist> 
    led zeppelin 
    </artist> 
    <description> 
    led in concert 
    </description> 
    <date> 
    18/05/1987 
    </date> 
    </title> 

내가 디스플레이 열반과 인도 + 설명, 그래서 해당 노드의 전체 내용을합니다.

편집 내가 코드를 시도하고 단 1 답을 얻을 수가있다 ... 아주 새로운 메신저 그래서 난 수표 itterated 때 내가 더 답변을 얻을하지 않는 이유하지 않는

xmlDoc=loadXMLDoc("data.xml"); 
var hash = getUrlVars(); 
var date = hash['date']; 
var nodeList = xmlDoc.getElementsByTagName("article"); 

for (var i = 0; i < nodeList.length; i++) { 
    var titleNode = nodeList[i]; 
    if(titleNode.getElementsByTagName("urltext")[i].nodeValue = date){ 
     document.write("<div style='width:450px;'>") 
     document.write("<p>"+titleNode.getElementsByTagName("title")[i].childNodes[0].nodeValue+"</p>"); 
     document.write("<p>"+titleNode.getElementsByTagName("description")[i].childNodes[0].nodeValue+"</p>"); 
     document.write("<p>"+titleNode.getElementsByTagName("urltext")[i].childNodes[0].nodeValue+"</p>"); 
     document.write("</div>") 
    } 
} 
여기
+0

이 링크 http://stackoverflow.com/questions/9065197/jquery-validation-plugin-using-custom-error-messages-from-xml-file/9065309를 참조 – mgraph

답변

3

사전

에서

타이 ... 내가 작업 할 수있는 XML DOM 문서를 생성하는 데 사용하는 작은 자바 스크립트 기능입니다

function GetXMLDoc(xml) { 
    var xmlDoc; 
    if (window.DOMParser) { 
     var parser = new DOMParser(); 
     xmlDoc = parser.parseFromString(xml, "text/xml"); 
    } 
    else // Internet Explorer 
    { 
     xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); 
     xmlDoc.async = "false"; 
     xmlDoc.loadXML(xml); 
    } 

    return xmlDoc; 
} 

그런 다음 일을 사용할 수 있습니다

var xmlDoc = GetXMLDoc(XML_String_Input); 
var title = xmlDoc.getElementsByTagName("title")[0].nodeValue; 

편집은 ... 예를 들어, 제목을 얻을과 같이이다 : 당신은 당신이 이런 일을 할 수있는 XML에 대한 검색을 수행 할 것으로 보인다으로 ...

var nodeList = xmlDoc.getElementsByTagName("title"); 
var resultNodes = []; 

for (var i = 0; i < nodeList.length; i++) { 
    var titleNode = nodeList[i]; 
    if(titleNode.getElementsByTagName("artist")[0].nodeValue == "Nirvana"){ 
     //match found for artist "Nirvana" 
     resultNodes.push(titleNode); 
    } 
} 

//here you will now have a list (`resultNodes`) of "title" nodes that matched the search (i.e. artist == "Nirvana") 
//there is enough sample code above for you to be able to process each of the seach results and gets the values for the various other properties like "data" for example 

See here for more information on XML DOM function and properties

+0

문제는 내가 질수없는 내용을 읽으십시오, 나는 특정한 내용을 ther 밖으로 얻어야한다 전자 (제 경우에는 첫 번째 페이지에서 날짜를 클릭하고 thatdate가있는 xml의 모든 내용은 다음에 표시되어야합니다. 따라서 대략 SQL에서 select * 일 것입니다. 여기서 date = 18/05/1987) 문제는 내가 내 데이터베이스에 PHP/sql을 가지고 있지 않기 때문에 xml을 작은 dbase로 사용해야한다. –

+1

@ ToonVanDooren : 그럼 XML에 대한 더 완벽한 예제를 제공 할 수 있습니까? 2 개의 제목과 루트 콜렉션 노드를 사용하여 (질문 편집) – musefan

+0

완료 ... 제 질문을 좀 더 명확히해야한다고 말해 줘서 고마워요. 때로는 자신을 설명하기가 힘들고 일부 PDL은 단지 -1 개의 설명만을 말합니다. 왜 –

관련 문제