简体   繁体   中英

How to generate a XML from XML in jQuery (Javascript)?

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

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") ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM