2011-07-14 148 views
0

我無法解析使用jQuery這個XML文件:XML解析與jQuery

<?xml version="1.0" encoding="utf-8"?> 
<?mso-infoPathSolution name="urn:schemas-microsoft-com:office:infopath:BOT-Memos:-myXSD-2011-07-13T14-29-57" solutionVersion="1.0.0.27" productVersion="14.0.0.0" PIVersion="1.0.0.0" href="http://sharept03sb1/BOT/BOT%20Memos/Forms/template.xsn"?> 
<?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.3"?> 
<my:myFields xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:pc="http://schemas.microsoft.com/office/infopath/2007/PartnerControls" xmlns:ma="http://schemas.microsoft.com/office/2009/metadata/properties/metaAttributes" xmlns:d="http://schemas.microsoft.com/office/infopath/2009/WSSList/dataFields" xmlns:q="http://schemas.microsoft.com/office/infopath/2009/WSSList/queryFields" xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" xmlns:dms="http://schemas.microsoft.com/office/2009/documentManagement/types" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2011-07-13T14:29:57" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" xml:lang="en-US"> 
    <my:content> 
     <my:title>Hello</my:title> 
     <my:description><html xmlns="http://www.w3.org/1999/xhtml" xml:space="preserve"><div>How are you?</div></html></my:description> 
     <my:memo xsi:nil="true"></my:memo> 
     <my:group> 

     <pc:Person xmlns:pc="http://schemas.microsoft.com/office/infopath/2007/PartnerControls"><pc:DisplayName>Johnny Doe</pc:DisplayName><pc:AccountId>ADMIN\fakeadminuser</pc:AccountId><pc:AccountType>User</pc:AccountType></pc:Person></my:group> 
     <my:meetingDate></my:meetingDate> 
     <my:addAttachments> 
      <my:attachments> 
       <my:attachment xsi:nil="true"></my:attachment> 
      </my:attachments> 
     </my:addAttachments> 
     <my:upload></my:upload> 
    </my:content> 
</my:myFields> 

我想讀my:title節點,但我有沒有運氣。這是我用來解析它的特定行。

$.get('target.xml', function(data){ 
    alert($(data).find("my:memo").text()); 
}); 

任何想法?

回答

1

jQuery的可能不被識別的數據作爲XML的某些原因。另外,通過顯式檢查nodeName,通常可以更好地選擇名稱空間標記。試試這個:

$.get('target.xml', function(data) { 
    alert($(data).find("[nodeName=my:memo]").text()); 
}, "xml"); 

或者:

$.ajax({ 
    type: "GET", 
    url: "target.xml", 
    dataType: "xml", 
    success: function(data) { 
     alert($(data).find("[nodeName=my:memo]").text()); 
    } 
}); 
0

你正在尋找一個標籤,但標籤有這個在它:my:memo xsi:nil="true"

$.get('target.xml', function(data){ 
    alert($(data).find("my:memo xsi:nil=\"true\"").text()); 
}); 

有更好的方法來做到這一點, 我相信。