2011-06-24 73 views
1

我有我使用jquery解析的以下xml。jquery解析xml和按元素值

但是我堅持只顯示按標題分組的城市和日期。 我如何解析XML使最終輸出會是這樣

測試賽1 - 奧斯陸(2011/08/11),達拉斯(2011/11/11)

測試賽2 - 新紐約(...),西雅圖(...)

任何幫助,將不勝感激,

感謝, 特里

<root> 
    <item guid="np_108886"> 
     <title>Test event 1</title> 
     <description>lorem ipsum</description> 
     <specfields> 
      <city>oslo</city> 
      <startdate>2011/08/11</startdate> 
     </specfields> 
    </item> 

    <item guid="np_108886"> 
     <title>Test event 1</title> 
     <description>lorem ipsum</description> 
     <specfields> 
      <city>dallas</city> 
      <startdate>2011/11/11</startdate> 
     </specfields> 
    </item> 

    <item guid="np_108886"> 
     <title>Test event 2</title> 
     <description>lorem ipsum</description> 
     <specfields> 
      <city>new york</city> 
      <startdate>2011/09/11</startdate> 
     </specfields> 
    </item> 
    <item guid="np_108886"> 
     <title>Test event 2</title> 
     <description>lorem ipsum</description> 
     <specfields> 
      <city>seattle</city> 
      <startdate>2011/09/11</startdate> 
     </specfields> 
    </item> 
</root> 

回答

3

http://jsfiddle.net/XnmNU/6/ - 工作jFiddle

我的版本進行比較逆水previouse標題,如果其decern愚弄的人或沒有,對不起,我做的{編輯子選擇!}沒有時間絕對優化我在工作中的代碼:(

輸出> 開始 測試事件1 - 奧斯陸(2011/08/11) 達拉斯(2011/11/11) 測試事件2 - 紐約(2011/09/11) 西雅圖(2011/09/11)

$("document").ready(function(){ 
    var xml = "<root>  <item guid='np_108886'>   <title>Test event 1</title>   <description>lorem ipsum</description>   <specfields>    <city>oslo</city>    <startdate>2011/08/11</startdate>   </specfields>  </item>  <item guid='np_108886'>   <title>Test event 1</title>   <description>lorem ipsum</description>   <specfields>    <city>dallas</city>    <startdate>2011/11/11</startdate>   </specfields>  </item>  <item guid='np_108886'>   <title>Test event 2</title>   <description>lorem ipsum</description>   <specfields>    <city>new york</city>    <startdate>2011/09/11</startdate>   </specfields>  </item>  <item guid='np_108886'>   <title>Test event 2</title>   <description>lorem ipsum</description>   <specfields>    <city>seattle</city>    <startdate>2011/09/11</startdate>   </specfields>  </item> </root>",  xmlDoc = $.parseXML(xml); xml = $(xmlDoc); 

    try 
    { 
    $(xml).find('[nodeName=item]').each(function(){ if ($(this).index() != 0) { 
     possitionTitle = $(this).prev(); 
    if ($(this).children('title').text() != possitionTitle.children('title').text()) { 
       $("div").append("</li><li>"+$(this).children('title').text()+" -- "+$(this).children("specfields").children("city").text()+" ("+$(this).children("specfields").children("startdate").text()+")"); 
       } else { 
        $("div").append($(this).children("specfields").children("city").text()+" ("+$(this).children("specfields").children("startdate").text()+")"); 
       } 
     } else { 
      $("div").append("Start<li>"+$(this).children('title').text()+" -- "+$(this).children("specfields").children("city").text()+" ("+$(this).children("specfields").children("startdate").text()+")"); 
     } 
     $("div").append("</li>"); 

    }); 

    } 
catch(err) 
    { 
    alert(err); 
    } 

}); 
<div></div> 
+0

謝謝尋求幫助,像魅力一樣工作。我知道我必須檢查標題,但無法得到我的頭在.prev()... –

+0

@ Terr Marder:讓我猜測一個學校項目?:P – John

+0

nope,在明天截止日期之前快速的最後一分鐘的項目變更。那些我們都非常喜歡的...... ;-) –

0

像這樣的事情壽LD工作(未經測試):

$(document).ready(function() 
{ 
    $.ajax({ 
    type: "GET", 
    url: "YOUR XML", 
    dataType: "xml", 
    success: parseXml 
    }); 
}); 

function parseXml(xml){ 
    $(xml).find("title").filter(function(){ 
       $(this).text() == "Test event 1"; 
     }).each(function(){ 
      $("#dataDiv").append($(this).find("city").text(); 
      $("#dataDiv").append("(" + $(this).find("startdate").text() + "),"); 
    }); 

    $(xml).find("title").filter(function(){ 
       $(this).text() == "Test event 2"; 
     }).each(function(){ 
      $("#dataDiv").append($(this).find("city").text(); 
      $("#dataDiv").append("(" + $(this).find("startdate").text() + "),"); 
    }); 

} 

其中#dataDiv#dataDiv2在頁面上的div只是測試賽1測試賽2在他們裏面。

0

希望這會有所幫助。我已經測試並創建了一個js小提琴:http://jsfiddle.net/xDqZK/

基本上,一旦您在javscript中有xml,您可以使用jquerys parseXML()函數將其獲取到jquery對象中。一旦進入對象,您可以使用$ .find()方法搜索不同的節點並在它們之間迭代。一個棘手的事情,如果你想獲得節點的價值,你需要使用.text()。我正在使用.val()和.hmtl(),它返回undefined。

var strXml ="<root><item guid=\"np_108886\"><title>Test event 1<\/title><description>lorem ipsum<\/description><specfields><city>oslo<\/city><startdate>2011\/08\/11<\/startdate><\/specfields><\/item><item guid=\"np_108886\"><title>Test event 1<\/title><description>lorem ipsum<\/description><specfields><city>dallas<\/city><startdate>2011\/11\/11<\/startdate><\/specfields><\/item><item guid=\"np_108886\"><title>Test event 2<\/title><description>lorem ipsum<\/description><specfields><city>new york<\/city><startdate>2011\/09\/11<\/startdate><\/specfields><\/item><item guid=\"np_108886\"><title>Test event 2<\/title><description>lorem ipsum<\/description><specfields><city>seattle<\/city><startdate>2011\/09\/11<\/startdate><\/specfields><\/item><\/root>"; 
var xml = $.parseXML(strXml); 

$("#test").html((parse(xml,"Test event 1"))); 

function parse(xml, strEvent) 
{ 
    var eventStr = "",$xml = $(xml); 
    $xml.find("item").each(function(){ 
     if($(this).find("title").text().toLowerCase() == strEvent.toLowerCase()){ 
      var childNode = $(this).find("specfields") 
      eventStr += childNode.find("city").text(); 
      eventStr += " (" + childNode.find("startdate").text() + ") "; 
     } 
    }); 
    return eventStr; 
} 

HTML

<div id="test></div>