Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
XML Parsing in python
#1
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
Reply
#2
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
Reply
#3
As well as this overview of XML modules:
https://wiki.python.org/moin/PythonXml
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Parsing link from html tags with Python Melcu54 0 1,616 Jun-14-2021, 09:25 AM
Last Post: Melcu54
  Is there a better way? Python parsing tlewick1 1 1,760 Oct-17-2020, 05:48 PM
Last Post: bowlofred
  python realtime parsing logs anna 2 2,856 Jul-05-2020, 06:36 AM
Last Post: anna
  Parsing Text file having repeated value key pair using python manussnair 3 3,291 Aug-04-2018, 11:48 PM
Last Post: micseydel
  Python file parsing, can't catch strings in new line Philia 5 3,970 Apr-29-2018, 01:09 PM
Last Post: snippsat
  Parsing csv using python Firstname_Lastname 4 3,946 Sep-03-2017, 06:26 PM
Last Post: Larz60+
  Text file parsing with python and with a list in grammar pitanga 2 3,237 Aug-31-2017, 02:21 PM
Last Post: pitanga

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020