如何使用jQuery生成本地xml文件

时间:2012-10-22 18:26:31

标签: jquery html xml

我一直在寻找this示例,但无法使其发挥作用。

我需要在用户单击按钮时生成本地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>

我尝试过类似的事情:

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文件?

1 个答案:

答案 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(),以防万一突然收到警报