Search code examples
pythonxml-parsinglxmlelementtreeminidom

Map xml file with extracted information


I am trying to map one xml file to another, based on a configuration file (that too can be an xml file).

Input

<ia>
    <ib>...</ib>
    <ic>...</ic>
</ia>

Output

<oa>
    <ob>...</ob>
    <oc>...</oc>
</oa>

Config

<config>
    <conf>
        <input>ia</input>
        <output>oa</output>
    </conf>
    <conf>
        <input>ib</input>
        <output>ob</output>
    </conf>
    .....
</config>

So, the intention is to parse an xml file and retrieve the information interesting to me, and write into another xml file where the mapping information is specified in the config file.

Due to the scripting nature (and extending with plugins lateron), and the support for xml processing I was considering python. I just learned the syntax and basics of language, and came to know about lxml

One way of doing this

  1. parse the config file (where , tag can have xpath to the node I am interested in)
  2. read the input file
  3. write into output, using etbuilder based on the config file

Being new to python, and not seeing xpath support for etbuilder I wonder is this the best approach. Also not sure all the exceptional cases. Is there an easier way, or native support in any other libraries. If possible, I do not want to spend too much time on this task as I could focus on the core task.

thanks ins advance.


Solution

  • If you wish to transform an XML file into another XML file then XSLT was made for this purpose. You have to define a .xslt file that describes the transformation of XML content and what the eventual output should look like. That's one way to do it.

    You can also read the XML file using lxml and generate the output XML with lxml.etree.ElementTree. I'm not familiar with etbuilder but I don't think generating the desired output is that difficult. Once you have parsed the input files, you can build the config XML and write it to a file.

    XPath is primarily for reading XML content, you don't need it for constructing XML files. In fact, if you use a proper XML parser then you don't need XPath either to read the file contents, although XPath could make life a bit easier.