jquery .append() case sensitive element

前端 未结 4 678
北恋
北恋 2020-12-19 14:39

Hi I need to create xml from data in form to send it to webservice. The problem is that .append() is case insensitive, so .append(\'\')

相关标签:
4条回答
  • 2020-12-19 14:47

    Try using $.parseXML() to create the XML element:

    yourObject.append($.parseXML("<EDO />").documentElement);
    
    0 讨论(0)
  • 2020-12-19 14:53

    jQuery.parseXML will always create a new DOMParse and a new Document, so it is pretty heavy.

    A better approach would be to use the (unintuitive) parseHTML, using the context paramter:

    // Create the context XML document; doc and $doc is reusable
    var doc = (new DOMParser()).parseFromString( '<root/>', 'text/xml' ); 
    var $doc = $( doc.documentElement )
    
    // Create case-sensitive XML element;
    // this will call doc.createElement( 'EDO' ), as of jQuery 2.1.3
    $doc.append( $.parseHTML( '<EDO />', doc ) ); 
    
    0 讨论(0)
  • 2020-12-19 15:03

    Finally as @Frédéric Hamidi said, to make case sensitive xml I used these functions:

    var domA = $.parseXML("<EDO_A />").documentElement; to create element

    $(domA ).append($.parseXML('<EDO_Child />').documentElement); to add child from string

    $(domA).append(domB) or domA.appendChild(domB) to add child object

    0 讨论(0)
  • 2020-12-19 15:11

    Notes:

    $.parseHTML("<AddPerson>Adel</AddPerson>"); //result: <addperson>Adel</addperson>
    
    $.parseXML("<AddPerson>Adel</AddPerson>"); //result: <AddPerson xmlns="">Adel</AddPerson>
    

    I guess that you need these lines:

    var request = $.parseXML(HtmlOrXmlStringSource);
    $(request).find(someElementInside).append(HtmlOrXmlStringSource_2);
    
    //Serialize XML
    var oSerializer = new XMLSerializer();
    request = oSerializer.serializeToString(request);
    request = request.replace(new RegExp(' xmlns=""', "igm"), "");
    

    Now the request is ready to be sent through $.ajax();

    Thanks

    0 讨论(0)
提交回复
热议问题