Python to convert YAML to XML

1. Background

We have an application that depends on several XML configuration files. XML files define things like connection settings, polling rates, multiple user accounts (using nested XSD types / sequences), etc.

I have an XSD schema for these XML files. Low shutter speed:

<?xml version="1.0" encoding="ISO-8859-1"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="FooExch" type="CConfigFooType"/> <xs:complexType name="CConfigFooType"> <xs:sequence> <xs:element name="_appID" type="xs:string"/> ... <xs:element name="_logins" type="FooLoginsType" maxOccurs="unbounded"/> <xs:element name="_passwords" type="FooPasswordType" maxOccurs="unbounded"/> ... </xs:sequence> </xs:complexType> <xs:complexType name="FooLoginsType"> <xs:sequence> <xs:element name="_name" type="xs:string"/> <xs:element name="_adapterID" type="xs:int"/> <xs:element name="_FooLogins" type="FooAccountType" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> <xs:complexType name="FooAccountType"> <xs:sequence> <xs:element name="_FooAccount" type="xs:string"/> <xs:element name="_mktFeed" type="xs:int"/> </xs:sequence> </xs:complexType> <xs:complexType name="FooPasswordType"> <xs:sequence> <xs:element name="_name" type="xs:string"/> <xs:element name="_password" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:schema> 

2. Purpose

Our goal is to make it easy for users to configure or add settings.

I would like to suggest a script that took the YAML file, and based on the XSD output the XML configuration (with default backup errors for missing values, possibly in the default.yaml file.).

We also need the ability to β€œadd” YAML to XML - as in the case, the user may have YAML, which simply defines their user account, and we import this into the existing list of user accounts in an existing XML file.

3. Approach

So far, I am using GenerateDS (http://www.rexx.com/~dkuhlman/generateDS.html) to create a Python shell from XSD.

Using this, I can create a Python object representing the XML schema, and then export them to XML.

Now the problem is how to switch from YAML to XML?

Ideally, I would like the common loop that just ran through each value to add it to the Python view.

My initial thought was to use getattr(config_wrapper, "yaml_name") = yaml_value and repeat each value in YAML and then catch AttributeError for everything that was not in the XSD.

My first question is, are there any questions about this approach, or maybe there is a more Pythonic / clean way to do this? Is there a smarter way to resolve this issue?

My second question is with the above part of XSD, we need to have nested logins with username / passwords. I know PyYAML offers nested collections, but I'm still not quite sure how this will be related to GenerateDS or how I can make it generalized enough for reliable operation.

Cheers, Victor

+4
source share
1 answer

I would avoid XSD, it sounds like a lot of bureaucracy for a small gain.

  • Create a YAML file with the same hierarchy as the XML file.
  • Download file using pyyaml
  • Complete the result by creating an equivalent tree in elementtree.
  • Writing a tree to an XML file
  • Submit the XML to the system when it complains, fix the YAML and go back to step 2.
+1
source

Source: https://habr.com/ru/post/1345647/


All Articles