Cannot find a namespaced attribute from current element - python

Hi I have the following XML and I am using the python ElementTree library to parse it.
<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camunda="http://camunda.org/schema/1.0/bpmn" id="Definitions_0qjsjhs" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="2.2.4">
<bpmn:process id="Process_1" isExecutable="true">
<bpmn:startEvent id="StartEvent_1">
<bpmn:outgoing>SequenceFlow_0bwuv8k</bpmn:outgoing>
</bpmn:startEvent>
<bpmn:sequenceFlow id="SequenceFlow_0bwuv8k" sourceRef="StartEvent_1" targetRef="Task_07owcwp" />
<bpmn:endEvent id="EndEvent_13n8600">
<bpmn:incoming>SequenceFlow_111n6oc</bpmn:incoming>
</bpmn:endEvent>
<bpmn:sequenceFlow id="SequenceFlow_111n6oc" sourceRef="Task_07owcwp" targetRef="EndEvent_13n8600">
<bpmn:conditionExpression xsi:type="bpmn:tFormalExpression">myexpression.</bpmn:conditionExpression>
</bpmn:sequenceFlow>
<bpmn:scriptTask id="Task_07owcwp" scriptFormat="testformat" camunda:resultVariable="output">
<bpmn:incoming>SequenceFlow_0bwuv8k</bpmn:incoming>
<bpmn:outgoing>SequenceFlow_111n6oc</bpmn:outgoing>
<bpmn:script>myscript</bpmn:script>
</bpmn:scriptTask>
</bpmn:process>
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_1">
<bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1">
<dc:Bounds x="173" y="102" width="36" height="36" />
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="SequenceFlow_0bwuv8k_di" bpmnElement="SequenceFlow_0bwuv8k">
<di:waypoint x="209" y="120" />
<di:waypoint x="266" y="120" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="EndEvent_13n8600_di" bpmnElement="EndEvent_13n8600">
<dc:Bounds x="423" y="102" width="36" height="36" />
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="SequenceFlow_111n6oc_di" bpmnElement="SequenceFlow_111n6oc">
<di:waypoint x="366" y="120" />
<di:waypoint x="423" y="120" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="ScriptTask_1p2sdkp_di" bpmnElement="Task_07owcwp">
<dc:Bounds x="266" y="80" width="100" height="80" />
</bpmndi:BPMNShape>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</bpmn:definitions>`
I am currently at the 'bpmn:scripttask' element and I am having issues working out how to extract the value of 'camunda:resultVariable'
<bpmn:scriptTask id="Task_07owcwp" scriptFormat="testformat" camunda:resultVariable="output">
<bpmn:incoming>SequenceFlow_0bwuv8k</bpmn:incoming>
<bpmn:outgoing>SequenceFlow_111n6oc</bpmn:outgoing>
<bpmn:script>myscript</bpmn:script>
</bpmn:scriptTask>
I have tried
node.find('camunda:resultVariable' {'camunda':'http://camunda.org/schema/1.0/bpmn'}
where node is a an ElementTree object scriptTask. Does find or findall all you to look at the current elements attributes as it doesn't seem to be finding it. How would I go about finding this value?
Thank you.

ok I worked it out. I can use .get but I need to expand out the namespace like so
node.get('{http://camunda.org/schema/1.0/bpmn}resultVariable')

Related

I cannot parse this xml file in python

I am trying to create an API connection and response is looking like below. I need to parse this data and turn it into a pd dataframe and/or create loop to find specific information belong to tags.
Below is the code i try to run but it returns with empty list, and it looks not iterable.
Also it is not convertible to a data frame for now. What steps should I take to handle this data?
import requests
import pandas as pd
import xml.etree.ElementTree as ET
response = """<?xml version = "1.0" encoding = "utf-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SOAP-ENV:Body>
<Desperados_Clientes_V2.DESPERADOSResponse xmlns="TrainsWebb_V16">
<Sdtdesperadosclient xmlns="TrainsWebb_V16">
<SDTDesperadosClientItem xmlns="TrainsWebb_V16">
<AESA>10555555555 </AESA>
<DOCUMENTO>1666666666</DOCUMENTO>
<REMITENTE>888888888 </REMITENTE>
<NM_REMITENTE>ABDULREZZAK S.A.S. </NM_REMITENTE>
<FECHA_ELABORACION>14/8/2020</FECHA_ELABORACION>
<HORA_ELABORACION>11:27</HORA_ELABORACION>
<CODIGO_DEST>0000000000</CODIGO_DEST>
<NIT_DESTINATARIO>0000000000</NIT_DESTINATARIO>
<NOMBRE_DESTINATARIO>HOST ADMIRALE GORA</NOMBRE_DESTINATARIO>
<DIRECCION_DESTINATARIO>BBA 56 # 21 - 001</DIRECCION_DESTINATARIO>
<DANE_DESTINO>0200000</DANE_DESTINO>
<CIUDAD_DESTINO>GORA </CIUDAD_DESTINO>
<DEPARTAMENTO_DESTINO>ANTIOCHIA </DEPARTAMENTO_DESTINO>
<FECHA_ENTREGA>11/02/2020</FECHA_ENTREGA>
<HORA_ENTREGA>11:44</HORA_ENTREGA>
<FECHA_CITA />
<HORA_CITA />
<CODIGO_ESTADO>Z </CODIGO_ESTADO>
<NOMBRE_ESTADO>CUMPLEANNO </NOMBRE_ESTADO>
<FECHA_ESTADO>11/01/2020</FECHA_ESTADO>
<HORA_ESTADO>11:44</HORA_ESTADO>
<CODIGO_NOVEDAD />
<NOMBRE_NOVEDAD />
<FECHA_NOVEDAD />
<HORA_NOVEDAD />
<COMENTARIO_NOVEDAD />
<OBSERVACIONES />
<ENLACE_IMAGEN>https://ssssss.ssssssss.com/SSSSSS/xxxxxxxxxxxxxx.aspx?1111111,222222222,SIXA_XEOX,SIXAXEOX2016</ENLACE_IMAGEN>
<DOCUMENTO_2>169999999999</DOCUMENTO_2>
<DOCUMENTO_3 />
<DOCUMENTO_4 />
<FECHA_TRANSMISION>18/02/2020</FECHA_TRANSMISION>
<HORA_TRANSMISION>08:12:30</HORA_TRANSMISION>
<MENSAJE_TRANSMISION>KK</MENSAJE_TRANSMISION>
<PROMESA_SERVICIO>15/10/21</PROMESA_SERVICIO>
<CODIGO_DIVISION>011111</CODIGO_DIVISION>
<NOMBRE_DIVISION>ABDURREZZAK </NOMBRE_DIVISION>
</SDTDesperadosClientItem>
<SDTDesperadosClientItem xmlns="TrainsWebb_V16">
<AESA>10555555555 </AESA>
<DOCUMENTO>177777777</DOCUMENTO>
<REMITENTE>9999999999 </REMITENTE>
<NM_REMITENTE>ABDULREZZAK S.A.S. </NM_REMITENTE>
<FECHA_ELABORACION>12/8/2020</FECHA_ELABORACION>
<HORA_ELABORACION>16:27</HORA_ELABORACION>
<CODIGO_DEST>0000000000</CODIGO_DEST>
<NIT_DESTINATARIO>0000000000</NIT_DESTINATARIO>
<NOMBRE_DESTINATARIO>GORA FORA</NOMBRE_DESTINATARIO>
<DIRECCION_DESTINATARIO>BBG 16 # 91 - 021</DIRECCION_DESTINATARIO>
<DANE_DESTINO>0500000</DANE_DESTINO>
<CIUDAD_DESTINO>AROG </CIUDAD_DESTINO>
<DEPARTAMENTO_DESTINO>ANTIOCHIA </DEPARTAMENTO_DESTINO>
<FECHA_ENTREGA>10/02/2020</FECHA_ENTREGA>
<HORA_ENTREGA>10:44</HORA_ENTREGA>
<FECHA_CITA />
<HORA_CITA />
<CODIGO_ESTADO>D </CODIGO_ESTADO>
<NOMBRE_ESTADO>CUMPLEANNI </NOMBRE_ESTADO>
<FECHA_ESTADO>11/01/2020</FECHA_ESTADO>
<HORA_ESTADO>11:44</HORA_ESTADO>
<CODIGO_NOVEDAD />
<NOMBRE_NOVEDAD />
<FECHA_NOVEDAD />
<HORA_NOVEDAD />
<COMENTARIO_NOVEDAD />
<OBSERVACIONES />
<ENLACE_IMAGEN>https://ssssss.ssssssss.com/SSSSSS/xxxxxxxxxxxxxx.aspx?1111111,222222222,SIXA_XEOX,SIXAXEOX2016</ENLACE_IMAGEN>
<DOCUMENTO_2>1677777777</DOCUMENTO_2>
<DOCUMENTO_3 />
<DOCUMENTO_4 />
<FECHA_TRANSMISION>18/02/2020</FECHA_TRANSMISION>
<HORA_TRANSMISION>08:12:30</HORA_TRANSMISION>
<MENSAJE_TRANSMISION>HK</MENSAJE_TRANSMISION>
<PROMESA_SERVICIO>15/10/21</PROMESA_SERVICIO>
<CODIGO_DIVISION>011111</CODIGO_DIVISION>
<NOMBRE_DIVISION>ABDURREZZAK </NOMBRE_DIVISION>
</SDTDesperadosClientItem>
</Sdtdesperadosclient>
</Desperados_Clientes_V2.DESPERADOSResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>"""
myroot = ET.fromstring(response)
for child in myroot.iter('*'):
print(child.tag)
sid = myroot.findall(".//{'TrainsWebb_V16'}AESA")
print(sid)
For parsing into a pandas DataFrame, you can use the pandas.read_xml function:
data_frame = pd.read_xml(response, xpath="//*[name()='SDTDesperadosClientItem']")
https://pandas.pydata.org/docs/reference/api/pandas.read_xml.html#pandas-read-xml

how to find and edit tags in XML files with namespaces using ElementTree

I would like to find specific tags in my XML document and edit their text or attributes. My XML file contains namespaces (and as I understand it correctly, nested namespaces). The tool I'd like to use for this purpose is ElementTree. I managed to read XML file by iterparse, however I don't know how I can save edited XML, because iterparse doesn't have write element. I need a solution to read XML file by parse and strip its namespaces and nested namespaces or a way to save iterparsed file.
For this case, let's edit the "Rating" tag text.
it = ET.iterparse(adiPath)
for _, el in it:
if '}' in el.tag:
el.tag = el.tag.split('}', 1)[1] # strip all namespaces
for at in list(el.attrib): # strip namespaces of attributes too
if '}' in at:
newat = at.split('}', 1)[1]
el.attrib[newat] = el.attrib[at]
del el.attrib[at]
root = it.root
# Search Rating tag and edit it's value
for rating in root.iter('Rating'):
print(rating.text) # Prints 18
rating.text = "999"
print(rating.text) # Prints 999
However in this case XML file remains unchanged.
Here is XML file:
<?xml version="1.0" encoding="utf-8"?>
<ADI3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:content="urn:cablelabs:md:xsd:content:3.0" xmlns:core="urn:cablelabs:md:xsd:core:3.0" xmlns:offer="urn:cablelabs:md:xsd:offer:3.0" xmlns:terms="urn:cablelabs:md:xsd:terms:3.0" xmlns:title="urn:cablelabs:md:xsd:title:3.0" xmlns:adb="urn:adb:md:xsd:adb:01" xmlns:schemaLocation="urn:adb:md:xsd:adb:01 ADB-EXT-C01.xsd urn:cablelabs:md:xsd:core:3.0 MD-SP-CORE-C01.xsd urn:cablelabs:md:xsd:content:3.0 MD-SP-CONTENT-C01.xsd urn:cablelabs:md:xsd:offer:3.0 MD-SP-OFFER-C01.xsd urn:cablelabs:md:xsd:terms:3.0 MD-SP-TERMS-C01.xsd urn:cablelabs:md:xsd:title:3.0 MD-SP-TITLE-C01.xsd" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns="urn:cablelabs:md:xsd:core:3.0">
<Asset xsi:type="title:TitleType" uriId="ab://cc.com" providerVersionNum="1" internalVersionNum="0" creationDateTime="2020-01-28T08:55:19Z" startDateTime="2019-05-20T00:00:00Z" endDateTime="2028-08-20T23:59:00Z">
<AlternateId identifierSystem="VOD1.1">ab://cc.com</AlternateId>
<Ext>
<adb:ExtensionType>
<adb:TitleExt>
<adb:SeriesInfo episodeNumber="6">
<adb:series seriesId="GOT" seasonCount="8"></adb:series>
<adb:season seasonId="GOTS08" number="8" episodeCount="6"></adb:season>
</adb:SeriesInfo>
</adb:TitleExt>
</adb:ExtensionType>
</Ext>
<title:LocalizableTitle xml:lang="pol">
<title:TitleLong>Game of Thrones VIII</title:TitleLong>
<title:SummaryLong>Long summary, long summary, long summary...</title:SummaryLong>
<title:Actor fullName="Peter Dinklage" firstName="Peter" lastName="Dinklage" />
<title:Actor fullName="Nikolaj Coster-Waldau" firstName="Nikolaj" lastName="Coster-Waldau" />
<title:Actor fullName="Emilia Clarke" firstName="Emilia" lastName="Clarke" />
<title:Actor fullName="Lena Headey" firstName="Lena" lastName="Headey" />
<title:Director fullName="David Nutter" firstName="David" lastname="Nutter" />
</title:LocalizableTitle>
<title:Rating ratingSystem="PL">18</title:Rating>
<title:Audience>General</title:Audience>
<title:DisplayRunTime>01:15</title:DisplayRunTime>
<title:Year>2019</title:Year>
<title:CountryOfOrigin>US</title:CountryOfOrigin>
<title:Genre>Film fantasy</title:Genre>
<title:ShowType>Movie</title:ShowType>
</Asset>
<Asset xsi:type="offer:CategoryType" uriId="cc.com/XX">
<AlternateId identifierSystem="VOD1.1">cc.com/XX</AlternateId>
<offer:CategoryPath>VOD/GOT/Season 8</offer:CategoryPath>
</Asset>
<Asset xsi:type="content:MovieType" uriId="GraoTronVIII_0_1080mp4">
<AlternateId identifierSystem="VOD1.1">GraoTronVIII_0_1080mp4</AlternateId>
<content:SourceUrl>GOTS08E06.mp4</content:SourceUrl>
<content:Resolution>1080p</content:Resolution>
<content:Duration>PT1H15M20S</content:Duration>
<content:Language>pol</content:Language>
<content:Language>eng</content:Language>
</Asset>
<Asset xsi:type="content:PreviewType" uriId="GraoTronVIII_1_1080mp4">
<AlternateId identifierSystem="VOD1.1">GraoTronVIII_1_1080mp4</AlternateId>
<content:SourceUrl>GOTS08E06_trailer.mp4</content:SourceUrl>
<content:Resolution>1080p</content:Resolution>
<content:Duration>PT0H01M48S</content:Duration>
<content:Language>pol</content:Language>
<content:Language>eng</content:Language>
</Asset>
<Asset xsi:type="content:PosterType" uriId="GraoTronVIIIPoster">
<AlternateId identifierSystem="VOD1.1">GraoTronVIIIPoster</AlternateId>
<content:SourceUrl>GOTS08E06.jpg</content:SourceUrl>
<content:X_Resolution>600</content:X_Resolution>
<content:Y_Resolution>900</content:Y_Resolution>
<content:Language>pol</content:Language>
</Asset>
<Asset xsi:type="offer:ContentGroupType" uriId="abc">
<AlternateId identifierSystem="VOD1.1">abc</AlternateId>
<offer:TitleRef uriId="abc" />
<offer:MovieRef uriId="GraoTronVIII_0_1080mp4" />
</Asset>
<Asset xsi:type="offer:ContentGroupType" uriId="abc">
<AlternateId identifierSystem="VOD1.1">abc</AlternateId>
<offer:TitleRef uriId="abc" />
<offer:MovieRef uriId="GraoTronVIII_1_1080mp4" />
</Asset>
<Asset xsi:type="offer:ContentGroupType" uriId="abc">
<AlternateId identifierSystem="VOD1.1">abc</AlternateId>
<offer:TitleRef uriId="abc" />
<offer:MovieRef uriId="GraoTronVIIIPoster" />
</Asset>
</ADI3>
Instead of stripping out the namespaces, I suggest using namespace wildcards. Support for this was added in Python 3.8.
from xml.etree import ElementTree as ET
tree = ET.parse(adiPath)
rating = tree.find(".//{*}Rating") # Find the Rating element in any namespace
rating.text = "999"
Note that you have to use find() (or findall()). Wildcards do not work with iter().
The following workaround can be used to preserve the original namespace prefixes when serializing the XML document (see also https://stackoverflow.com/a/42372404/407651 and https://stackoverflow.com/a/54491129/407651).
namespaces = dict([elem for _, elem in ET.iterparse("test1.xml", events=['start-ns'])])
for ns in namespaces:
ET.register_namespace(ns, namespaces[ns])

Why do I get an error while iterating through a dictionary

I'm a beginner in Python and struggling to understand why while iterating through a dictionary obtained from an XML file I get an error when I try to search for required keys.I should also mention that, I still get the result I want, but somehow I keep receiving an error.
import os
import shelve
import xml.etree.ElementTree as et
shelfFile = shelve.open('xml_data')
base_path = os.path.dirname(os.path.realpath(__file__))
xml_file = os.path.join(base_path, "data\\nrm_icg_catalog.xml")
tree = et.parse(xml_file)
root = tree.getroot()
elements = ['Name','WBS']
for child in root:
for itemGroup1 in child:
for item in elements:
print(itemGroup1.attrib[item])
Result with Error Message:
Facilitating Works
0
Substructure
1
Superstructure
2
Internal Finish
3
Fittings
4
Services
5
Prefabs
6
Works to Existing Building
7
External Works
8
MC Prelims
9
MC OH and P
10
Traceback (most recent call last):
File "c:/Users/Dodzi Agbenorku/OneDrive/Training Files/Programming Lessons/Python/xmlExcel/app.py", line 22, in <module>
print(itemGroup1.attrib[item])
KeyError: 'Name'
Here's a small section of the xml file I am using:
<?xml version="1.0" encoding="utf-8"?>
<Takeoff xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://download.autodesk.com/us/navisworks/schemas/nw-TakeoffCatalog-10.0.xsd">
<Catalog>
<ItemGroup Name="Facilitating Works" WBS="0" CatalogId="32b4ab2d-6fe8-4c45-9872-c8ea68c0c4de">
<ItemGroup Name="Hazardous Materials" WBS="1" CatalogId="2fdb6bd1-b2d1-4167-a74d-da818183a156">
<ItemGroup Name="Material Removal" WBS="1" CatalogId="ccc8a515-4152-400c-a72e-6fd78561325e">
<Item Name="Material Details" WBS="1" Transparency="0.3" Color="-15161029" LineThickness="0.1" CatalogId="c0a7de26-6bc3-491e-b3c7-3ff5b560eeaf">
<VariableCollection>
<Variable Name="Length" Formula="=ModelLength" Units="Meter" />
<Variable Name="Width" Formula="=ModelWidth" Units="Meter" />
<Variable Name="Thickness" Formula="=ModelThickness" Units="Meter" />
<Variable Name="Height" Formula="=ModelHeight" Units="Meter" />
<Variable Name="Perimeter" Formula="=ModelPerimeter" Units="Meter" />
<Variable Name="Area" Formula="=ModelArea" Units="SquareMeter" />
Any help will be extremely appreciated.
The error occurs because for some nodes, Name is not in attrib which is a dictionary.
Instead of itemGroup1.attrib[item], use itemGroup1.attrib.get(item). It will return the value None if the key does not exist, and it will not throw an error.

Why lxml (Python Lib) doesn't parse Signature node using objectify.fromstring(xml_string)?

I'm using lxml lib to parse XML(My XML file uses XML Signature) to python object, everything works except just one case, the xml model below the Signature tag is not parsed to Object.
from lxml import etree,objectify
xml_object = objectify.fromstring(xml_string)
# This works
xml_object.infNFe
#This doesn't works, raise the exception that Signature attribute is not defined
xml_object.Signature
PyCharm debug, the Signature tag was not parsed
Curiosity, if I try to parse ONLY Signature tag it works, but if the it's together with others, it doesn't works.
I've looked the website about this issue but I didn't find any solution!
So, the question is, how to parse the whole XML below?
The XML used
<?xml version="1.0" encoding="UTF-8"?>
<NFe>
<infNFe Id="NFe24151011111111111111550010000000301973817012" versao="3.10">
<ide>
<cUF>24</cUF>
<cNF>97381701</cNF>
<natOp>Venda de produto do estabelecimento</natOp>
<indPag>0</indPag>
<mod>55</mod>
<serie>1</serie>
<nNF>30</nNF>
<dhEmi>2015-10-08T15:38:45-03:00</dhEmi>
<dhSaiEnt>2015-10-08T15:38:45-03:00</dhSaiEnt>
<tpNF>1</tpNF>
<idDest>1</idDest>
<cMunFG>2408003</cMunFG>
<tpImp>1</tpImp>
<tpEmis>1</tpEmis>
<cDV>2</cDV>
<tpAmb>2</tpAmb>
<finNFe>1</finNFe>
<indFinal>0</indFinal>
<indPres>1</indPres>
<procEmi>0</procEmi>
<verProc>PyComp Modulo NFe</verProc>
</ide>
<emit>
<CNPJ>11111111111111</CNPJ>
<xNome>Razão Social Ltda. EPP</xNome>
<xFant>Nome Fantasia</xFant>
<enderEmit>
<xLgr>Al. Kenworthy</xLgr>
<nro>140</nro>
<xBairro>Jd. Santa Rosália</xBairro>
<cMun>2408003</cMun>
<xMun>Mossoro</xMun>
<UF>RN</UF>
<CEP>59600000</CEP>
<cPais>1058</cPais>
<fone>1534110602</fone>
</enderEmit>
<IE>111111111111</IE>
<CRT>1</CRT>
</emit>
<dest>
<CNPJ>11111111111111</CNPJ>
<xNome>EMPRESA EXEMPLO LTDA</xNome>
<enderDest>
<xLgr>RUA NISIA FLORESTA</xLgr>
<nro>846</nro>
<xBairro>BOA VISTA</xBairro>
<cMun>2408003</cMun>
<xMun>MOSSORO</xMun>
<UF>RN</UF>
<cPais>1058</cPais>
<xPais>BRASIL</xPais>
<fone>1534110602</fone>
</enderDest>
<indIEDest>9</indIEDest>
<IE>111111111111</IE>
</dest>
<det nItem="1">
<prod>
<cProd>1253236</cProd>
<cEAN>7896297837046</cEAN>
<xProd>Descrição do produto</xProd>
<NCM>999999999</NCM>
<CFOP>5102</CFOP>
<uCom>UN</uCom>
<qCom>100.00</qCom>
<vUnCom>10.00</vUnCom>
<vProd>1000.00</vProd>
<cEANTrib>7896297837046</cEANTrib>
<uTrib>UN</uTrib>
<qTrib>100.00</qTrib>
<vUnTrib>10.00</vUnTrib>
<indTot>1</indTot>
</prod>
<imposto>
<vTotTrib>18.00</vTotTrib>
<ICMS>
<ICMS00>
<orig>0</orig>
<CST>00</CST>
<modBC>3</modBC>
<vBC>1.00</vBC>
<pICMS>17.00</pICMS>
<vICMS>0.17</vICMS>
</ICMS00>
</ICMS>
</imposto>
</det>
<det nItem="2">
<prod>
<cProd>1253236</cProd>
<cEAN>7896297837046</cEAN>
<xProd>PRODUTO 2</xProd>
<NCM>999999999</NCM>
<CFOP>5102</CFOP>
<uCom>UN</uCom>
<qCom>1.00</qCom>
<vUnCom>1.00</vUnCom>
<vProd>1.00</vProd>
<cEANTrib>7896297837046</cEANTrib>
<uTrib>UN</uTrib>
<qTrib>1.00</qTrib>
<vUnTrib>1.00</vUnTrib>
<indTot>1</indTot>
</prod>
<imposto>
<vTotTrib>18.17</vTotTrib>
<ICMS>
<ICMS00>
<orig>0</orig>
<CST>00</CST>
<modBC>3</modBC>
<vBC>1.00</vBC>
<pICMS>17.00</pICMS>
<vICMS>0.17</vICMS>
</ICMS00>
</ICMS>
</imposto>
</det>
<total>
<ICMSTot>
<vBC>1000.00</vBC>
<vICMS>180.00</vICMS>
<vICMSDeson>0.00</vICMSDeson>
<vBCST>0.00</vBCST>
<vST>0.00</vST>
<vProd>1000.00</vProd>
<vFrete>0.00</vFrete>
<vSeg>0.00</vSeg>
<vDesc>0.00</vDesc>
<vII>0.00</vII>
<vIPI>0.00</vIPI>
<vPIS>0.00</vPIS>
<vCOFINS>0.00</vCOFINS>
<vOutro>0.00</vOutro>
<vNF>1100.00</vNF>
<vTotTrib>0.00</vTotTrib>
</ICMSTot>
</total>
<transp>
<modFrete>9</modFrete>
</transp>
</infNFe>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
<Reference URI="#NFe24151011111111111111550010000000301973817012">
<Transforms>
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
<Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
<DigestValue>OWcT6jHwBr4c3kGNBwzFoQ03oHQ=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>BxP66jlYeNfD2JPKkUaih3gJzMvxIWWZsGDqps/lylpW55uyCl4DnK9ppdXAzKNsoRwyQz5fPE8CECSbJbkZkIth82pK0Ul7ZjWsogD6luncJ78oS+B6xFqUL3ypm5mtq+vkQOK8ljdBxAh8gYRW/V8yEMu2rTcmI1vki6PGOMnItM68e7vpJl01GLI2FwI9gLBDNjHJJ2i8snJL6waS/PHx+UOX2vYogIGbtw+y11FI0brcm056AjyIycZsEg8XByoVB7WfLVNIpCPwdDrDjMV6TOTX9MGMuoVRxAYz+7W5PEkJTU6Sd5FmHcsRkw7SeDaXGgdcBR7SIbrnOOH6zQ==</SignatureValue>
<KeyInfo>
<X509Data>
<X509Certificate>MIIIcjCCBlqgAwIBAgIQNRo7p1A4dwNd7Yu7TSyJ7jANBgkqhkiG9w0BAQsFADB4MQswCQYDVQQGEwJCUjETMBEGA1UEChMKSUNQLUJyYXNpbDE2MDQGA1UECxMtU2VjcmV0YXJpYSBkYSBSZWNlaXRhIEZlZGVyYWwgZG8gQnJhc2lsIC0gUkZCMRwwGgYDVQQDExNBQyBDZXJ0aXNpZ24gUkZCIEc0MB4XDTE0MTExMDAwMDAwMFoXDTE1MTEwOTIzNTk1OVowgf0xCzAJBgNVBAYTAkJSMRMwEQYDVQQKFApJQ1AtQnJhc2lsMQswCQYDVQQIEwJSTjEQMA4GA1UEBxQHTU9TU09STzE2MDQGA1UECxQtU2VjcmV0YXJpYSBkYSBSZWNlaXRhIEZlZ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TtdLIclcNoYNjVBbiGtC/xMS53PwQJ6q3yApth4BiDkS4ZFkk4DqaQ3hPExFpfjZ4fDLEt8MT5SBTD/3Efn7CMy0rx2Bq4H1mWEzJ2SCU01iMcqGPdg5TR7OzraCnztjfAK6DFlO3Q9EzrM5Rn5LtmPpRlpTRrr0HzH+movukshOqZO9yXgKkSuf6RD3rKGgNP+O5idXegi9frr+Hp9TA8eejf7IpvYSAEvDJHs8uIKvmbhcGLNpSL+mWod/H23j9sUFkmqHOahxhhOqg9LzShdEbtlts+XaeZTlGz2D3MKG/LjPkDw+QkrWIBTS8zOfrj0MeMtuW/uDKoLL6iQzYj</X509Certificate>
</X509Data>
</KeyInfo>
</Signature>
</NFe>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
There is a problem with xmlns attribute. lmlx will try to parse it as namespase. Just remove it or replace.
>>> print etree.tostring(xml_object.Signature, pretty_print=True)
<Signature>
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
<Reference URI="#NFe24151011111111111111550010000000301973817012">
<Transforms>
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
<Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<DigestValue>OWcT6jHwBr4c3kGNBwzFoQ03oHQ=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>...</SignatureValue>
<KeyInfo>
<X509Data>
<X509Certificate>....</X509Certificate>
</X509Data>
</KeyInfo>
</Signature>

Minidom getElementById not working

Minidom's getElementById function is returning None for any entry I pass to it.
For example, this code:
l = minidom.parseString('<node id="node">Node</node>')
print(l.getElementById("node"))
Prints "None" on my computer.
I must be doing something here wrong but I can't figure it out!
I'm running Python 3.3.2 if that helps.
I used another approach to get Elemnts by ID (meaning the XML-attribute "id"), since I wanted to only use xml.dom.minidom.
Here is an example from my work:
#import minidom
from xml.dom.minidom import parse as p
#parse your XML-document
cmmn_doc = p("document.xml")
#Get all child nodes of your root-element or any element surrounding your "target" (in my example "cmmn:casePlanModel")
notelist = cmmn_doc.getElementsByTagName("cmmn:casePlanModel")[0].childNodes
#Now find the element via the id-tag
def find_element(id):
i=0
for i in range(len(notelist)):
if notelist[i].getAttribute("id") == id:
return notelist[i].nodeName #(or whatever you want to do)
#Call find_element with the id you are looking for
find_element(id)
XML from the example:
<cmmn:casePlanModel id="CasePlanModel_1" name="A CasePlanModel">
<cmmn:planItem id="PlanItem_1" definitionRef="Task_1" />
<cmmn:planItem id="PlanItem_08uai3q" definitionRef="HumanTask_0pgsk2i" />
<cmmn:planItem id="PlanItem_0crahv8" definitionRef="HumanTask_0jvecsr">
<cmmn:itemControl id="PlanItemControl_0tdwp8g">
<cmmn:repetitionRule id="RepetitionRule_03ky93m" />
<cmmn:requiredRule id="RequiredRule_1klzaio" />
<cmmn:manualActivationRule id="ManualActivationRule_1rek2bf" />
</cmmn:itemControl>
</cmmn:planItem>
<cmmn:planItem id="PlanItem_08kswcr" definitionRef="HumanTask_14zxi11" />
<cmmn:planItem id="PlanItem_12b1nkx" definitionRef="ProcessTask_10xuu3g">
<cmmn:exitCriterion id="EntryCriterion_09gio4l" sentryRef="Sentry_0hst9b5" />
</cmmn:planItem>
<cmmn:planItem id="PlanItem_1v34h5m" definitionRef="CaseTask_0hwjce3">
<cmmn:entryCriterion id="EntryCriterion_1j8r6j1" sentryRef="Sentry_1ii8w5d" />
</cmmn:planItem>
<cmmn:planItem id="PlanItem_0wroqsx" definitionRef="EventListener_17yxe7z" />
<cmmn:sentry id="Sentry_0hst9b5" />
<cmmn:sentry id="Sentry_1ii8w5d">
<cmmn:planItemOnPart id="PlanItemOnPart_1gt5jrc" sourceRef="PlanItem_12b1nkx"> <cmmn:standardEvent>complete</cmmn:standardEvent>
</cmmn:planItemOnPart>
<cmmn:planItemOnPart id="PlanItemOnPart_01b6uw3" sourceRef="PlanItem_0wroqsx"> <cmmn:standardEvent>occur</cmmn:standardEvent>
</cmmn:planItemOnPart>
</cmmn:sentry>
<cmmn:task id="Task_1" name="Simple Task" />
<cmmn:humanTask id="HumanTask_0pgsk2i" name="Human Task" />
<cmmn:humanTask id="HumanTask_0jvecsr" name="Human_Blocking" isBlocking="false" />
<cmmn:humanTask id="HumanTask_14zxi11" name="Human_mit_Anhang">
<cmmn:planningTable id="PlanningTable_1yxv7gm">
<cmmn:discretionaryItem id="DiscretionaryItem_0ne79yh" definitionRef="DecisionTask_1ecc5v8" />
</cmmn:planningTable>
</cmmn:humanTask>
<cmmn:decisionTask id="DecisionTask_1ecc5v8" name="Descritionary to Human Task" />
<cmmn:processTask id="ProcessTask_10xuu3g" name="Prozess Task" />
<cmmn:caseTask id="CaseTask_0hwjce3" name="Case Task" />
<cmmn:eventListener id="EventListener_17yxe7z" name="EventListener" />
</cmmn:casePlanModel>
I found this way more convenient.
If you want to get elements with name="node"
l.getElementsByTagName("node")
If you want to get elements with attribute having an attribute "id" with value "node", use xpath:
import xpath
xpath.find("//*['id=node']",l) #search for all elements with an attribute id="node"
From the instruction you typed, I understand you are trying to get the element which id value is node.
The solution is to loop over all your XML elements (well, you have only one in this situation, but it does not matter), and then check if that element has an attribute called id and the value of that attribute is node.
Let us translate this logic into a program:
>>> from xml.dom import minidom
>>> xml_string = '<node id="node">Node</node>'
>>> xml_doc = minidom.parseString(xml_string)
>>> elements = xml_doc.getElementsByTagName('node')
>>> for element in elements:
... if element.hasAttribute('id') and element.getAttribute('id') == 'node':
... print(element.toxml())
...
<node id="node">Node</node>

Categories

Resources