Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Parse XML with Namespaces
#1
Hello All -

I am trying to parse some xml files that are saturated with Namespaces. I have been able to parse XML files without namespaces. I have found several articles online on parsing with Namespaces, but the namespaces in my file don't seem like most of the examples I have found online.

Below is an example of the XML. I am trying to get to the ConnectionString property.

I have tried using findall with the full hierarchy like
conn_mgrs = root.findall('ConnectionManagers/ConnectionManager/ObjectData/ConnectionManager')
And, also using the namespace argument like:
ns = {'XYZ': 'www.example.com/myExample/Xyz'}
conn_mgrs = root.findall('ConnectionManagers/ConnectionManager/ObjectData/ConnectionManager', ns)
Both just return a null element.

My next move is to probably strip out the namespace prefixes and then parse the file, but figured i'd check with others to see if someone knows a way to resolve.

Thanks for any help

The XML looks like this:

<XYZ:Executable xmlns:XYZ="www.example.com/myExample/Xyz"
  XYZ:Id="Package"
  XYZ:CreationDate="2/21/2018 11:11:48 AM"
  XYZ:XYZID="{FB8BE06B-76B6-44DA-B2C7-043BD0989CBF}"
  XYZ:ObjectName="MyTestProject"
  XYZ:VersionGUID="{8D9F7CDA-590E-44C3-8896-786D27167F7D}">
  <XYZ:Property
    XYZ:Name="PackageFormatVersion">6</XYZ:Property>
  <XYZ:ConnectionManagers>
    <XYZ:ConnectionManager
      XYZ:refId="Package.ConnectionManagers[RTG093939BB.AdminDB]"
      XYZ:CreationName="OLEDB"
      XYZ:XYZID="{C67B6283-781F-4B0E-A9A7-376A157B6F16}"
      XYZ:ObjectName="RTG093939BB.AdminDB">
      <XYZ:ObjectData>
        <XYZ:ConnectionManager
          XYZ:ConnectionString="Data Source=RTG093939BB;Initial Catalog=AdminDB;Provider=SQLNCLI11.1;Integrated Security=SSPI;Auto Translate=False;" />
      </XYZ:ObjectData>
    </XYZ:ConnectionManager>
    <XYZ:ConnectionManager
      XYZ:refId="Package.ConnectionManagers[RTG093955XT.Stage]"
      XYZ:CreationName="OLEDB"
      XYZ:XYZID="{8B4F57EA-03EA-49FA-B4BD-828A89FE5A32}"
      XYZ:ObjectName="RTG093955XT.Stage">
      <XYZ:ObjectData>
        <XYZ:ConnectionManager
          XYZ:ConnectionString="Data Source=RTG093955XT;Initial Catalog=Stage;Provider=SQLNCLI11.1;Integrated Security=SSPI;Auto Translate=False;" />
      </XYZ:ObjectData>
    </XYZ:ConnectionManager>
  </XYZ:ConnectionManagers>
 </XYZ:Executable>
Reply


Messages In This Thread
Parse XML with Namespaces - by dwill - Apr-11-2018, 09:51 PM
RE: Parse XML with Namespaces - by Larz60+ - Apr-12-2018, 01:13 AM
RE: Parse XML with Namespaces - by dwill - Apr-12-2018, 03:25 AM
RE: Parse XML with Namespaces - by stranac - Apr-12-2018, 07:58 AM
RE: Parse XML with Namespaces - by dwill - Apr-12-2018, 04:10 PM
RE: Parse XML with Namespaces - by stranac - Apr-12-2018, 06:51 PM
RE: Parse XML with Namespaces - by dwill - Apr-12-2018, 08:08 PM
RE: Parse XML with Namespaces - by dwill - Apr-12-2018, 09:22 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Still not completely clear about namespaces... bytecrunch 3 1,992 Oct-07-2022, 05:44 PM
Last Post: bytecrunch
  Modules and namespaces (again?) ptrivino 1 1,883 Oct-24-2020, 10:37 PM
Last Post: Larz60+
  namespaces in comprehension in exec() Skaperen 4 4,764 Mar-28-2017, 03:31 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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