2012-10-22 63 views
0

我一直在尋找this示例,但無法使其工作。如何使用jQuery生成本地xml文件

我需要在用戶單擊按鈕時生成本地XML文件。

我需要建立這樣一個

<array> 
     <dict> 
      <key>files</key> 
      <array> 
       <dict> 
        <key>date</key> 
        <string>2012/09/09</string> 
        <key>name</key> 
        <string>acatBriefing.pdf</string> 
        <key>description</key> 
        <string>ACAT Briefing</string> 
       </dict> 
      </array> 
      <key>subject</key> 
      <string>FAE approved ACAT Designations</string> 
      <key>presenter</key> 
      <string>Rebecca T. King</string> 
      <key>time</key> 
      <string>2:00 - 2:05 PM</string> 
     </dict> 
</array> 

一個XML我想是這樣的:

function generateXML(){ 
    // Simple helper function creates a new element from a name, so you don't have to add the brackets etc. 
$.createElement = function(name) 
{ 
    return $('<'+name+' />'); 
}; 

// JQ plugin appends a new element created from 'name' to each matched element. 
$.fn.appendNewElement = function(name) 
{ 
    this.each(function(i) 
    { 
     $(this).append('<'+name+' />'); 
    }); 
    return this; 
} 

/* xml root element - because html() does not include the root element and we want to 
* include <report /> in the output. There may be a better way to do this. 
*/ 
var $root = $('<XMLDocument />'); 

$root.append 
(
    // one method of adding a basic structure 
$('<plist />').append 
    (
    $('<dict />').append 
    (
     $('<key />').text('subject') 
     $('<string />').text('September 21') 
     $('<key />').text('date') 
     $('<string />').text('FOB10 Room') 
     $('<key />').text('time') 
     $('<string />').text('2.00 pm - 5.00 pm') 
     $('<key />').text('briefings') 

     $('<array />').append 
      (
       $('<dict />').append 
        (
         $('<key />').text('files') 
         $('<array />').append 
          (
          $('<dict />').append 
           (
            $('<key />').text('date') 
           $('<string />').text('09/09/2012') 
            $('<key />').text('name') 
           $('<string />').text('acatBriefing.pdf') 
           $('<key />').text('description') 
            $('<string />').text('ACAT Briefing') 
     ) 
     ) 
      $('<key />').text('subject') 
      $('<string />').text('FAE approved ACAT Designations') 
       $('<key />').text('presenter') 
      $('<string />').text('Rebecca T. King') 
      $('<key />').text('time') 
       $('<string />').text('2.00 - 2.05 PM') 

     ) 
    ) 
    ) 
    ) 
); 


alert($root.html()); 
} 

我不能這樣做,我怎麼可以創建jQuery的本地XML文件?

+0

通過「本地」得到提醒,你在內存中的意思,或者保存爲實際本地文件? –

+0

我需要的是一個字符串或一個包含正確xml標籤的文本文件 –

+0

如果您只是想要一個字符串,我會跳過實際創建一個文檔並創建一個字符串。 –

回答

0

你的功能是正確的,你只是錯過了一些逗號;

編輯的代碼:

function generateXML(){ 
    // Simple helper function creates a new element from a name, so you don't have to add the brackets etc. 
    $.createElement = function(name) 
    { 
     return $('<'+name+' />'); 
    }; 

    // JQ plugin appends a new element created from 'name' to each matched element. 
    $.fn.appendNewElement = function(name) 
    { 
     this.each(function(i) 
     { 
      $(this).append('<'+name+' />'); 
     }); 
     return this; 
    } 

    /* xml root element - because html() does not include the root element and we want to 
     * include <report /> in the output. There may be a better way to do this. 
     */ 
    var $root = $('<XMLDocument />'); 

    $root.append 
    (
    // one method of adding a basic structure 
    $('<plist />').append(
    $('<dict />').append(
    $('<key />').text('subject'), 
    $('<string />').text('September 21'), 
    $('<key />').text('date'), 
    $('<string />').text('FOB10 Room'), 
    $('<key />').text('time'), 
    $('<string />').text('2.00 pm - 5.00 pm'), 
    $('<key />').text('briefings'), 

    $('<array />').append 
    (
    $('<dict />').append 
    (
    $('<key />').text('files'), 
    $('<array />').append 
    (
    $('<dict />').append 
    (
    $('<key />').text('date'), 
    $('<string />').text('09/09/2012'), 
    $('<key />').text('name'), 
    $('<string />').text('acatBriefing.pdf'), 
    $('<key />').text('description'), 
    $('<string />').text('ACAT Briefing') 
) 
), 
    $('<key />').text('subject'), 
    $('<string />').text('FAE approved ACAT Designations'), 
    $('<key />').text('presenter'), 
    $('<string />').text('Rebecca T. King'), 
    $('<key />').text('time'), 
    $('<string />').text('2.00 - 2.05 PM') 

) 
) 
) 
) 
); 


    alert($root.html()); 
} 
generateXML(); 

只要注意我generateXML()在情況到底有ü突然不知從哪兒