0

我一直在寻找这个例子,但无法让它工作。

当用户单击按钮时,我需要生成一个本地 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 文件?

4

1 回答 1

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()最后的我,以防你突然从任何地方收到警报

于 2012-10-22T19:02:30.907 回答