views:

46

answers:

1

I'm trying to generate a XML from client side XML according to this link. But it isn't work. Here is my js code:

// XML Builder
$.createElement = function(name)
{
    return $('<'+name+' />');
};

$.fn.appendNewElement = function(name)
{
    this.each(function(i)
    {
        $(this).append('<'+name+' />');
    });
    return this;
}

generateXMLFromXML(1);

function generateXMLFromXML(id)
{
    var $root = $('<?xml version="1.0" encoding="utf-8" ?>');

    $root.append(
        $('row').append (
                $('page').text(1)
        )
    );
    console.log($.isXMLDoc($root)); // return false
    $root.find('page').each(function(){
        console.log($(this).text());
    });
}

I'm hoping jQuery can help me out. And any idea?

+1  A: 

You will want to use the DOM instead of jQuery for making the XML document. The best way to create a document is document.implementation.createDocument(null, null, null). If you're just trying to parse a string to an XML document object, look into DOMParser (specifically, (new DOMParser).parseFromString(xmlDocString, "application/xml")).

Eli Grey
Thank you very much. You save my life.
Zeck
Mind accepting my answer?
Eli Grey