Python, lxml and Xpath

https://lxml.de/xpathxslt.html

https://www.w3schools.com/xml/xpath_syntax.asp

Read a file with xml, convert it to a string from bytes:

pip install lxml

from lxml import etree as et

def pack_xml(xmlfile):

    msg = {}
    count = 1
    with open(xmlfile) as r:
        xml = r.read()
    print(type(xml))
    parser = et.XMLParser(recover=True) 
    # params
    # https://lxml.de/api/lxml.etree.XMLParser-class.html
    root = et.fromstring(bytes(xml, encoding="utf-8"), parser)
    # tree = et.tostring(root) # byte
    tree = et.tostring(root).decode("utf-8")# str
    print(len(tree))
    msg = {"msg1":tree}
    with open("rv.xml", "w") as w:
        w.write(tree)
    print(msg)

Make dictionary (duplicate keys, get atr)

result:

Code:

def xml_to_dict(xmlfile):
    msg = {}
    count = 1
    with open(xmlfile) as r:
        xml = r.read()
    print(type(xml))
    parser = et.XMLParser(recover=True, collect_ids=True)
    root = et.fromstring(bytes(xml, encoding="utf-8"), parser)
    x = root.xpath("//*")# get all
    #x = root.xpath("//*[@id]")# get all
    for y in x:
        key = et.QName(y).localname
        value = y.text
        i = y.attrib.get("id")
        if i is not None:
            print(i)
        if key in msg:
            key = key + str(count)
            count += 1
        tmp = {key:value}
        msg.update(tmp)
    for k, v in msg.items():
        print(k, ":", v)

Leave a Reply