Python Forum

Full Version: XML Parsing in python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
HI All,

I have an input file sample like below

 <file="excel.1ew9u4t0tggb9315darmm0nfz2kb"
  name ="Custom SQL Query" type="text">SELECT [EMP$].[Category] AS [Category],&amp;#13,&amp;#10, 
  [EMP$].[A] AS [A],&amp;#13,&amp;#10, 
  [EMP$].[B] AS [B],&amp;#13,&amp;#10, 
  [EMP$].[C] AS [C],&amp;#13,&amp;#10, 
  [EMP$].[D AS [D,&amp;#13,&amp;#10, 
  [Dept$].[E] AS [E],&amp;#13,&amp;#10,
  [Dept$].[F] AS [F (Dept)]&amp;#13,&amp;#10,FROM [EMP$]&amp;#13,&amp;#10,  
  INNER JOIN [Dept$] ON [EMP$].[F] = [Dept$].[F]
  </file>
I have to break the SQL and create an output xml like below

  <records class="column">
    <remote-name>A</remote-name>
    <remotetype>130</remotetype>
    <local-name>[A]</local-name>
    <parent-name>[Custom SQL Query]</parent-name>
    <remote-alias>A</remote-alias>
    <ordinal>1</ordinal>
    <local-type>string</local-type>
    <aggregation>Count</aggregation>
    <contains-null>true</contains-null>
    <collation-flag>1</collation-flag>
    <attributes>
      <attribute datatype="string" name="DebugRemoteType">WSTR</attribute>
    </attributes>
  </records>
  
  
  <records class="column">
    <remote-name>B</remote-name>
    <remotetype>130</remotetype>
    <local-name>[B]</local-name>
    <parent-name>[Custom SQL Query]</parent-name>
    <remote-alias>B</remote-alias>
    <ordinal>2</ordinal>
    <local-type>string</local-type>
    <aggregation>Count</aggregation>
    <contains-null>true</contains-null>
    <collation-flag>1</collation-flag>
    <attributes>
      <attribute datatype="string" name="DebugRemoteType">WSTR</attribute>
    </attributes>
  </records>

and so on for c , d etc
Can someone help ? As im new to the xml and Im also trying but not able to get the desired answer.

Thanks,
Aarush
Hello,
it will take an effort on your part and showing your attempt, before anyone of us can help you correct the mistakes or guide you towards your desired solution.
Python fortunately has great built in XML modules, which is a good place for you to start exploring:
https://docs.python.org/3/library/xml.html
As well as this overview of XML modules:
https://wiki.python.org/moin/PythonXml