Python Forum
Reusing same namespace name when writing back XML with xml.etree.ElementTree
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reusing same namespace name when writing back XML with xml.etree.ElementTree
#1
I'm editing an XML file with xml.etree.ElementTree. On input the XML has a namespace defined so all tags are of the form
<somenamespace:tag>
When I write the XML back to file, the namespace is called ns0 and the tags look like:

<ns0:tag>
This is valid XML but not as readable. How can i tell Python to use "somenamespace" instead of "ns0"? I tried ET.register_namespace(prefix,namespace) but that doesn't work.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#2
Oops, actually a problem in my code. ET.register_namespace(prefix,namespace) works provided that:

  1. It executed before reading the XML
  2. The namespace string is the correct one.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  xml file editing with lxml.etree FlavioBueno 2 683 Jun-09-2023, 02:00 PM
Last Post: FlavioBueno
  [SOLVED] [ElementTree] Grab text in attributes? Winfried 3 1,637 May-27-2022, 04:59 PM
Last Post: Winfried
  [ElementTree] Insert big block of HTML? Winfried 0 1,185 May-12-2022, 07:08 AM
Last Post: Winfried
  ElementTree get attribute value part of string paulo79 1 2,144 Apr-05-2022, 09:13 PM
Last Post: deanhystad
  'namespace' shorthand for function arguments? shadowphile 5 2,599 Aug-11-2021, 09:02 PM
Last Post: shadowphile
  xml.etree.ElementTree question. water 0 3,284 Oct-09-2020, 06:47 PM
Last Post: water
  [PyKML] Loop through all Placemarks; Remove namespace Winfried 2 3,434 Aug-28-2020, 09:24 AM
Last Post: Winfried
  xml.etree.ElementTree extract string values matthias100 2 4,982 Jul-12-2020, 06:02 PM
Last Post: snippsat
  [pykml] "AttributeError: 'lxml.etree._ElementTree' object has no attribute 'Document' Winfried 3 6,592 May-26-2020, 09:30 PM
Last Post: Winfried
  Python 3 Elementtree and Comment First gw1500se 3 3,739 May-25-2020, 09:02 PM
Last Post: gw1500se

Forum Jump:

User Panel Messages

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