Convert SVG to XML with Base64 Encoding

XML is a text-based format, and while it’s excellent for structuring data, it doesn’t handle binary data (like images) very well. SVG is an XML-based format, so it might seem unnecessary to encode it into Base64 and save it as XML. However, SVG can contain special characters and other non-text content. Therefore, encoding SVG in Base64 avoids potential issues with embedding SVG in XML and the integrity of the overall XML structure. This ensures that the image data stays safe and doesn’t get messed up when you move it around or store it.


How to convert SVG to XML in Python

In order to convert SVG to XML, we use Aspose.SVG for Python via .NET API, which is a feature-rich, powerful, easy-to-use document manipulation API for the Python platform. We consider the example of SVG to Base64 encoding and embedding Base64 string into XML document:


Python code to convert SVG to XML

import base64
import os
from aspose.svg import *
from xml.etree.ElementTree import *

# Create a function to convert SVG to XML with Base64-encoded SVG data
def convert_svg_to_xml(svg_path, xml_path):
    # Load an SVG document
    document = SVGDocument(svg_path)

    # Temporary file path to save the SVG content
    temp_svg_path = "temp_output.svg"

    try:
        # Save the SVG content to a temporary file
        document.save(temp_svg_path)

        # Read the content from the temporary file
        with open(temp_svg_path, "r", encoding="utf-8") as svg_file:
            svg_content = svg_file.read()

        # Encode the SVG content in Base64
        encoded_svg = base64.b64encode(svg_content.encode("utf-8")).decode("utf-8")

        # Create XML structure
        root = Element("root")
        image = SubElement(root, "image", mime="image/svg+xml")
        image.text = encoded_svg

        # Write the XML to the file
        tree = ElementTree(root)
        tree.write(xml_path, encoding="utf-8", xml_declaration=True)

    finally:
        # Clean up the temporary file
        if os.path.exists(temp_svg_path):
            os.remove(temp_svg_path)

# Example usage
input_svg = "image.svg"
output_xml = "image-base64.xml"

convert_svg_to_xml(input_svg, output_xml)



Steps to Convert SVG to XML in Python

  1. Use the SVGDocument class to load the SVG content from the specified file path.
  2. Save the loaded SVG document to a temporary file. This step facilitates reading the SVG content as a string for encoding.
  3. Open the temporary SVG file and read its content into a string. Convert this string into Base64 encoding to prepare it for embedding in XML.
  4. Create an XML object that includes the MIME type and the Base64-encoded SVG data. Structure the XML as required, with appropriate fields for the image data.
  5. Save the constructed XML object to the specified file path. This XML file will now contain the Base64-encoded SVG data.
  6. Remove the temporary SVG file to ensure no unnecessary files are left on the system.

The XML object typically includes the MIME type of an image and encoded data, like so:

XML code with embedded Base64 SVG image

<?xml version="1.0" encoding="UTF-8"?>
<root>
<image mime = "image/svg+xml">base64_string...</image>
</root>



Online Base64 Encoders

Aspose.SVG offers free online applications for encodind and decoding binary data:

  • Base64 Encoders are a set of tools that allow you to encode binary data in various output formats: Plain Base64, JSON, XML, URI, or CSS.
  • Image Base64 Decoder converts a data URI containing a Base64 string to an image by pasting a URI string in the input control.

Our browser-based applications work from all platforms, including Windows, Linux, Mac OS, Android and iOS. No registration, plugin or software installation required for you. Start using our online Base64 encoding/decoding tools safely, secure, and easy way!

FAQ

1. How can I convert SVG to XML?

Aspose.SVG allows you to convert SVG to XML in any way – online or programmatically. For example, you can encode SVG in real-time using Online SVG Encoder that converts your files quickly and with high quality. Upload, encode SVG and get the result in a few seconds! On the other hand, you can use Aspose.SVG for Python via .NET API to convert SVG to XML programmatically.

2. Why encode SVG Files?

Historically, many data transfer and storage formats, such as HTML, XML, and email, rely on text rather than binary code. When binary data, for example, in SVG files need to be embedded in these text-based formats, and Base64 encoding becomes necessary. Encoding SVG files to Base64 allows them to be safely embedded within XML, JSON, CSS, and URI schemes without modification during transport. This approach simplifies integration, enhances portability, and improves load times by reducing HTTP requests.

3. What is Base64 encoding?

Base64 encoding is a method for converting binary data into a text format using a set of 64 ASCII characters that are safe for use in text-based formats like HTML, XML, or email. This encoding ensures that binary data, such as images or files, can be embedded, transmitted, or stored in systems that only handle text without data corruption. Base64 is commonly used for embedding images in web pages, sending files via email, and storing data in text-based formats.

4. Can Base64 encoding affect the quality of an SVG image?

No, Base64 encoding does not affect the quality of the SVG image. Base64 encoding is a way to represent binary data as text, preserving the SVG file’s original quality. The encoding process simply converts the SVG file into a text format suitable for embedding in various text formats such as JSON, XML, or CSS.

Get Started with Python API

If you want to develop scalable vector graphics and their applications, install our flexible, high-speed Aspose.SVG for Python via .NET API. pip is the easiest way to download and install Aspose.SVG for Python via .NET API. To do this, run the following command:

pip install aspose-svg-net

For more details about Python library installation and system requirements, please refer to Aspose.SVG Documentation.

Other Supported Base64 Encoders

You can convert SVG to Base64 string and save it as JSON, XML, and CSS:

Image to Base64 (Binary-to-text encoding)
SVG to JSON (JavaScript Object Notation)
SVG to XML (Extensible Markup Language)
SVG to CSS (Cascading Style Sheets)