Coder Social home page Coder Social logo

create_nested_xml's Introduction

XML Creator

This is a Python script that creates an XML document from a dictionary.

Usage

To use this script, follow these steps:

1. Import the necessary modules:

  • import xml.etree.ElementTree as ET
  • import xml.dom.minidom

2. Define the create_xml_element function:

def create_xml_element(name, value):
   element = ET.Element(name)
   if value:
      element.text = value
      return element

This function creates an XML element with the given name and value.

3. Define the create_nested_xml function:

def create_nested_xml(data: dict, root=None):
  if root is None:
  root = ET.Element("Assets")
  for key, value in data.items():
    if isinstance(value, dict):
      sub_element = ET.Element(key.split("")[0])
      create_nested_xml(value, sub_element)
      root.append(sub_element)
    else:
      element = create_xml_element(key.split("")[0], str(value))
      root.append(element)
      return root

This function creates a nested XML document from a dictionary. It calls the create_xml_element function to create the XML elements.

4. Call the create_nested_xml function with your dictionary to create the XML document:

data = {
"asset_name": "My Asset",
"description": "This is a test asset",
"data": {
"data_type": "text",
"data_value": "Hello, world!"
}
}

xml_tree = ET.ElementTree(create_nested_xml(data))
xml_string = ET.tostring(xml_tree.getroot(), encoding="utf-8", method="xml")
pretty_xml_string = xml.dom.minidom.parseString(xml_string).toprettyxml(encoding="utf-8")
print(pretty_xml_string)

This will create an XML string from the data dictionary in pretty format and print it to the console.

Contributing

If you would like to contribute to this project, please follow these steps:

  1. Fork the repository
  2. Create a new branch
  3. Make your changes
  4. Submit a pull request

create_nested_xml's People

Contributors

akshaysharma17 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.