victorhooi
victorhooi

Reputation: 17285

Python to "Convert" YAML into XML

1. Background

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

I have the XSD schema for these XML files. Small excerpt below:

<?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. Objective

Our aim is to make it easier for users to tweak or add to settings.

I'd like to offer a script that took a YAML file, and based on the XSD outputted a XML configuration (with default fallbacks for missing values, perhaps in a default.yaml file.).

We'd also need the ability to "append" a YAML to the XML - as in, a user could have a YAML which just defines their user account, and we'd import this to the existing list of user accounts in an existing XML file.

3. Approach

So far I'm using GenerateDS (http://www.rexx.com/~dkuhlman/generateDS.html) to generate a Python wrapper from the XSD.

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

The issue now is how do I go from a YAML to the XML?

Ideally, I'd like a generic loop that just ran over each value, and added it to the Python representation.

My initial thought was to use getattr(config_wrapper, "yaml_name") = yaml_value and iterate over each value in the YAML, then catch AttributeError for anything that wasn't in the XSD.

My first question - are any issues to this approach, or is there perhaps a more Pythonic/cleaner way of doing this? Is there a smarter way of tackling this question?

My second question is - with the above part of the 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 would tie into GenerateDS, or how I could make it generic enough to work reliable.

Cheers, Victor

Upvotes: 4

Views: 6600

Answers (1)

Gringo Suave
Gringo Suave

Reputation: 31890

I'd avoid the XSD, sounds like a lot of bureaucracy for little gain.

  1. Create YAML file with same hierarchy as the XML file.
  2. Load file with pyyaml
  3. Loop over result creating the equivalent tree in elementtree.
  4. Write tree to XML file
  5. Send XML to system, when it complains, fix YAML and return to step 2.

Upvotes: 1

Related Questions