title: Python to "Convert" YAML into XML
body: <p><strong>1. Background</strong></p>
  
  <p>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.</p>
  
  <p>I have the XSD schema for these XML files. Small excerpt below:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
  &lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
  &lt;xs:element name="FooExch"  type="CConfigFooType"/&gt;
  
  &lt;xs:complexType name="CConfigFooType"&gt;
    &lt;xs:sequence&gt;
      &lt;xs:element name="_appID" type="xs:string"/&gt;
      ...
      &lt;xs:element name="_logins" type="FooLoginsType" maxOccurs="unbounded"/&gt;
      &lt;xs:element name="_passwords" type="FooPasswordType" maxOccurs="unbounded"/&gt;
      ...
    &lt;/xs:sequence&gt;
  &lt;/xs:complexType&gt;
  
  &lt;xs:complexType name="FooLoginsType"&gt;
    &lt;xs:sequence&gt;
      &lt;xs:element name="_name" type="xs:string"/&gt;
      &lt;xs:element name="_adapterID" type="xs:int"/&gt;
      &lt;xs:element name="_FooLogins" type="FooAccountType" maxOccurs="unbounded"/&gt;
    &lt;/xs:sequence&gt;
  &lt;/xs:complexType&gt;
  
  &lt;xs:complexType name="FooAccountType"&gt;
    &lt;xs:sequence&gt;
      &lt;xs:element name="_FooAccount" type="xs:string"/&gt;
      &lt;xs:element name="_mktFeed" type="xs:int"/&gt;
    &lt;/xs:sequence&gt;
  &lt;/xs:complexType&gt;
  
  &lt;xs:complexType name="FooPasswordType"&gt;
    &lt;xs:sequence&gt;
      &lt;xs:element name="_name" type="xs:string"/&gt;
      &lt;xs:element name="_password" type="xs:string"/&gt;
    &lt;/xs:sequence&gt;
  &lt;/xs:complexType&gt;
  
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p><strong>2. Objective</strong></p>
  
  <p>Our aim is to make it easier for users to tweak or add to settings.</p>
  
  <p>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 <code>default.yaml</code> file.).</p>
  
  <p>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.</p>
  
  <p><strong>3. Approach</strong></p>
  
  <p>So far I'm using GenerateDS (http://www.rexx.com/~dkuhlman/generateDS.html) to generate a Python wrapper from the XSD.</p>
  
  <p>Using that, I can create a Python object representing the XML schema, then export them to XML.</p>
  
  <p>The issue now is how do I go from a YAML to the XML?</p>
  
  <p>Ideally, I'd like a generic loop that just ran over each value, and added it to the Python representation.</p>
  
  <p>My initial thought was to use <code>getattr(config_wrapper, "yaml_name") = yaml_value</code> and iterate over each value in the YAML, then catch <code>AttributeError</code> for anything that wasn't in the XSD.</p>
  
  <p>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?</p>
  
  <p>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.</p>
  
  <p>Cheers,
  Victor</p>