This is my XML file:
<?xml version="1.0" ?>
<Items>
<Item>
<ASIN>3570102769</ASIN>
<DetailPageURL>http://www.amazon.de/Inside-IS-Tage-Islamischen-Staat/dp/3570102769%3FSubscriptionId%3DAKIAI554OLCUMRCYB7ZA%26tag%3DjPp08vuSO4osfgfbCbEdF7TNqnWOm7YtprtqRPB9%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D3570102769</DetailPageURL>
<ItemLinks>
<ItemLink>
<Description>Add To Wishlist</Description>
<URL>http://www.amazon.de/gp/registry/wishlist/add-item.html%3Fasin.0%3D3570102769%26SubscriptionId%3DAKIAI554OLCUMRCYB7ZA%26tag%3DjPp08vuSO4osfgfbCbEdF7TNqnWOm7YtprtqRPB9%26linkCode%3Dxm2%26camp%3D2025%26creative%3D12738%26creativeASIN%3D3570102769</URL>
</ItemLink>
<ItemLink>
<Description>Tell A Friend</Description>
<URL>http://www.amazon.de/gp/pdp/taf/3570102769%3FSubscriptionId%3DAKIAI554OLCUMRCYB7ZA%26tag%3DjPp08vuSO4osfgfbCbEdF7TNqnWOm7YtprtqRPB9%26linkCode%3Dxm2%26camp%3D2025%26creative%3D12738%26creativeASIN%3D3570102769</URL>
</ItemLink>
<ItemLink>
<Description>All Customer Reviews</Description>
<URL>http://www.amazon.de/review/product/3570102769%3FSubscriptionId%3DAKIAI554OLCUMRCYB7ZA%26tag%3DjPp08vuSO4osfgfbCbEdF7TNqnWOm7YtprtqRPB9%26linkCode%3Dxm2%26camp%3D2025%26creative%3D12738%26creativeASIN%3D3570102769</URL>
</ItemLink>
<ItemLink>
<Description>All Offers</Description>
<URL>http://www.amazon.de/gp/offer-listing/3570102769%3FSubscriptionId%3DAKIAI554OLCUMRCYB7ZA%26tag%3DjPp08vuSO4osfgfbCbEdF7TNqnWOm7YtprtqRPB9%26linkCode%3Dxm2%26camp%3D2025%26creative%3D12738%26creativeASIN%3D3570102769</URL>
</ItemLink>
</ItemLinks>
<ItemAttributes>
<Author>Jürgen Todenhöfer</Author>
<Binding>Gebundene Ausgabe</Binding>
<EAN>9783570102763</EAN>
<EANList>
<EANListElement>9783570102763</EANListElement>
</EANList>
<ISBN>3570102769</ISBN>
<IsEligibleForTradeIn>1</IsEligibleForTradeIn>
<ItemDimensions>
<Height Units="hundredths-inches">874</Height>
<Length Units="hundredths-inches">575</Length>
<Width Units="hundredths-inches">126</Width>
</ItemDimensions>
<Label>C. Bertelsmann Verlag</Label>
<Languages>
<Language>
<Name>Deutsch</Name>
<Type>Published</Type>
</Language>
<Language>
<Name>Deutsch</Name>
<Type>Original</Type>
</Language>
<Language>
<Name>Deutsch</Name>
<Type>Unbekannt</Type>
</Language>
</Languages>
<ListPrice>
<Amount>1799</Amount>
<CurrencyCode>EUR</CurrencyCode>
<FormattedPrice>EUR 17,99</FormattedPrice>
</ListPrice>
<Manufacturer>C. Bertelsmann Verlag</Manufacturer>
<ManufacturerMinimumAge Units="months">192</ManufacturerMinimumAge>
<NumberOfPages>288</NumberOfPages>
<PackageDimensions>
<Height Units="hundredths-inches">118</Height>
<Length Units="hundredths-inches">567</Length>
<Weight Units="hundredths-pounds">93</Weight>
<Width Units="hundredths-inches">252</Width>
</PackageDimensions>
<PackageQuantity>1</PackageQuantity>
<ProductGroup>Book</ProductGroup>
<ProductTypeName>ABIS_BOOK</ProductTypeName>
<PublicationDate>2015-04-27</PublicationDate>
<Publisher>C. Bertelsmann Verlag</Publisher>
<Studio>C. Bertelsmann Verlag</Studio>
<Title>Inside IS - 10 Tage im 'Islamischen Staat'</Title>
<TradeInValue>
<Amount>930</Amount>
<CurrencyCode>EUR</CurrencyCode>
<FormattedPrice>EUR 9,30</FormattedPrice>
</TradeInValue>
</ItemAttributes>
<OfferSummary>
<LowestNewPrice>
<Amount>1799</Amount>
<CurrencyCode>EUR</CurrencyCode>
<FormattedPrice>EUR 17,99</FormattedPrice>
</LowestNewPrice>
<LowestUsedPrice>
<Amount>1390</Amount>
<CurrencyCode>EUR</CurrencyCode>
<FormattedPrice>EUR 13,90</FormattedPrice>
</LowestUsedPrice>
<LowestCollectiblePrice>
<Amount>4999</Amount>
<CurrencyCode>EUR</CurrencyCode>
<FormattedPrice>EUR 49,99</FormattedPrice>
</LowestCollectiblePrice>
<TotalNew>56</TotalNew>
<TotalUsed>8</TotalUsed>
<TotalCollectible>1</TotalCollectible>
<TotalRefurbished>0</TotalRefurbished>
</OfferSummary>
<Offers>
<TotalOffers>1</TotalOffers>
<TotalOfferPages>1</TotalOfferPages>
<MoreOffersUrl>http://www.amazon.de/gp/offer-listing/3570102769%3FSubscriptionId%3DAKIAI554OLCUMRCYB7ZA%26tag%3DjPp08vuSO4osfgfbCbEdF7TNqnWOm7YtprtqRPB9%26linkCode%3Dxm2%26camp%3D2025%26creative%3D12738%26creativeASIN%3D3570102769</MoreOffersUrl>
<Offer>
<OfferAttributes>
<Condition>New</Condition>
</OfferAttributes>
<OfferListing>
<OfferListingId>9KHCZj9qtL6ucVBPASfXaryQjU8tWbc0n%2F3F4F7GraOKW6Csji2OxpD93%2FkoHwgIGQctlnrtx4RWIeJULAcvvsFhiopFi08JdsZ%2FeO3u6g0%3D</OfferListingId>
<Price>
<Amount>1799</Amount>
<CurrencyCode>EUR</CurrencyCode>
<FormattedPrice>EUR 17,99</FormattedPrice>
</Price>
<Availability>Gewöhnlich versandfertig in 24 Stunden</Availability>
<AvailabilityAttributes>
<AvailabilityType>now</AvailabilityType>
<MinimumHours>0</MinimumHours>
<MaximumHours>0</MaximumHours>
</AvailabilityAttributes>
<IsEligibleForSuperSaverShipping>1</IsEligibleForSuperSaverShipping>
</OfferListing>
</Offer>
</Offers>
</Item>
<Item>
<ASIN>3813506479</ASIN>
<DetailPageURL>http://www.amazon.de/Altes-Land-Roman-D%C3%B6rte-Hansen/dp/3813506479%3FSubscriptionId%3DAKIAI554OLCUMRCYB7ZA%26tag%3DjPp08vuSO4osfgfbCbEdF7TNqnWOm7YtprtqRPB9%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D3813506479</DetailPageURL>
<ItemLinks>
<ItemLink>
<Description>Add To Wishlist</Description>
<URL>http://www.amazon.de/gp/registry/wishlist/add-item.html%3Fasin.0%3D3813506479%26SubscriptionId%3DAKIAI554OLCUMRCYB7ZA%26tag%3DjPp08vuSO4osfgfbCbEdF7TNqnWOm7YtprtqRPB9%26linkCode%3Dxm2%26camp%3D2025%26creative%3D12738%26creativeASIN%3D3813506479</URL>
</ItemLink>
<ItemLink>
<Description>Tell A Friend</Description>
<URL>http://www.amazon.de/gp/pdp/taf/3813506479%3FSubscriptionId%3DAKIAI554OLCUMRCYB7ZA%26tag%3DjPp08vuSO4osfgfbCbEdF7TNqnWOm7YtprtqRPB9%26linkCode%3Dxm2%26camp%3D2025%26creative%3D12738%26creativeASIN%3D3813506479</URL>
</ItemLink>
<ItemLink>
<Description>All Customer Reviews</Description>
<URL>http://www.amazon.de/review/product/3813506479%3FSubscriptionId%3DAKIAI554OLCUMRCYB7ZA%26tag%3DjPp08vuSO4osfgfbCbEdF7TNqnWOm7YtprtqRPB9%26linkCode%3Dxm2%26camp%3D2025%26creative%3D12738%26creativeASIN%3D3813506479</URL>
</ItemLink>
<ItemLink>
<Description>All Offers</Description>
<URL>http://www.amazon.de/gp/offer-listing/3813506479%3FSubscriptionId%3DAKIAI554OLCUMRCYB7ZA%26tag%3DjPp08vuSO4osfgfbCbEdF7TNqnWOm7YtprtqRPB9%26linkCode%3Dxm2%26camp%3D2025%26creative%3D12738%26creativeASIN%3D3813506479</URL>
</ItemLink>
</ItemLinks>
<ItemAttributes>
<Author>Dörte Hansen</Author>
<Binding>Gebundene Ausgabe</Binding>
<EAN>9783813506471</EAN>
<EANList>
<EANListElement>9783813506471</EANListElement>
</EANList>
<ISBN>3813506479</ISBN>
<IsEligibleForTradeIn>1</IsEligibleForTradeIn>
<ItemDimensions>
<Height Units="hundredths-inches">870</Height>
<Length Units="hundredths-inches">567</Length>
<Width Units="hundredths-inches">114</Width>
</ItemDimensions>
<Label>Albrecht Knaus Verlag</Label>
<Languages>
<Language>
<Name>Deutsch</Name>
<Type>Published</Type>
</Language>
<Language>
<Name>Deutsch</Name>
<Type>Original</Type>
</Language>
</Languages>
<ListPrice>
<Amount>1999</Amount>
<CurrencyCode>EUR</CurrencyCode>
<FormattedPrice>EUR 19,99</FormattedPrice>
</ListPrice>
<Manufacturer>Albrecht Knaus Verlag</Manufacturer>
<NumberOfPages>288</NumberOfPages>
<PackageDimensions>
<Height Units="hundredths-inches">118</Height>
<Length Units="hundredths-inches">858</Length>
<Weight Units="hundredths-pounds">101</Weight>
<Width Units="hundredths-inches">559</Width>
</PackageDimensions>
<ProductGroup>Book</ProductGroup>
<ProductTypeName>ABIS_BOOK</ProductTypeName>
<PublicationDate>2015-02-16</PublicationDate>
<Publisher>Albrecht Knaus Verlag</Publisher>
<Studio>Albrecht Knaus Verlag</Studio>
<Title>Altes Land: Roman</Title>
<TradeInValue>
<Amount>965</Amount>
<CurrencyCode>EUR</CurrencyCode>
<FormattedPrice>EUR 9,65</FormattedPrice>
</TradeInValue>
</ItemAttributes>
<OfferSummary>
<LowestNewPrice>
<Amount>1999</Amount>
<CurrencyCode>EUR</CurrencyCode>
<FormattedPrice>EUR 19,99</FormattedPrice>
</LowestNewPrice>
<LowestUsedPrice>
<Amount>1599</Amount>
<CurrencyCode>EUR</CurrencyCode>
<FormattedPrice>EUR 15,99</FormattedPrice>
</LowestUsedPrice>
<TotalNew>72</TotalNew>
<TotalUsed>8</TotalUsed>
<TotalCollectible>0</TotalCollectible>
<TotalRefurbished>0</TotalRefurbished>
</OfferSummary>
<Offers>
<TotalOffers>1</TotalOffers>
<TotalOfferPages>1</TotalOfferPages>
<MoreOffersUrl>http://www.amazon.de/gp/offer-listing/3813506479%3FSubscriptionId%3DAKIAI554OLCUMRCYB7ZA%26tag%3DjPp08vuSO4osfgfbCbEdF7TNqnWOm7YtprtqRPB9%26linkCode%3Dxm2%26camp%3D2025%26creative%3D12738%26creativeASIN%3D3813506479</MoreOffersUrl>
<Offer>
<OfferAttributes>
<Condition>New</Condition>
</OfferAttributes>
<OfferListing>
<OfferListingId>aeRv5KPt26T8S0hLrgV8Bv9UPYABYOMijGRxffbNJXUZSN4XfeeOZZpCZ28EURzmgMLlcYEBSRlMXS%2F8Z0pN1JbYerndME%2B2VK3RosfdQJA%3D</OfferListingId>
<Price>
<Amount>1999</Amount>
<CurrencyCode>EUR</CurrencyCode>
<FormattedPrice>EUR 19,99</FormattedPrice>
</Price>
<Availability>Gewöhnlich versandfertig in 24 Stunden</Availability>
<AvailabilityAttributes>
<AvailabilityType>now</AvailabilityType>
<MinimumHours>0</MinimumHours>
<MaximumHours>0</MaximumHours>
</AvailabilityAttributes>
<IsEligibleForSuperSaverShipping>1</IsEligibleForSuperSaverShipping>
</OfferListing>
</Offer>
</Offers>
</Item>
</Items>
I want to get any ASIN element. So I tried this:
from lxml import etree
doc = etree.fromstring(xmlstring)
items = doc.xpath('//Items/Item')
for a in items:
asin = a.xpath('//ASIN/text()')
print asin
What I get is this:
['3570102769', '3813506479']
['3570102769', '3813506479']
But I want this:
['3570102769']
['3813506479']
I don't understand what's the problem here? I think I should iterate over any element and in every element is one item with one asin. Why does it return two times two asin?
When you're searching for a.xpath('//ASIN/text()') you're searching the complete document tree again. Quoting from the XML Path language specification:
//para selects all the para descendants of the document root and thus selects all para elements in the same document as the context node
So what you're doing is iterating over the matched Item nodes and saying "Give me all ASIN nodes in this document please". The context for this (the Item node) is ignored.
What you should do instead, is directly select the ASIN child-node directly. Keeping to your original implementation this could look like this:
doc = etree.fromstring(xmlstring)
items = doc.xpath('//Items/Item')
for a in items:
asin = a.xpath('ASIN/text()')
print asin
which gives the output you desire:
['3570102769']
['3813506479']
Alternatively, if you're not certain where in the Item node your ASIN appears, you could use .//ASIN/text()
Related
I am facing with another problem.
I have kind of XML file with example code:
<case classname="perf" name="device">
<Type>GET</Type>
<Name>xxx</Name>
<Request>Something</Request>
</case>
<case2 name="device_n>
<Type>GET</Type>
<Name>yyy</Name>
<Request>Something</Request>
</case2>
<case...n>
...
</case...n>
How can I replace xxx to be named 'device' and yyy in case2 to be device_n?
I think something like that could be OK, but don't know how to write it:
with open('some_xml.xml') as parsed:
if line in parsed.readlines.startswith("name="):
line.replace('', "name=... #what's now?
This should be somekind of iterator because I have a lot of cases.
There may be a more clever way to do this with regex, but basic string searching seems to work:
import io
data = '''
<case classname="perf" name="device">
<Type>GET</Type>
<Name>xxx</Name>
<Request>Something</Request>
</case>
<case2 name="device_n">
<Type>GET</Type>
<Name>yyy</Name>
<Request>Something</Request>
</case2>'''
strOut="" # new string
f = io.StringIO(data) # load string data as file text
for rowx in f: # each line in data
row = rowx.rstrip() # remove newline
if ' name="' in row: # <case name=...>
nm = row[row.index(' name="')+7:len(row)-2] # get name
if "<Name>" in row and nm: # <Name>....
idx = row.index("<Name>") + 6
strOut += row[:idx] + nm + "</Name>\n" # set name field here
nm = "" # in case bad block, don't copy wrong name
else:
strOut += row + "\n" # copy line to new string
print(strOut)
Output
<case classname="perf" name="device">
<Type>GET</Type>
<Name>device</Name>
<Request>Something</Request>
</case>
<case2 name="device_n">
<Type>GET</Type>
<Name>device_n</Name>
<Request>Something</Request>
</case2>
A different approach (using xml parsing)
import xml.etree.ElementTree as ET
to_replace = {".//case/Name":'device',".//case2/Name":'device_n'}
xml = '''<r>
<case classname="perf" name="device">
<Type>GET</Type>
<Name>xxx</Name>
<Request>Something</Request>
</case>
<case2 name="device_n">
<Type>GET</Type>
<Name>yyy</Name>
<Request>Something</Request>
</case2>
</r>'''
root = ET.fromstring(xml)
for x_path,new_val in to_replace.items():
ele = root.find(x_path)
ele.text = new_val
ET.dump(root)
output
<r>
<case classname="perf" name="device">
<Type>GET</Type>
<Name>device</Name>
<Request>Something</Request>
</case>
<case2 name="device_n">
<Type>GET</Type>
<Name>device_n</Name>
<Request>Something</Request>
</case2>
</r>
I have a text file with entries like this:
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<Applications_GetResponse xmlns="http://www.country.com">
<Applications>
<CS_Application>
<Name>Spain</Name>
<Key>2345364564</Key>
<Status>NORMAL</Status>
<Modules>
<CS_Module>
<Name>zaragoza</Name>
<Key>8743249725</Key>
<DevelopmentEffort>0</DevelopmentEffort>
<LogicalDBConnections/>
</CS_Module>
<CS_Module>
<Name>malaga</Name>
<Key>8743249725</Key>
<DevelopmentEffort>0</DevelopmentEffort>
<LogicalDBConnections/>
</CS_Module>
</Modules>
<CreatedBy>7</CreatedBy>
</CS_Application>
<CS_Application>
<Name>UK</Name>
<Key>2345364564</Key>
<Status>NORMAL</Status>
<Modules>
<CS_Module>
<Name>london</Name>
<Key>8743249725</Key>
<DevelopmentEffort>0</DevelopmentEffort>
<LogicalDBConnections/>
</CS_Module>
<CS_Module>
<Name>liverpool</Name>
<Key>8743249725</Key>
<DevelopmentEffort>0</DevelopmentEffort>
<LogicalDBConnections/>
</CS_Module>
</Modules>
<CreatedBy>7</CreatedBy>
</CS_Application>
</Applications>
</Applications_GetResponse>
</soap:Body>
</soap:Envelope>
I would like to analyze it and obtain the name of the country in the sequence of the cities.
I tried some things with python re.finall, but I didn't get anything like it
print("HERE APPLICATIONS")
applications = re.findall('<CS_Application><Name>(.*?)</Name>', response_apply.text)
print(applications)
print("HERE MODULES")
modules = re.findall('<CS_Module><Name>(.*?)</Name>', response_apply.text)
print(modules)
return:
host-10$ sudo python3 capture.py
HERE APPLICATIONS
['Spain', 'UK']
HERE MODULES
['zaragoza', 'malaga', 'london', 'liverpool']
The expected result is, I would like the result to be like this:
HERE
The Country: Spain - Cities: zaragoza,malaga
The Country: UK - Cities: london,liverpool
Regex is not good to parse xml. Better use xml parser..
If you want regex solution then hope below code help you.
import re
s = """\n<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">\n <soap:Body>\n <Applications_GetResponse xmlns="http://www.country.com">\n <Applications>\n <CS_Application>\n <Name>Spain</Name>\n <Key>2345364564</Key>\n <Status>NORMAL</Status>\n <Modules>\n <CS_Module>\n <Name>zaragoza</Name>\n <Key>8743249725</Key>\n <DevelopmentEffort>0</DevelopmentEffort>\n <LogicalDBConnections/>\n </CS_Module>\n <CS_Module>\n <Name>malaga</Name>\n <Key>8743249725</Key>\n <DevelopmentEffort>0</DevelopmentEffort>\n <LogicalDBConnections/>\n </CS_Module>\n </Modules>\n <CreatedBy>7</CreatedBy>\n </CS_Application>\n <CS_Application>\n <Name>UK</Name>\n <Key>2345364564</Key>\n <Status>NORMAL</Status>\n <Modules>\n <CS_Module>\n <Name>london</Name>\n <Key>8743249725</Key>\n <DevelopmentEffort>0</DevelopmentEffort>\n <LogicalDBConnections/>\n </CS_Module>\n <CS_Module>\n <Name>liverpool</Name>\n <Key>8743249725</Key>\n <DevelopmentEffort>0</DevelopmentEffort>\n <LogicalDBConnections/>\n </CS_Module>\n </Modules>\n <CreatedBy>7</CreatedBy>\n </CS_Application>\n </Applications>\n </Applications_GetResponse>\n </soap:Body>\n</soap:Envelope>\n"""
pattern1 = re.compile(r'<CS_Application>([\s\S]*?)</CS_Application>')
pattern2 = re.compile(r'<Name>(.*)?</Name>')
for m in re.finditer(pattern1, s):
ss = m.group(1)
res = []
for mm in re.finditer(pattern2, ss):
res.append(mm.group(1))
print("The Country: "+res[0]+" - Cities: "+",".join(res[1:len(res)]))
I need to generate and sign an XML. I'm using Windows 10.
I've a certificate in PFX format (also known as P12). I've succesfully extracted the key and cert from that file, in order to sign an XML.
But now I need to know how to sign that XML? More exactly, how to generate the content for this XMl elements: ds:DigestValue, ds:SignatureValue and ds:X509Certificate???
According to the documentation of the entity that will verify the signed XML, this elements are:
ds:SignatureValue: contains the base64 coded sign. The sign is the result of a series of transformations over the binari data of the element <ds:SignedInfo>. The element <ds:SignatureValue> contains this binari value of the coded sign in base64.
ds:DigestMethod: defines the hash function to be used, through the Algorithm atribute.
ds: DigestValue: Is the hash value in base64.
ds:X509Certificate: it is just the sign.
The final XML has other parts that holds different information, but I like to understand how to construct the mentioned tags:
<ds:Signature Id="IDSignSA">
<ds:SignedInfo>
<ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
<ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
<ds:Reference URI="">
<ds:Transforms>
<ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#envelopedsignature"/>
</ds:Transforms>
<ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<ds:DigestValue>X4V0Z4K8CNcrud9vojN Iy/8hjkk=</ds:DigestValue>
</ds:Reference>
</ds:SignedInfo>
<ds:SignatureValue>U8bSaZUgOiD F3VDtwFRPiZ/6AQkSht7ezF8QVv+g5ELiLqkQHmRoL7VMLUtaHr+A9VhkIb5RZU5P EoRcvxI9v37zJg2WUe2wpKAY7AHm7kvvUHEs09K8Q+c0rqNaAAK1kvXPHbVFXnn0o2oLWI1bI/rS sWsFtjdmVtyLjkDEzf8=</ds:SignatureValue>
<ds:KeyInfo>
<ds:X509Data>
<ds:X509SubjectName>1. 2.840.113549.1.9.1=#161a4253554c434140534f55544845524e504552552e434f4d2e5045,CN=Pedro Solano,OU=10261110983,O=SOLANO ALMAGRO PEDRO,L=TACNA,ST=TACNA,C=PE</ds:X509SubjectName>
<ds:X509Certificate>MIIESTCCAzGgAwIBAgI
KWOCRzgAAAAAAIjANBgkqhkiG9w0BAQUFADAnMRUwEwYKCZImiZPyLGQB
GRYFU1VOQVQxDjAMBgNVBAMTBVNVTkFUMB4XDTEwMTIyODE5NTExMFoXDTExMTIyODIwMDExMFow
gZUxCzAJBgNVBAYTAlBFMQ0wCwYDVQQIEwRMSU1BMQ0wCwYDVQQHEwRMSU1BMREwDwYDVQQKEwhT
T1VUSEVSTjEUMBIGA1UECxMLMjAxMDAxNDc1MTQxFDASBgNVBAMTC0JvcmlzIFN1bGNhMSkwJwYJ
KoZIhvcNAQkBFhpCU1VMQ0FAU09VVEhFUk5QRVJVLkNPTS5QRTCBnzANBgkqhkiG9w0BAQEFAAOB
jQAwgYkCgYEAtRtcpfBLzyajuEmYt4mVH8EE02KQiETsdKStUThVYM7g3Lkx5zq3SH5nLH00EKGC
tota6RR+V40sgIbnh+Nfs1SOQcAohNwRfWhho7sKNZFR971rFxj4cTKMEvpt8Dr98UYFkJhph6Wn
sniGM2tJDq9KJ52UXrlScMfBityx0AsCAwEAAaOCAYowggGGMA4GA1UdDwEB/wQEAwIE8DBEBgkq
hkiG9w0BCQ8ENzA1MA4GCCqGSIb3DQMCAgIAgDAOBggqhkiG9w0DBAICAIAwBwYFKw4DAgcwCgYI
KoZIhvcNAwcwHQYDVR0OBBYEFG/m6twbiRNzRINavjq+U0j/sZECMBMGA1UdJQQMMAoGCCsGAQUF
BwMCMB8GA1UdIwQYMBaAFN9kHQDqWONmozw3xdNSIMFW2t+7MFkGA1UdHwRSMFAwTqBMoEqGImh0
dHA6Ly9wY2IyMjYvQ2VydEVucm9sbC9TVU5BVC5jcmyGJGZpbGU6Ly9cXHBjYjIyNlxDZXJ0RW5y
b2xsXFNVTkFULmNybDB+BggrBgEFBQcBAQRyMHAwNQYIKwYBBQUHMAKGKWh0dHA6Ly9wY2IyMjYv
Q2VydEVucm9sbC9wY2IyMjZfU1VOQVQuY3J0MDcGCCsGAQUFBzAChitmaWxlOi8vXFxwY2IyMjZc
Q2VydEVucm9sbFxwY2IyMjZfU1VOQVQuY3J0MA0GCSqGSIb3DQEBBQUAA4IBAQBI6wJ/QmRpz3C3
rorBflOvA9DOa3GNiiB7rtPIjF4mPmtgfo2pK9gvnxmV2pST3ovfu0nbG2kpjzzaaelRjEodHvkc
M3abGsOE53wfxqQF5uf/jkzZA9hbLHtE1aLKBD0Mhzc6cvI072alnE6QU3RZ16ie9CYsHmMrs+sP
HMy8DJU5YrdnqHdSn2D3nhKBi4QfT/WURPOuo6DF4iWgrCyMf3eJgmGKSUN3At5fK4HSpfyURT0k
boaJKNBgQwy0HhGh5BLM7DsTi/KwfdUYkoFgrY71Pm23+ra+xTow1Vk9gj5NqrlpMY5gAVQXEIo1
++GxDtaK/5EiVKSqzJ6geIfz</ds:X509Certificate>
</ds:X509Data>
</ds:KeyInfo>
</ds:Signature>
<cac:Signature>
<cbc:ID>IDSignSA</cbc:ID>
<cac:SignatoryParty>
<cac:PartyIdentification>
<cbc:ID>10261110983</cbc:ID>
</cac:PartyIdentification>
<cac:PartyName>
<cbc:Name>SOLANO ALMAGRO PEDRO</cbc:Name>
</cac:PartyName>
</cac:SignatoryParty>
<cac:DigitalSignatureAttachment>
<cac:ExternalReference>
<cbc:URI>#signature</cbc:URI>
</cac:ExternalReference>
</cac:DigitalSignatureAttachment>
</cac:Signature>
I've found the signxml library. And it has an example:
from lxml import etree
from signxml import XMLSigner, XMLVerifier
data_to_sign = "<Test/>"
cert = open("example.pem").read()
key = open("example.key").read()
root = etree.fromstring(data_to_sign)
signed_root = XMLSigner().sign(root, key=key, cert=cert)
verified_data = XMLVerifier().verify(signed_root).signed_xml
But would this example generate the contents for the 3 tags?
UPDATE 1:
I've tried to put my XML like this to sign it but getting an error:
from lxml import etree
from signxml import XMLSigner, XMLVerifier
passwd = 'caballo123'
cd = 'D:\\facturacion_electronica\\cetificado_prueba\\'
my_data_to_sign = '''
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
xmlns:ccts="urn:un:unece:uncefact:documentation:2"
xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"
xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2"
xmlns:sac="urn:sunat:names:specification:ubl:peru:schema:xsd:SunatAggregateComponents-1"
xmlns:udt="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ext:UBLExtensions>
<ext:UBLExtension>
<ext:ExtensionContent>
<sac:AdditionalInformation>
<sac:AdditionalMonetaryTotal>
<cbc:ID>1001</cbc:ID>
<cbc:PayableAmount currencyID="PEN">1407.29</cbc:PayableAmount>
</sac:AdditionalMonetaryTotal>
<sac:AdditionalMonetaryTotal>
<cbc:ID>1004</cbc:ID>
<cbc:PayableAmount currencyID="PEN">48.00</cbc:PayableAmount>
</sac:AdditionalMonetaryTotal>
<sac:AdditionalMonetaryTotal>
<cbc:ID>2005</cbc:ID>
<cbc:PayableAmount currencyID="PEN">74.07</cbc:PayableAmount>
</sac:AdditionalMonetaryTotal>
<sac:AdditionalProperty>
<cbc:ID>1000</cbc:ID>
<cbc:Value>SON MIL SEISCIENTOS SESENTA Y 60/100</cbc:Value>
</sac:AdditionalProperty>
</sac:AdditionalInformation>
</ext:ExtensionContent>
</ext:UBLExtension>
<ext:UBLExtension>
<ext:ExtensionContent>
<ds:Signature Id="signatureKG">
<ds:SignedInfo>
<ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
<ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
<ds:Reference URI="">
<ds:Transforms>
<ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#envelopedsignature"/>
</ds:Transforms>
<ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<ds:DigestValue></ds:DigestValue>
</ds:Reference>
</ds:SignedInfo>
<ds:SignatureValue></ds:SignatureValue>
<ds:KeyInfo>
<ds:X509Data>
<ds:X509SubjectName>1.2 .840.113549.1.9.1=#161a4253554c434140534f55544845524e504552552e434f4d2e5045,CN=Carlos Vega,OU=10200545523,O=Vega Poblete Carlos Enrique,L=CHICLAYO,ST=LAMBAYEQUE,C=PE</ds:X509SubjectName>
<ds:X509Certificate></ds:X509Certificate>
</ds:X509Data>
</ds:KeyInfo>
</ds:Signature>
</ext:ExtensionContent>
</ext:UBLExtension>
</ext:UBLExtensions>
<cbc:UBLVersionID>2.0</cbc:UBLVersionID>
<cbc:CustomizationID>1.0</cbc:CustomizationID>
<cbc:ID>BC01-3652</cbc:ID>
<cbc:IssueDate>2012-06-24</cbc:IssueDate>
<cbc:InvoiceTypeCode>03</cbc:InvoiceTypeCode>
<cbc:DocumentCurrencyCode>PEN</cbc:DocumentCurrencyCode>
<cac:Signature>
<cbc:ID>IDSignKG</cbc:ID>
<cac:SignatoryParty>
<cac:PartyIdentification>
<cbc:ID>10200545523</cbc:ID>
</cac:PartyIdentification>
<cac:PartyName>
<cbc:Name>VEGA POBLETE CARLOS ENRIQUE</cbc:Name>
</cac:PartyName>
</cac:SignatoryParty>
<cac:DigitalSignatureAttachment>
<cac:ExternalReference>
<cbc:URI>#SignatureKG</cbc:URI>
</cac:ExternalReference>
</cac:DigitalSignatureAttachment>
</cac:Signature>
<cac:AccountingSupplierParty>
<cbc:CustomerAssignedAccountID>10200545523</cbc:CustomerAssignedAccountID>
<cbc:AdditionalAccountID>6</cbc:AdditionalAccountID>
<cac:Party>
<cac:PartyName>
<cbc:Name>ELECTRODOMESTICOS CRUZ DE MOTUPE</cbc:Name>
</cac:PartyName>
<cac:PostalAddress>
<cbc:ID>140106</cbc:ID>
<cbc:StreetName>AV. LOS TALLANES #235</cbc:StreetName>
<cbc:CitySubdivisionName>URB. MIGUEL GRAU</cbc:CitySubdivisionName>
<cbc:CityName>CHICLAYO</cbc:CityName>
<cbc:CountrySubentity>LAMBAYEQUE</cbc:CountrySubentity>
<cbc:District>LA VICTORIA</cbc:District>
<cac:Country>
<cbc:IdentificationCode>PE</cbc:IdentificationCode>
</cac:Country>
</cac:PostalAddress>
<cac:PartyLegalEntity>
<cbc:RegistrationName>VEGA POBLETE CARLOS ENRIQUE</cbc:RegistrationName>
</cac:PartyLegalEntity>
</cac:Party>
</cac:AccountingSupplierParty>
<cac:AccountingCustomerParty>
<cbc:CustomerAssignedAccountID>00078647</cbc:CustomerAssignedAccountID>
<cbc:AdditionalAccountID>1</cbc:AdditionalAccountID>
<cac:Party>
<cac:PartyLegalEntity>
<cbc:RegistrationName>SOLEDAD ASUNCION CARRASCO PEREZ</cbc:RegistrationName>
</cac:PartyLegalEntity>
</cac:Party>
</cac:AccountingCustomerParty>
<cac:TaxTotal>
<cbc:TaxAmount currencyID="PEN">253.31</cbc:TaxAmount>
<cac:TaxSubtotal>
<cbc:TaxAmount currencyID="PEN">253.31</cbc:TaxAmount>
<cac:TaxCategory>
<cac:TaxScheme>
<cbc:ID>1000</cbc:ID>
<cbc:Name>IGV</cbc:Name>
<cbc:TaxTypeCode>VAT</cbc:TaxTypeCode>
</cac:TaxScheme>
</cac:TaxCategory>
</cac:TaxSubtotal>
</cac:TaxTotal>
<cac:LegalMonetaryTotal>
<cbc:PayableAmount currencyID="PEN">1660.60</cbc:PayableAmount>
</cac:LegalMonetaryTotal>
<cac:InvoiceLine>
<cbc:ID>1</cbc:ID>
<cbc:InvoicedQuantity unitCode="NIU">1</cbc:InvoicedQuantity>
<cbc:LineExtensionAmount currencyID="PEN">845.76</cbc:LineExtensionAmount>
<cac:PricingReference>
<cac:AlternativeConditionPrice>
<cbc:PriceAmount currencyID="PEN">998.00</cbc:PriceAmount>
<cbc:PriceTypeCode>01</cbc:PriceTypeCode>
</cac:AlternativeConditionPrice>
</cac:PricingReference>
<cac:TaxTotal>
<cbc:TaxAmount currencyID="PEN">152.24</cbc:TaxAmount>
<cac:TaxSubtotal>
<cbc:TaxAmount currencyID="PEN">152.24</cbc:TaxAmount>
<cac:TaxCategory>
<cbc:TaxExemptionReasonCode>10</cbc:TaxExemptionReasonCode>
<cac:TaxScheme>
<cbc:ID>1000</cbc:ID>
<cbc:Name>IGV</cbc:Name>
<cbc:TaxTypeCode>VAT</cbc:TaxTypeCode>
</cac:TaxScheme>
</cac:TaxCategory>
</cac:TaxSubtotal>
</cac:TaxTotal>
<cac:Item>
<cbc:Description>Refrigeradora marca "AXM" no frost de 200 ltrs</cbc:Description>
<cac:SellersItemIdentification>
<cbc:ID>REF564</cbc:ID>
</cac:SellersItemIdentification>
</cac:Item>
<cac:Price>
<cbc:PriceAmount currencyID="PEN">845.76</cbc:PriceAmount>
</cac:Price>
</cac:InvoiceLine>
<cac:InvoiceLine>
<cbc:ID>2</cbc:ID>
<cbc:InvoicedQuantity unitCode="NIU">1</cbc:InvoicedQuantity>
<cbc:LineExtensionAmount currencyID="PEN">635.59</cbc:LineExtensionAmount>
<cac:PricingReference>
<cac:AlternativeConditionPrice>
<cbc:PriceAmount currencyID="PEN">750.00</cbc:PriceAmount>
<cbc:PriceTypeCode>01</cbc:PriceTypeCode>
</cac:AlternativeConditionPrice>
</cac:PricingReference>
<cac:TaxTotal>
<cbc:TaxAmount currencyID="PEN">114.41</cbc:TaxAmount>
<cac:TaxSubtotal>
<cbc:TaxAmount currencyID="PEN">114.41</cbc:TaxAmount>
<cac:TaxCategory>
<cbc:TaxExemptionReasonCode>10</cbc:TaxExemptionReasonCode>
<cac:TaxScheme>
<cbc:ID>1000</cbc:ID>
<cbc:Name>IGV</cbc:Name>
<cbc:TaxTypeCode>VAT</cbc:TaxTypeCode>
</cac:TaxScheme>
</cac:TaxCategory>
</cac:TaxSubtotal>
</cac:TaxTotal>
<cac:Item>
<cbc:Description>Cocina a gas GLP, marca "AXM" de 5 hornillas</cbc:Description>
<cac:SellersItemIdentification>
<cbc:ID>COC124</cbc:ID>
</cac:SellersItemIdentification>
</cac:Item>
<cac:Price>
<cbc:PriceAmount currencyID="PEN">635.59</cbc:PriceAmount>
</cac:Price>
</cac:InvoiceLine>
<cac:InvoiceLine>
<cbc:ID>3</cbc:ID>
<cbc:InvoicedQuantity unitCode="NIU">1</cbc:InvoicedQuantity>
<cbc:LineExtensionAmount currencyID="PEN">0.00</cbc:LineExtensionAmount>
<cac:PricingReference>
<cac:AlternativeConditionPrice>
<cbc:PriceAmount currencyID="PEN">0.00</cbc:PriceAmount>
<cbc:PriceTypeCode>01</cbc:PriceTypeCode>
</cac:AlternativeConditionPrice>
<cac:AlternativeConditionPrice>
<cbc:PriceAmount currencyID="PEN">4.80</cbc:PriceAmount>
<cbc:PriceTypeCode>02</cbc:PriceTypeCode>
</cac:AlternativeConditionPrice>
</cac:PricingReference>
<cac:TaxTotal>
<cbc:TaxAmount currencyID="PEN">0.00</cbc:TaxAmount>
<cac:TaxSubtotal>
<cbc:TaxAmount currencyID="PEN">0.00</cbc:TaxAmount>
<cac:TaxCategory>
<cbc:TaxExemptionReasonCode>31</cbc:TaxExemptionReasonCode>
<cac:TaxScheme>
<cbc:ID>1000</cbc:ID>
<cbc:Name>IGV</cbc:Name>
<cbc:TaxTypeCode>VAT</cbc:TaxTypeCode>
</cac:TaxScheme>
</cac:TaxCategory>
</cac:TaxSubtotal>
</cac:TaxTotal>
<cac:Item>
<cbc:Description>Sixpack de gaseosa "Guaraná" de 400 ml.</cbc:Description>
<cac:SellersItemIdentification>
<cbc:ID>NOB012</cbc:ID>
</cac:SellersItemIdentification>
</cac:Item>
<cac:Price>
<cbc:PriceAmount currencyID="PEN">0.00</cbc:PriceAmount>
</cac:Price>
</cac:InvoiceLine>
</Invoice>
'''
data_to_sign = my_data_to_sign
cert = open("llama_cert.pem").read()
key = open("llama.key").read()
root = etree.fromstring(data_to_sign)
signed_root = XMLSigner().sign(root, key=key, cert=cert)
verified_data = XMLVerifier().verify(signed_root, x509_cert=cert).signed_xml
print(verified_data)
I have an xml with a structure as follows:
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://sumo.dlr.de/xsd/routes_file.xsd">
<vType id="Bus" vClass="ignoring" guiShape="bus" color="cyan"/>
<vehicle id="1.0" type="Bus" depart="0.00">
<route edges="207358226 206878618#0 206878618#1 206878618#2 206878571 206878624#0 195427225#1 25450515 171767377#0 171767377#1 195427224#0 96336873 96336870"/>
</vehicle>
<vehicle id="2.0" type="Taxi" depart="0.00">
<route edges="172428613 -25301974#1 -25301974#0 172428582 -172428593 -25301969#5 -25301969#4 -165310768#1 -165310768#0 -45073854#4 -45073854#3 -45073854#0 -32932418#2 172436826#1 172436826#2 172436826#3 172436826#4 172436826#5 172405270#0 24629564 172405301#1 -172405301#1 -24629564 -172405270#0"/>
</vehicle>
<vehicle id="1.1" type="Bus" depart="0.00">
<route edges="207358226 206878618#0 206878618#1 206878618#2 206878571 206878624#0 195427225#1 25450515 171767377#0 171767377#1 195427224#0 96336873 96336870"/>
</vehicle>
There are multiple vType elements (ex. Bus, taxi, passenger car etc) and for each vType, there are multiple instantantiations of vehicle (numbered 1.0, 1.1 etc.) that has the route edges as attributes.
I want to now append the file such that I now have a subelement stop under vehicle that specifies the stops as follows
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://sumo.dlr.de/xsd/routes_file.xsd">
<vType id="Bus" vClass="ignoring" guiShape="bus" color="cyan"/>
<vehicle id="1.0" type="Bus" depart="0.00">
<route edges="207358226 206878618#0 206878618#1 206878618#2 206878571 206878624#0 195427225#1 25450515 171767377#0 171767377#1 195427224#0 96336873 96336870"/>
<stop lane="207358226" endPos="10" duration="20"/>
<stop lane="206878618#0" endPos="10" duration="20"/>
<stop lane="206878618#1" endPos="10" duration="20"/>
..........
..........
</vehicle>
<vehicle id="2.0" type="Taxi" depart="0.00">
<route edges="172428613 -25301974#1 -25301974#0 172428582 -172428593 -25301969#5 -25301969#4 -165310768#1 -165310768#0 -45073854#4 -45073854#3 -45073854#0 -32932418#2 172436826#1 172436826#2 172436826#3 172436826#4 172436826#5 172405270#0 24629564 172405301#1 -172405301#1 -24629564 -172405270#0"/>
</vehicle>
<vehicle id="1.1" type="Bus" depart="0.00">
<route edges="207358226 206878618#0 206878618#1 206878618#2 206878571 206878624#0 195427225#1 25450515 171767377#0 171767377#1 195427224#0 96336873 96336870"/>
<stop lane="207358226" endPos="10" duration="20"/>
<stop lane="206878618#0" endPos="10" duration="20"/>
<stop lane="206878618#1" endPos="10" duration="20"/>
..........
..........
</vehicle>
My initial approach is to iteratively parse the xml and pick up the elements with tag vehicle and attribute Bus. I then copy the edges into a list edgesnew. I then create a subelement iteratively inside a loop under vehicle named stop. The code is as follows
parser = etree.XMLParser(encoding='utf-8', recover=True)
routesFileTree = etree.parse('kaiserslautern.rou1.xml', parser)
routesFileRoot = routesFileTree.getroot()
vehicle = routesFileRoot.find('vehicle')
route = etree.SubElement(vehicle, 'route')
for elem in routesFileRoot.iter(tag = 'vehicle'):
if elem.attrib['type'] == 'Bus':
for subelem in elem.iter(tag = 'route'):
if subelem.attrib.get('edges'):
edgesnew = subelem.attrib['edges'].split(' ')
for edges in range(0,len(edgesnew),3):
stop = etree.SubElement(vehicle,'stop', lane = stops[edgesnew[edges]], duration = "30")
The program executes but my algorithm is wrong as it returns me the following output when I try to print
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://sumo.dlr.de/xsd/routes_file.xsd">
<vType id="Bus" vClass="ignoring" guiShape="bus" color="cyan"/>
<vehicle id="1.0" type="Bus" depart="0.00">
<route edges="207358226 206878618#0 206878618#1 206878618#2 206878571 206878624#0 195427225#1 25450515 171767377#0 171767377#1 195427224#0 96336873 96336870"/>
<route><stop lane="207358226" duration="30" endPos="10"/><stop lane="206878618#0" duration="30" endPos="10"/><.........../></route></vehicle>
<vehicle id="2.0" type="Taxi" depart="0.00">
<route edges="172428613 -25301974#1 -25301974#0 172428582 -172428593 -25301969#5 -25301969#4 -165310768#1 -165310768#0 -45073854#4 -45073854#3 -45073854#0 -32932418#2 172436826#1 172436826#2 172436826#3 172436826#4 172436826#5 172405270#0 24629564 172405301#1 -172405301#1 -24629564 -172405270#0"/>
</vehicle>
<vehicle id="1.1" type="Bus" depart="0.00">
<route edges="207358226 206878618#0 206878618#1 206878618#2 206878571 206878624#0 195427225#1 25450515 171767377#0 171767377#1 195427224#0 96336873 96336870"/>
</vehicle>
There are multiple problems in the code..First it only creates a new subelement for one instantiation of the vehicle only. and secondly it creates new route element rathat than appending to the existing xml. I have seen that I need to use element.append but cant figure out where and how.
Thanks in advance for the help
I am trying to parse this xml file with python element tree:
<?xml version="1.0" encoding="Windows-1250"?>
<rsp:responsePack version="2.0" id="001" state="ok" note="" programVersion="9801.8 (19.5.2011)" xmlns:rsp="http://www.stormware.cz/schema/version_2/response.xsd" xmlns:rdc="http://www.stormware.cz/schema/version_2/documentresponse.xsd" xmlns:typ="http://www.stormware.cz/schema/version_2/type.xsd" xmlns:lst="http://www.stormware.cz/schema/version_2/list.xsd" xmlns:lStk="http://www.stormware.cz/schema/version_2/list_stock.xsd" xmlns:lAdb="http://www.stormware.cz/schema/version_2/list_addBook.xsd" xmlns:acu="http://www.stormware.cz/schema/version_2/accountingunit.xsd" xmlns:inv="http://www.stormware.cz/schema/version_2/invoice.xsd" xmlns:vch="http://www.stormware.cz/schema/version_2/voucher.xsd" xmlns:int="http://www.stormware.cz/schema/version_2/intDoc.xsd" xmlns:stk="http://www.stormware.cz/schema/version_2/stock.xsd" xmlns:ord="http://www.stormware.cz/schema/version_2/order.xsd" xmlns:ofr="http://www.stormware.cz/schema/version_2/offer.xsd" xmlns:enq="http://www.stormware.cz/schema/version_2/enquiry.xsd" xmlns:vyd="http://www.stormware.cz/schema/version_2/vydejka.xsd" xmlns:pri="http://www.stormware.cz/schema/version_2/prijemka.xsd" xmlns:bal="http://www.stormware.cz/schema/version_2/balance.xsd" xmlns:pre="http://www.stormware.cz/schema/version_2/prevodka.xsd" xmlns:vyr="http://www.stormware.cz/schema/version_2/vyroba.xsd" xmlns:pro="http://www.stormware.cz/schema/version_2/prodejka.xsd" xmlns:con="http://www.stormware.cz/schema/version_2/contract.xsd" xmlns:adb="http://www.stormware.cz/schema/version_2/addressbook.xsd" xmlns:prm="http://www.stormware.cz/schema/version_2/parameter.xsd" xmlns:lCon="http://www.stormware.cz/schema/version_2/list_contract.xsd" xmlns:ctg="http://www.stormware.cz/schema/version_2/category.xsd" xmlns:ipm="http://www.stormware.cz/schema/version_2/intParam.xsd">
<rsp:responsePackItem version="2.0" id="li1" state="ok">
<lst:listInvoice version="2.0" dateTimeStamp="2011-05-27T10:47:23Z" dateValidFrom="2011-05-27" state="ok">
<lst:invoice version="2.0">
<inv:invoiceHeader>
<inv:id>20</inv:id>
<inv:invoiceType>issuedInvoice</inv:invoiceType>
<inv:number>
<typ:id>26</typ:id>
<typ:ids>1101</typ:ids>
<typ:numberRequested>110100001</typ:numberRequested>
</inv:number>
<inv:symVar>110100001</inv:symVar>
<inv:date>2011-01-30</inv:date>
<inv:dateTax>2011-01-30</inv:dateTax>
<inv:dateAccounting>2011-01-30</inv:dateAccounting>
<inv:dateDue>2011-02-13</inv:dateDue>
<inv:accounting>
<typ:id>17</typ:id>
<typ:ids>3Fv</typ:ids>
</inv:accounting>
<inv:classificationVAT>
<typ:id>251</typ:id>
<typ:ids>UD</typ:ids>
<typ:classificationVATType/>
</inv:classificationVAT>
<inv:text>Fakturujeme Vám zboží dle Vaší objednávky: </inv:text>
<inv:partnerIdentity>
<typ:id>15</typ:id>
<typ:address>
<typ:company>INTEAK spol. s r. o.</typ:company>
<typ:division>prodejna</typ:division>
<typ:name>David Jánský</typ:name>
<typ:city>Benešovice</typ:city>
<typ:street>Jiřího z Poděbrad 35</typ:street>
<typ:zip>463 48</typ:zip>
<typ:ico>85236972</typ:ico>
<typ:dic>CZ85236972</typ:dic>
</typ:address>
<typ:shipToAddress>
<typ:company/>
<typ:division/>
<typ:name/>
<typ:city/>
<typ:street/>
</typ:shipToAddress>
</inv:partnerIdentity>
<inv:myIdentity>
<typ:address>
<typ:company>Novák </typ:company>
<typ:surname>Novák</typ:surname>
<typ:name>Jan</typ:name>
<typ:city>Jihlava 1</typ:city>
<typ:street>Horní</typ:street>
<typ:number>15</typ:number>
<typ:zip>586 01</typ:zip>
<typ:ico>12345678</typ:ico>
<typ:dic>CZ12345678</typ:dic>
<typ:phone>569 876 542</typ:phone>
<typ:mobilPhone>602 852 369</typ:mobilPhone>
<typ:fax>564 563 216</typ:fax>
<typ:email>info#novak.cz</typ:email>
<typ:www>www.novak.cz</typ:www>
</typ:address>
</inv:myIdentity>
<inv:dateOrder>2011-01-22</inv:dateOrder>
<inv:paymentType>
<typ:id>1</typ:id>
<typ:ids>příkazem</typ:ids>
<typ:paymentType>draft</typ:paymentType>
</inv:paymentType>
<inv:account>
<typ:id>2</typ:id>
<typ:ids>KB</typ:ids>
</inv:account>
<inv:symConst>0308</inv:symConst>
<inv:centre>
<typ:id>1</typ:id>
<typ:ids>BRNO</typ:ids>
</inv:centre>
<inv:activity>
<typ:id>2</typ:id>
<typ:ids>NÁBYTEK</typ:ids>
</inv:activity>
<inv:liquidation>
<typ:date>2011-02-12</typ:date>
<typ:amountHome>356</typ:amountHome>
</inv:liquidation>
</inv:invoiceHeader>
<inv:invoiceDetail>
<inv:invoiceItem>
<inv:id>19</inv:id>
<inv:text>Židle Z220</inv:text>
<inv:quantity>2.0</inv:quantity>
<inv:unit>ks</inv:unit>
<inv:coefficient>1.0</inv:coefficient>
<inv:rateVAT>high</inv:rateVAT>
<inv:discountPercentage>0.0</inv:discountPercentage>
<inv:homeCurrency>
<typ:unitPrice>1968</typ:unitPrice>
<typ:price>3936</typ:price>
<typ:priceVAT>787.2</typ:priceVAT>
<typ:priceSum>4723.2</typ:priceSum>
</inv:homeCurrency>
<inv:code>Z220</inv:code>
<inv:guarantee>0</inv:guarantee>
<inv:guaranteeType>none</inv:guaranteeType>
<inv:stockItem>
<typ:store>
<typ:id>1</typ:id>
<typ:ids>ZBOŽÍ</typ:ids>
</typ:store>
<typ:stockItem>
<typ:id>27</typ:id>
<typ:ids>Z220</typ:ids>
<typ:PLU>650</typ:PLU>
</typ:stockItem>
</inv:stockItem>
</inv:invoiceItem>
<inv:invoiceItem>
<inv:id>20</inv:id>
<inv:text>Konferenční stolek chrom</inv:text>
<inv:quantity>1.0</inv:quantity>
<inv:unit>ks</inv:unit>
<inv:coefficient>1.0</inv:coefficient>
<inv:rateVAT>high</inv:rateVAT>
<inv:discountPercentage>0.0</inv:discountPercentage>
<inv:homeCurrency>
<typ:unitPrice>7680</typ:unitPrice>
<typ:price>7680</typ:price>
<typ:priceVAT>1536</typ:priceVAT>
<typ:priceSum>9216</typ:priceSum>
</inv:homeCurrency>
<inv:note>Rozměr: 120 x 60</inv:note>
<inv:code>Konf11</inv:code>
<inv:guarantee>0</inv:guarantee>
<inv:guaranteeType>none</inv:guaranteeType>
<inv:stockItem>
<typ:store>
<typ:id>1</typ:id>
<typ:ids>ZBOŽÍ</typ:ids>
</typ:store>
<typ:stockItem>
<typ:id>10</typ:id>
<typ:ids>Konf11</typ:ids>
<typ:PLU>625</typ:PLU>
</typ:stockItem>
</inv:stockItem>
</inv:invoiceItem>
<inv:invoiceItem>
<inv:id>21</inv:id>
<inv:text>Křeslo čalouněné 1320</inv:text>
<inv:quantity>4.0</inv:quantity>
<inv:unit>ks</inv:unit>
<inv:coefficient>1.0</inv:coefficient>
<inv:rateVAT>high</inv:rateVAT>
<inv:discountPercentage>0.0</inv:discountPercentage>
<inv:homeCurrency>
<typ:unitPrice>5988</typ:unitPrice>
<typ:price>23952</typ:price>
<typ:priceVAT>4790.4</typ:priceVAT>
<typ:priceSum>28742.4</typ:priceSum>
</inv:homeCurrency>
<inv:code>Kř1320</inv:code>
<inv:guarantee>0</inv:guarantee>
<inv:guaranteeType>none</inv:guaranteeType>
<inv:stockItem>
<typ:store>
<typ:id>1</typ:id>
<typ:ids>ZBOŽÍ</typ:ids>
</typ:store>
<typ:stockItem>
<typ:id>13</typ:id>
<typ:ids>Kř1320</typ:ids>
<typ:PLU>627</typ:PLU>
</typ:stockItem>
</inv:stockItem>
</inv:invoiceItem>
</inv:invoiceDetail>
<inv:invoiceSummary>
<inv:roundingDocument>up2one</inv:roundingDocument>
<inv:roundingVAT>none</inv:roundingVAT>
<inv:homeCurrency>
<typ:priceNone>0</typ:priceNone>
<typ:priceLow>0</typ:priceLow>
<typ:priceLowVAT>0</typ:priceLowVAT>
<typ:priceLowSum>0</typ:priceLowSum>
<typ:priceHigh>35568</typ:priceHigh>
<typ:priceHighVAT>7113.6</typ:priceHighVAT>
<typ:priceHighSum>42681.6</typ:priceHighSum>
<typ:round>
<typ:priceRound>0.4</typ:priceRound>
</typ:round>
</inv:homeCurrency>
</inv:invoiceSummary>
</lst:invoice>
</lst:listInvoice>
</rsp:responsePackItem>
</rsp:responsePack>
Please, how can I get data such as: (?)
inv:invoiceSummary - typ:priceHighSum
inv:partnerIdentity - typ:name, typ:ico
inv:myIdentity - typ:company
inv:liquidation - typ:date
I tried this but can't get it working:
import xml.etree.ElementTree as ET
tree = ET.parse('temp_xml2.xml')
root = tree.getroot()
for listInvoice in root.findall('listInvoice'):
invoiceHeader = listInvoice.find('invoiceHeader').text
print invoiceHeader
Try yo use jsoup. An related example is parse XML.
this works:
for listInvoice in root.findall('.//{http://www.stormware.cz/schema/version_2/invoice.xsd}invoiceHeader'):
invoiceHeader = listInvoice.find('.//{http://www.stormware.cz/schema/version_2/invoice.xsd}id').text
print invoiceHeader