XML to BSON Converter

Converts XML to JSON, then BSON-encodes that document (doubles, strings, embedded documents, arrays, booleans, null, and integers, per the BSON element type codes), shown as hex, reusing this site's existing JSON to BSON Converter. A free online tool from Staaarter, right in your browser.

Runs locallyUpdated 2026-07-23

Overview

Introduction

MongoDB stores and transmits documents as BSON, not JSON.

This tool shows what an XML document looks like encoded that way.

What Is XML to BSON Converter?

A converter that turns XML into JSON.

It then reuses this site's existing JSON to BSON Converter to encode that JSON as BSON bytes, shown as hex.

How XML to BSON Converter Works

XML is converted to JSON the same way the XML to JSON Converter does it.

Each key/value pair is then encoded with a BSON type byte and length-prefixed content: numbers become int32, int64, or double depending on their value, and strings, booleans, null, nested objects, and arrays each get their own BSON element type.

When To Use XML to BSON Converter

Use it to see what an XML document would look like as a MongoDB-style BSON document.

It's also useful when working with tooling that expects BSON input.

Features

Advantages

  • Reuses the existing, tested JSON to BSON Converter rather than duplicating encoding logic.
  • Runs entirely client-side.
  • Automatically picks the right numeric BSON type (int32, int64, or double) for each value.

Limitations

  • Covers a practical subset of BSON types (see the FAQ), not MongoDB-specific types like ObjectId or Date.
  • 64-bit integers are approximated using JS's double-precision numbers, adequate for realistic data but not bit-exact for the full 64-bit integer range.

Examples

Encoding a small document

Input

<user><name>Ada</name><age>36</age></user>

Output

2a 00 00 00 03 75 73 65 72 00 1f 00 00 00 02 6e 61 6d 65 00 04 00 00 00 41 64 61 00 02 61 67 65 00 03 00 00 00 33 36 00 00 00

The XML structure becomes a nested BSON document, with 'name' as a BSON string and 'age' as a BSON int32.

Best Practices & Notes

Best Practices

  • Use BSON to XML Converter to check what a round trip does to your specific data.
  • Reach for XML to BXML Converter instead if you specifically need to preserve XML's attribute/element distinction exactly.
  • Treat this as a way to inspect BSON encoding, not as a substitute for a real MongoDB driver.

Developer Notes

This is a thin wrapper composing `xmlToJson` with the existing `jsonToBson` encoder in the JSON category; no BSON-encoding logic is duplicated here.

XML to BSON Converter Use Cases

  • Seeing what an XML document looks like as a MongoDB-style BSON document
  • Working with tooling that expects BSON input
  • Teaching or demonstrating BSON's element-type encoding using a familiar XML document

Common Mistakes

  • Expecting MongoDB-specific types like ObjectId or Date, they aren't produced by this converter.
  • Assuming the hex output is directly usable as a MongoDB driver's binary payload without further handling.

Tips

  • Use XML to BXML Converter instead if you want a binary encoding that preserves XML's attribute/element distinction exactly.
  • Check the FAQ above if the output doesn't look like what you expected.

References

Frequently Asked Questions