Fixing Missing Element Tags in XML with Python (xml.etree.ElementTree)

Поделиться
HTML-код
  • Опубликовано: 1 ноя 2020
  • Example of approach for XML data which is missing tags in elements using Python xml.etree.ElementTree
    Reference: docs.python.org/3/library/xml...
    Previous video, overview of the Python xlm.etree.ElementTree module: • Parsing XML files with...
    Next video, XML with Namespaces:
    • Parsing XML with Names...

Комментарии • 13

  • @kn7298
    @kn7298 2 года назад +2

    I know it's a bit late, but you can correct the XML layout after adding a new subelement by using the Indent method of ElementTree. This refreshes the automatic indent for the whole tree (or element). You may need to specify "space = ' '" (4 spaces) to override the default setting, to match the default layout for the dump method.
    ET.indent (tree, space = ' ') # for entire tree
    ET.indent (elm, space = ' ', level = 1) # for just the element
    although I did have problems with inconsistent spacing trying to just indent the element!! :O

  • @hoscoharding7319
    @hoscoharding7319 3 года назад +1

    Ty so much man!

  • @skillbuilder138
    @skillbuilder138 2 года назад

    Hi, How to write the content of etree.dump to an xml file?

  • @rupeshbhuju2897
    @rupeshbhuju2897 2 года назад

    Hi Francesco Cento. I would like to know how to implement below two use cases
    1) Incorrect type of data inside an element, for e.g string inside an element that is supposed to have an integer.
    2) Missing element: An element that must be present according to XSD is not present in the XML.
    Could you please suggest any idea on this ? thanks

    • @fcento
      @fcento  2 года назад

      Rupesh, for your use case you may have to refer to a different library called “LXML” which has xml schema (XSD) support. I’m not experienced with this but looking at the documentation it has has an example on how to construct a validator which should address both your issues.

    • @rupeshbhuju2897
      @rupeshbhuju2897 2 года назад

      @@fcento Thank you for your suggestion. I will look into that more.

  • @vishalkalal22
    @vishalkalal22 2 года назад +1

    Hi Francesco,
    In my xml closing tag is missing for example,
    Current xml -

    1
    2008
    141100


    Expected xml -

    1
    2008
    141100


    Can you please help me with this?

    • @fcento
      @fcento  2 года назад

      If the closing tag is missing, it is technically an invalid file. You may have to manually close it yourself as there is no way for the code to know where it should be closed.

    • @AnilKumar23456
      @AnilKumar23456 2 года назад +1

      Vishal, you can use regex to find missing closed tag and then just replace it with desired closing tag

  • @vvtwins4kidz
    @vvtwins4kidz 2 года назад

    this code is specific to specific xml, the code should be generic for any XML, if tags are missing it should add the missing tags

    • @aryan6536
      @aryan6536 Год назад

      Surely you can use this information now to achieve many things, are you having any issues? Please share someone might be able ot help.

  • @aryan6536
    @aryan6536 Год назад

    Have you stopped created videos?

    • @fcento
      @fcento  Год назад

      Is there anything in particular you would like to see? Been thinking to possibly do a video on CuPy