0

Please find the mwe xml file (The actual file is 81k lines long, I am showing a tiny part).

<?xml version="1.0" encoding="ISO-8859-1"?>
<modeling>
  <dos>
    <i name="efermi">     -2.48501882 </i>
    <total>
      <array>
        <dimension dim="1">gridpoints</dimension>
        <dimension dim="2">spin</dimension>
        <field>energy</field>
        <field>total</field>
        <field>integrated</field>
        <set>
          <set comment="spin 1">
            <r>   -55.6029     0.0000     0.0000 </r>
            <r>   -55.3940     0.0000     0.0000 </r>
            <r>   -55.1850     0.0000     0.0000 </r>
            <r>   -54.9761     0.0000     0.0000 </r>
          </set>
          <set comment="spin 2">
            <r>   -55.6029     0.0000     0.0000 </r>
            <r>   -55.3940     0.0000     0.0000 </r>
            <r>   -55.1850     0.0000     0.0000 </r>
            <r>   -54.9761     0.0000     0.0000 </r>
          </set>
        </set>
      </array>
    </total>
    <partial>
      <array>
        <dimension dim="1">gridpoints</dimension>
        <dimension dim="2">spin</dimension>
        <dimension dim="3">ion</dimension>
        <field>energy</field>
        <field>    s</field>
        <field>   py</field>
        <field>   pz</field>
        <field>   px</field>
        <field>  dxy</field>
        <field>  dyz</field>
        <field>  dz2</field>
        <field>  dxz</field>
        <field>x2-y2</field>
        <set>
          <set comment="ion 1">
            <set comment="spin 1">
              <r>   -55.6029     0.0000     0.0000     0.0000     0.0000     0.0000     0.0000     0.0000     0.0000     0.0000 </r>
              <r>   -55.3940     0.0000     0.0000     0.0000     0.0000     0.0000     0.0000     0.0000     0.0000     0.0000 </r>
              <r>   -55.1850     0.0000     0.0000     0.0000     0.0000     0.0000     0.0000     0.0000     0.0000     0.0000 </r>
              <r>   -54.9761     0.0000     0.0000     0.0000     0.0000     0.0000     0.0000     0.0000     0.0000     0.0000 </r>
            </set>
            <set comment="spin 2">
              <r>   -55.6029     0.0000     0.0000     0.0000     0.0000     0.0000     0.0000     0.0000     0.0000     0.0000 </r>
              <r>   -55.3940     0.0000     0.0000     0.0000     0.0000     0.0000     0.0000     0.0000     0.0000     0.0000 </r>
              <r>   -55.1850     0.0000     0.0000     0.0000     0.0000     0.0000     0.0000     0.0000     0.0000     0.0000 </r>
              <r>   -54.9761     0.0000     0.0000     0.0000     0.0000     0.0000     0.0000     0.0000     0.0000     0.0000 </r>
            </set>
          </set>
        </set>
      </array>
    </partial>
  </dos>
</modeling>

The dos tag nested well below, and the values in the component spin 1 etc are not necessarily 0. I have managed to reach till dos tag, and get the efermi value, but don't understand how to get the sets separately and selectively so that I can plot it using matplotlib.

This is my current code:

#!/usr/bin/env python
import xml.etree.ElementTree as ET

tree = ET.parse("trial.xml")
root = tree.getroot()

for elem in root:
  print(elem.tag)
  if elem.tag == 'dos':
    for x in elem:
      print(x.attrib.get('name'), x.text)

1 Answer 1

1

You can get the sets directly using https://docs.python.org/3/library/xml.etree.elementtree.html#xml.etree.ElementTree.Element.findall and xpath (like : 'node_a.node_b.etc.req_node') like shown below code... (also can access the comment text)

import xml.etree.ElementTree as ET
import pandas as pd
from matplotlib import pyplot as plt

tree = ET.fromstring("test.xml")
root = tree.getroot()


for elem in root.findall('./dos/partial/array/set/set/set'):
    comment = elem.get('comment')
    print(comment)
    data = list()
    for row in elem.findall('r'):
        data.append(list(map(float, row.text.split())))
    df = pd.DataFrame(data)
    print(df)
    df.plot()
    plt.show()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.