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 9101559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:09:42+00:00 2026-06-17T01:09:42+00:00

The following snippet of code converts a Javascript object into an XML string using

  • 0

The following snippet of code converts a Javascript object into an XML string using the createDocument and XMLSerializer APIs. The problem is that it generates different outputs on Chrome (23.0.1271.101) and Firefox (14.0.1) browsers.

var item = { _dto: {...} }; // the 'model' object
var xmlDto = $('<Column />'); // this is eventually serialized and sent to the server
var optionalTags = ['Abstract', 'Note', 'Size', 'Digits', 'Nullable', 'AutoUpdate', 'DataType'];

// convert badgerfish JSON back to XML.
// use XML because it is not possible serialize JSON and preserve key order.
var xmlDoc = document.implementation.createDocument("http://example.org/v1", "Column", null);
var root = xmlDoc.childNodes[0];
var nameTag = xmlDoc.createElement("Name");
nameTag.setAttribute('uuid', item._dto['Name']['@uuid']);
nameTag.textContent=item._dto['Name']['$'];
root.appendChild(nameTag);
optionalTags.map(function (tagName) {
    var tag = xmlDoc.createElement(tagName);
    tag.textContent=item._dto[tagName];
    if (item._dto.hasOwnProperty(tagName)) {
        tag.textContent=item._dto[tagName];
        root.appendChild(tag);
    }
});

var xmlStr = new XMLSerializer().serializeToString(xmlDoc);
xmlStr = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'+xmlStr;

On Chrome, the following desired/expected output is generated:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <Column xmlns="http://example.org/v1">
          <Name uuid="001b5cbe-bab7-4880-90b6-9e8f47f6e4af">FAILED_ID</Name>
          <Size>38</Size>
          <Digits>0</Digits>
          <Nullable>true</Nullable>
          <AutoUpdate>false</AutoUpdate>
          <DataType>NUMERIC</DataType>
    </Column>

But in Firefox, the generated output has the xmlns attribute inserted in every tag with an empty value:

     <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
     <Column xmlns="http://example.org/v1">
          <Name xmlns="" uuid="001b5cbe-bab7-4880-90b6-9e8f47f6e4af">FAILED_ID</Name>
          <Size xmlns="">38</Size>
          <Digits xmlns="">0</Digits>
          <Nullable xmlns="">true</Nullable>
          <AutoUpdate xmlns="">false</AutoUpdate>
          <DataType xmlns="">NUMERIC</DataType>
     </Column>

It looks like the XMLSerializer in Firefox and Chrome have subtle differences but I need to verify this. In any case, the Firefox output is invalid XML. Can somebody shed some light on this?

Is there a better way to generate an XML document in the browser?

I wouldn’t be doing this if in the first place I had a way to serialize to JSON with the ability to preserve key order.

  • 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-06-17T01:09:43+00:00Added an answer on June 17, 2026 at 1:09 am

    The Firefox serialization is correct. When you create those elements with createElement, not createElementNS, that puts them in the null namespace (you can check by examining the namespaceURI of the elements after you create them). However parsing the serialization Chrome produces would put them in the http://example.org/v1 namespace.

    And yes, the Firefox output is valid XML. What made you think it’s not valid?

    I filed https://bugs.webkit.org/show_bug.cgi?id=106531

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing an application that converts an ajax xml response into html using
I have the following code snippet that converts a const char * to a
I have the following snippet of code (I'm using jQuery 1.4.2): $.post('/Ads/GetAdStatsRow/', { 'ad_id':
I have tried to use the following snippet of code: int main() { string
Never thought I'd have this problem :) The following snippet of code works in
I am trying to convert the following Java code snippet into Python. Can someone
Following is the code snippet that I have: // Make Auto release pool NSAutoreleasePool
I have this following snippet from c++ code that is used for encryption: EVP_CIPHER_CTX
I just tried the following code snippet for shellcode testing purposes:- #include<iostream> using namespace
I have the following C# code snippet string[] lines = File.ReadAllLines(@C:\test.txt); for(int i=0; i<lines.Length;

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.