I'm writing code that creates configuration files for our program. The configuration files are in an XML format.
I'm using Python's xml.dom.minidom module to parse the XML file. I want to be able to break up the configuration file into 2 or more smaller XML files.
Say if I have a file called main_config.xml:
<configuration>
<server name="my_servername" ip="10.10.10.10">
<disk>
<volume>/dev/sdc1</volume>
</disk>
</server>
<server>
<redirect loc="./child.xml"
</server>
<server name="server3" ip="10.10.10.13">
<disk>
<volume>/dev/sdf1</volume>
</disk>
</server>
And the <"redirect"> element has a "loc" attribute that points to a file named "child.xml"
And the file "child.xml" has this
<server name="server2" ip="10.10.10.12">
<disk>
<volume>/dev/sde1</volume>
</disk>
</server>
{Note: these are small simple configuration files. The ones I work with are much, much much longer (5000 lines or so) and are hard to edit, hence, the proposal to break up the configuration file into several smaller, more modular ones so it's much easier to edit}
What I want to do, with the xml.dom.minidom, to
1) Read in the XML document in main_config.xml
2) Parse the XML document from main_config.xml
3) If I see a <"redirect"> element, go to the file that the "loc" attribute is point to
4) Read in the XML document from "child.xml"
5) Replace the <"server"> element with the child <"redirect"> element in the main_config.xml with the <"server"> element from child.xml so that the XML document looks like this below:
<configuration>
<server name="my_servername" ip="10.10.10.10">
<disk>
<volume>/dev/sdc1</volume>
</disk>
</server>
<server name="server2" ip="10.10.10.12">
<disk>
<volume>/dev/sde1</volume>
</disk>
</server>
<server name="server3" ip="10.10.10.13">
<disk>
<volume>/dev/sdf1</volume>
</disk>
</server>
Using xml.dom.minidom, I already can do steps 1 thru 4. However, I am stuck in Step 5 because the <"server"> element in main_config.xml is considered a nodeType of type ELEMENT_NODE but the <"server"> element from child.xml is considered a DOCUMENT_TYPE_NODE. So therefore, I cannot use the node.replaceChild() call because xml.dom.minidom complains that you can't put an XML document as a child to <"configuration">.
There is one way I could go about doing this, which is to walk thru the XML tree in the XML doc from child.xml file, delete the <"server"> element from the XML doc from main_config.xml, then create a new <"server"> element in the main_config.xml with all the nodes/attributes from the child.xml file. But I'd rather not do that unless it's the last resort.
Is there any other way where I can replace an ELEMENT_NODE with a DOCUMENT_TYPE_NODE? Is there a way to change the nodeType of a node object so replaceChild() works? (The xml.dom.minidom says that the nodeType is read-only though).