Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 890219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:41:45+00:00 2026-05-15T13:41:45+00:00

I am working on a Web application that needs to send XML to a

  • 0

I am working on a Web application that needs to send XML to a server backend. I’d like to build a XML document in-memory on the client-side, but using XML manipulation routines, instead of appending countless strings together. I’m hoping jQuery can help me out.

Let’s say I need to generate this (toy) XML document with JavaScript:

<report>
    <submitter>
        <name>John Doe</name>
    </submitter>
    <students>
        <student>
            <name>Alice</name>
            <grade>80</grade>
        </student>
        <student>
            <name>Bob</name>
            <grade>90</grade>
        </student>
    </students>
</report>

To start, I need to create some kind of an XML document object with the “report” root. I’m assuming one of these should be close, but none of them work quite right, and/or I can’t quite figure out how to use the object properly:

function generateDocument1()
{
    var report = $('<report></report>');
    return report;
}

function generateDocument2()
{
    var report = document.implementation.createDocument(null, "report", null);

    return new XMLSerializer().serializeToString(report);   
}

function createXmlDocument(string)
{
    var doc;
    if (window.DOMParser)
    {
        parser = new DOMParser();
        doc = parser.parseFromString(string, "application/xml");
    }
    else // Internet Explorer
    {
        doc = new ActiveXObject("Microsoft.XMLDOM");
        doc.async = "false";
        doc.loadXML(string); 
    }
    return doc;
}

function generateDocument3()
{
    var report = createXmlDocument('<report></report>');

    return report;
}

Now I want to create and append elements. How do I do that? I imagine it’s something like this:

function generateReportXml()
{
    // Somehow generate the XML document object with root
    var report = /*???*/;

    // Somehow create the XML nodes
    var submitter = /*???*/;
    var name = /*???*/;

    // Somehow append name to submitter, and submitter to report
    submitter.append(name); /*???*/
    report.append(submitter); /*???*/

    // ... append the rest of the XML

    return report;
}

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T13:41:46+00:00Added an answer on May 15, 2026 at 1:41 pm

    Without addressing whether you should use jQuery to build XML, here are some ideas on how you might do it:

    // 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
        $('<report />').append
        (
            $('<submitter />').append
            (
                $('<name />').text('John Doe')
            )
        )
        // example of our plugin
        .appendNewElement('students')
    );
    
    // get a reference to report
    var $report = $root.find('report');
    
    // get a reference to students
    var $students = $report.find('students');
    // or find students from the $root like this: $root.find('report>students');
    
    // create 'Alice'
    var $newStudent = $.createElement('student');
    // add 'name' element using standard jQuery
    $newStudent.append($('<name />').text('Alice'));
    // add 'grade' element using our helper
    $newStudent.append($.createElement('grade').text('80'));
    
    // add 'Alice' to <students />
    $students.append($newStudent);
    
    // create 'Bob'
    $newStudent = $.createElement('student');
    $newStudent.append($('<name />').text('Bob'));
    $newStudent.append($.createElement('grade').text('90'));
    
    // add 'Bob' to <students />
    $students.append($newStudent);
    
    // display the markup as text
    alert($root.html());
    

    Output:

    <report>
        <submitter>
            <name>John Doe</name>
        </submitter>
        <students>
            <student>
                <name>Alice</name>
                <grade>80</grade>
            </student>
            <student>
                <name>Bob</name>
                <grade>90</grade>
            </student>
        </students>
    </report>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a web application that needs to frequently poll the server database
I have an asp.net Web Forms application that needs to send emails. I like
I'm working on an application that needs to get the source of a web
I currently am working on a web application that needs to collect data from
I am working on a web forms application that needs to support multiple languages
I'm working on an in-house project management web based application that needs to support
i am currently working on a web application that needs to accept video uploaded
I'm working on an web application that needs a demo/example version, so that potential
Hi i am working on web service application in android.i need a class that
I am working on web application that will use PHP & MySQL. Application will

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.