Home > Software design >  How to find XML element by tag in Python
How to find XML element by tag in Python

Time:09-29

I am having trouble identifying the correct element in Python. What I actually want to see is the latest accessed file in the recently-used.xbel. Therefore I want to iterate over every file to find the one with the latest modified or latest visited This is how the XML file looks like.

<?xml version="1.0" encoding="UTF-8"?>
<xbel version="1.0"
      xmlns:bookmark="http://www.freedesktop.org/standards/desktop-bookmarks"
      xmlns:mime="http://www.freedesktop.org/standards/shared-mime-info"
>
  <bookmark href="file:///tmp/google-chrome-stable_current_amd64.deb" added="2021-09-14T12:09:05Z" modified="2021-09-14T12:09:05Z" visited="2021-09-15T09:12:13Z">
    <info>
      <metadata owner="http://freedesktop.org">
        <mime:mime-type type="application/vnd.debian.binary-package"/>
        <bookmark:applications>
          <bookmark:application name="Firefox" exec="&apos;firefox %u&apos;" modified="2021-09-14T12:09:05Z" count="1"/>
        </bookmark:applications>
      </metadata>
    </info>
  </bookmark>
  <bookmark href="file:///home/test/Git/testprog" added="2021-09-15T09:12:13Z" modified="2021-09-15T09:12:13Z" visited="2021-09-15T09:12:13Z">
    <info>
      <metadata owner="http://freedesktop.org">
        <mime:mime-type type="inode/directory"/>
        <bookmark:applications>
          <bookmark:application name="code" exec="&apos;code %u&apos;" modified="2021-09-15T09:12:13Z" count="1"/>
        </bookmark:applications>
      </metadata>
    </info>
  </bookmark>
  <bookmark href="file:///home/test/.local/share/recently-used.xbel" added="2021-09-15T09:51:57Z" modified="2021-09-15T09:51:57Z" visited="2021-09-15T09:51:57Z">
    <info>
      <metadata owner="http://freedesktop.org">
        <mime:mime-type type="application/x-xbel"/>
        <bookmark:applications>
          <bookmark:application name="code" exec="&apos;code %u&apos;" modified="2021-09-15T09:51:57Z" count="1"/>
        </bookmark:applications>
      </metadata>
    </info>
  </bookmark>
  <bookmark href="file:///tmp/slack-desktop-4.19.2-amd64.deb" added="2021-09-15T11:45:49Z" modified="2021-09-15T11:45:49Z" visited="2021-09-16T13:26:26Z">
    <info>
      <metadata owner="http://freedesktop.org">
        <mime:mime-type type="application/vnd.debian.binary-package"/>
        <bookmark:applications>
          <bookmark:application name="Firefox" exec="&apos;firefox %u&apos;" modified="2021-09-15T11:45:49Z" count="1"/>
        </bookmark:applications>
      </metadata>
    </info>
  </bookmark>
  <bookmark href="file:///home/test/Downloads/google-chrome-stable_current_amd64.deb" added="2021-09-15T11:52:39Z" modified="2021-09-15T11:52:39Z" visited="2021-09-16T13:26:26Z">
    <info>
      <metadata owner="http://freedesktop.org">
        <mime:mime-type type="application/vnd.debian.binary-package"/>
        <bookmark:applications>
          <bookmark:application name="Firefox" exec="&apos;firefox %u&apos;" modified="2021-09-15T11:52:39Z" count="1"/>
        </bookmark:applications>
      </metadata>
    </info>
  </bookmark>
  <bookmark href="file:///home/test/Documents/libretest" added="2021-09-15T11:58:53Z" modified="2021-09-15T11:58:53Z" visited="2021-09-16T13:26:26Z">
    <info>
      <metadata owner="http://freedesktop.org">
        <mime:mime-type type="application/octet-stream"/>
        <bookmark:applications>
          <bookmark:application name="LibreOffice 6.4" exec="&apos;soffice %u&apos;" modified="2021-09-15T11:58:53Z" count="1"/>
        </bookmark:applications>
      </metadata>
    </info>
  </bookmark>
  <bookmark href="file:///home/test/Documents/libretest.odt" added="2021-09-15T11:58:53Z" modified="2021-09-15T15:42:04Z" visited="2021-09-16T13:26:26Z">
    <info>
      <metadata owner="http://freedesktop.org">
        <mime:mime-type type="application/vnd.oasis.opendocument.text"/>
        <bookmark:applications>
          <bookmark:application name="LibreOffice 6.4" exec="&apos;soffice %u&apos;" modified="2021-09-15T15:42:04Z" count="12"/>
        </bookmark:applications>
      </metadata>
    </info>
  </bookmark>
  <bookmark href="file:///home/test/Git/node-socket" added="2021-09-16T13:26:25Z" modified="2021-09-16T13:26:49Z" visited="2021-09-16T13:26:26Z">
    <info>
      <metadata owner="http://freedesktop.org">
        <mime:mime-type type="inode/directory"/>
        <bookmark:applications>
          <bookmark:application name="code" exec="&apos;code %u&apos;" modified="2021-09-16T13:26:49Z" count="2"/>
        </bookmark:applications>
      </metadata>
    </info>
  </bookmark>
</xbel>

In my code I am trying to access bookmark:applications but with no success.

    home = str(Path.home())
    root = ET.parse(home   '/.local/share/recently-used.xbel').getroot()
    print(root)
    print('lower')
    for bookmark in root.iter('bookmark'):
        print(bookmark)
        for applications in bookmark.find('applications'):
            print(applications)

What would be the correct way to access bookmark:applications and find the last visited?

CodePudding user response:

I'm not a professional in Python But I think this will help you. How to find xml element by tag

CodePudding user response:

import xml.etree.ElementTree as ET
import pandas as pd

root = ET.parse('/content/sample.xml').getroot()
lst = []

for bookmark in bookmarklist:
  bookmark_lst = []
  print(bookmark.attrib)
  bookmark_lst.append(bookmark.attrib['href'])
  bookmark_lst.append(bookmark.attrib['modified'])
  bookmark_lst.append(bookmark.attrib['visited'])
  for ele in list(bookmark.iter()) :
    if 'application' in ele.tag:
      if 'name' in ele.attrib:
        bookmark_lst.append(ele.attrib['name'])
  lst.append(bookmark_lst)

df = pd.DataFrame(lst,columns ['href','modified','visited','application_name'])

df['modified'] = pd.to_datetime(df['modified'])
df['visited'] = pd.to_datetime(df['visited'])

least_recent_date = df['visited'].min()
most_recent_date = df['visited'].max()

please upvote if this is helpful.

  • Related