0

I have been looking to this example but could not make it work.

I need to generate a local XML file when user clicks a button.

I need to create an xml like this one

<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>

I tried something like:

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());
}

I could not do it, how can I create a local XML file with jQuery?

3
  • By "local", do you mean in-memory, or saved as an actual local file? Commented Oct 22, 2012 at 18:33
  • all I need is a string or a text file that contains correct xml tags Commented Oct 22, 2012 at 18:34
  • If you simply want a string, i would skip actually creating a document and create a string. Commented Oct 22, 2012 at 18:39

1 Answer 1

0

your function was correct, u just missed putting some commas;

edited code:

    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();

just note my generateXML() at the end there in case u suddenly get alerts from nowhere

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.