Разобрать формат NCPDP XML - PullRequest
0 голосов
/ 15 ноября 2018

Я хотел разобрать ниже xml в python. И если есть какая-либо ошибка, то я хотел бы извлечь эту информацию с описанием кода и описания

xml в формате NCPDP. Я хотел бы получить код описания и описание из приведенного ниже XML.

<transport:Message StructuresVersion="2014041" ECLVersion="2014041" DatatypesVersion="2014041" TransactionDomain="SCRIPT" PA-StructuresVersion="2014041" TransactionVersion="2014041" TransportVersion="2014041" 
xsi:schemaLocation="http://www.ncpdp.org/schema/transport transport.xsd" 
xmlns:transport="http://www.ncpdp.org/schema/transport" 
xmlns:datatypes="http://www.ncpdp.org/schema/datatypes" 
xmlns:script="http://www.ncpdp.org/schema/script" 
xmlns:structures="http://www.ncpdp.org/schema/structures" 
xmlns:pa-structures="http://www.ncpdp.org/schema/pa-structures" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<transport:Header>
    <transport:To Qualifier="C">123456</transport:To>
    <transport:From Qualifier="ZZZ">PAYER123</transport:From>
    <transport:MessageID>ab29d6cd563b43f09e7519314fef0b8d</transport:MessageID>
    <transport:RelatesToMessageID>ab29d6cd563b43f09e7519314fef0b8d</transport:RelatesToMessageID>
    <transport:SentTime>2018-11-15T07:28:34.1539289Z</transport:SentTime>
    <transport:SenderSoftware>
        <transport:SenderSoftwareDeveloper>quanti</transport:SenderSoftwareDeveloper>
        <transport:SenderSoftwareProduct>quanti</transport:SenderSoftwareProduct>
        <transport:SenderSoftwareVersionRelease>1.0</transport:SenderSoftwareVersionRelease>
    </transport:SenderSoftware>
    <transport:TestMessage>1</transport:TestMessage>
</transport:Header>
<transport:Body>
    <transport:Error>
        <transport:Code>601</transport:Code>
        <transport:DescriptionCode>500</transport:DescriptionCode>
        <transport:Description>XML Validation Error: The 'http://www.ncpdp.org/schema/datatypes:Number' element is invalid - The value 'None' is invalid according to its datatype 'http://www.ncpdp.org/schema/datatypes:n1..10' - The Pattern constraint failed.</transport:Description>
    </transport:Error>
</transport:Body>

Я попробовал ниже

import xml.etree.ElementTree as ET 
tree = ET.parse('parse.xml') 
root = tree.getroot() 
for child in root: 
    print(child.tag) 
    print("_______")

1 Ответ

0 голосов
/ 15 ноября 2018

если есть какая-либо ошибка, то я хотел бы извлечь эту информацию с кодом описания и описанием

Это должно помочь вам начать ...

Python

import xml.etree.ElementTree as ET

tree = ET.parse('parse.xml')

ns = {"tr": "http://www.ncpdp.org/schema/transport"}

for error in tree.findall("./tr:Body/tr:Error", ns):
    print(f"ERROR CODE: {error.find('./tr:Code', ns).text}\n"
          f"ERROR DESCRIPTION: {error.find('./tr:Description', ns).text}")

Выход

ERROR CODE: 601
ERROR DESCRIPTION: XML Validation Error: The 'http://www.ncpdp.org/schema/datatypes:Number' element is invalid - The value 'None' is invalid according to its datatype 'http://www.ncpdp.org/schema/datatypes:n1..10' - The Pattern constraint failed.

См. документацию для получения дополнительной информации.

...