ElementTree parse xml file - problem with parsing











up vote
0
down vote

favorite












I have a problem parsing data from xml file. I'm using xml.etree.ElementTree to extract data from files and then save them into .csv. I have all the necessery modules installed on server.
I am aware that there is bs4 module with BeutifulSoup, yet I would like to know if is possible to parse this data/xml file using ElementTree. Sorry if the answear is easy or obvious, yet I'm still very much a beginner and with this problem I could not name the problem in a way to find an answear.



While running python script written below I have no errors and no outcome. I don't really know what should I change. I can not find solution. I tried using different child.tag or attributes but with no result.



The xml file that I have problem with.:



<?xml version="1.0" encoding="utf-8"?>
<offer file_format="IOF" version="2.6" extensions="yes" xmlns="http://www.iai-shop.com/developers/iof.phtml">
<product id="9" vat="23.0" code_on_card="BHA">
<producer id="1308137276" name="BEAL"/>
...
<price gross="175" net="142.28"/>
<sizes>
<size code_producer="3700288265272" code="9-uniw" weight="0">
<stock id="0" quantity="-1"/>
<stock id="1" quantity="4"/>
</size>
</sizes>
</product>
<product>
...
</product>
...


and script that I tried to use (here to extract code_on_card, price net, quantity).



(I am aware that there are two childs: stock and quantity, and I'm completely fine with the second one overwrting the first one)



import requests
import os,sys
import csv
import xml.etree.ElementTree as ET

reload(sys)
sys.setdefaultencoding('utf-8')

xml_path = '/file.xml'

xml = ET.parse(xml_path)

with open('/home/file.csv', 'wb') as f:
c = csv.writer(f, delimiter=';')
for product in xml.iter('product'):
product_id = product.attrib["code_on_card"]
for child in product:
if child.tag == 'price':
if child.attrib["net"] != None:
hurt_net = child.attrib["net"]
for size in product.iter('size'):
for stock in size.iter('stock'):
if 'quantity' in stock.attrib.keys():
quantity = stock.attrib["quantity"]

line = product_id, hurt_net, quantity
c.writerow(line)


Files that seem to me to be built on similar scheme work just fine (offer -> product ->child/attrib ), like this one:



<?xml version="1.0" encoding="UTF-8"?>
<offer file_format="IOF" version="2.5">
<product id="2">
<price gross="0.00" net="0.00" vat="23.0"/>
<srp gross="0.00" net="0" vat="23.0"/>
<sizes>
<size id="0" code="2-0" weight="0" >
</size>
</sizes>
</product>
...
</product>
...


EDIT:
Outcome should be .csv file containing multpile rows (each for each product in xml file) of code_on_card, price net, quantity. It should look like:



BC097B.50GD.O;70.81;37
BC097B.50.A;76.75;24
BC086C.50.B;76.75;29
BGRT.L;3;96.75;28
....


EDIT2
code as it is, after drec4s answear:



import requests
import os,sys
import csv
import xml.etree.cElementTree as ET

reload(sys)
sys.setdefaultencoding('utf-8')

xml_path = '/home/platne/serwer16373/dane/z_hurtowni/pobrane/beal2.xml'

root = ET.parse(xml_path)

ns = {'offer': 'http://www.iai-shop.com/developers/iof.phtml'}

products = root.getchildren()

with open('/home/platne/serwer16373/dane/z_hurtowni/stany_magazynowe/karol/bealKa.csv', 'wb') as f:
c = csv.writer(f, delimiter=';')
hurtownia = 'beal'
for product in root.iter('product'):
qtt = [1]
code = product.get('code_on_card')
hurt_net = product.find('price').get('net')
for stock in product.find('sizes').find('size').getchildren():
qtt.append(stock.get('quantity'))
quantity = max(qtt)


line = 'beal-'+str(code), hurt_net, quantity
c.writerow(line)


somehow I'm getting
AttributeError: 'ElementTree' object has no attribute 'getchildren'
I've got Ele










share|improve this question




















  • 1




    It's because of the default namespace (see here on how to parse XML with namespaces). It would also be helpful if you added an example of what your output should look like.
    – Daniel Haley
    Nov 7 at 23:22










  • Another thing that would be helpful is code that will run as-is (see stackoverflow.com/help/mcve).
    – Daniel Haley
    Nov 7 at 23:25










  • default namespace - I think thats it - need time to bite into it.
    – Rafae Rafad
    Nov 8 at 0:23















up vote
0
down vote

favorite












I have a problem parsing data from xml file. I'm using xml.etree.ElementTree to extract data from files and then save them into .csv. I have all the necessery modules installed on server.
I am aware that there is bs4 module with BeutifulSoup, yet I would like to know if is possible to parse this data/xml file using ElementTree. Sorry if the answear is easy or obvious, yet I'm still very much a beginner and with this problem I could not name the problem in a way to find an answear.



While running python script written below I have no errors and no outcome. I don't really know what should I change. I can not find solution. I tried using different child.tag or attributes but with no result.



The xml file that I have problem with.:



<?xml version="1.0" encoding="utf-8"?>
<offer file_format="IOF" version="2.6" extensions="yes" xmlns="http://www.iai-shop.com/developers/iof.phtml">
<product id="9" vat="23.0" code_on_card="BHA">
<producer id="1308137276" name="BEAL"/>
...
<price gross="175" net="142.28"/>
<sizes>
<size code_producer="3700288265272" code="9-uniw" weight="0">
<stock id="0" quantity="-1"/>
<stock id="1" quantity="4"/>
</size>
</sizes>
</product>
<product>
...
</product>
...


and script that I tried to use (here to extract code_on_card, price net, quantity).



(I am aware that there are two childs: stock and quantity, and I'm completely fine with the second one overwrting the first one)



import requests
import os,sys
import csv
import xml.etree.ElementTree as ET

reload(sys)
sys.setdefaultencoding('utf-8')

xml_path = '/file.xml'

xml = ET.parse(xml_path)

with open('/home/file.csv', 'wb') as f:
c = csv.writer(f, delimiter=';')
for product in xml.iter('product'):
product_id = product.attrib["code_on_card"]
for child in product:
if child.tag == 'price':
if child.attrib["net"] != None:
hurt_net = child.attrib["net"]
for size in product.iter('size'):
for stock in size.iter('stock'):
if 'quantity' in stock.attrib.keys():
quantity = stock.attrib["quantity"]

line = product_id, hurt_net, quantity
c.writerow(line)


Files that seem to me to be built on similar scheme work just fine (offer -> product ->child/attrib ), like this one:



<?xml version="1.0" encoding="UTF-8"?>
<offer file_format="IOF" version="2.5">
<product id="2">
<price gross="0.00" net="0.00" vat="23.0"/>
<srp gross="0.00" net="0" vat="23.0"/>
<sizes>
<size id="0" code="2-0" weight="0" >
</size>
</sizes>
</product>
...
</product>
...


EDIT:
Outcome should be .csv file containing multpile rows (each for each product in xml file) of code_on_card, price net, quantity. It should look like:



BC097B.50GD.O;70.81;37
BC097B.50.A;76.75;24
BC086C.50.B;76.75;29
BGRT.L;3;96.75;28
....


EDIT2
code as it is, after drec4s answear:



import requests
import os,sys
import csv
import xml.etree.cElementTree as ET

reload(sys)
sys.setdefaultencoding('utf-8')

xml_path = '/home/platne/serwer16373/dane/z_hurtowni/pobrane/beal2.xml'

root = ET.parse(xml_path)

ns = {'offer': 'http://www.iai-shop.com/developers/iof.phtml'}

products = root.getchildren()

with open('/home/platne/serwer16373/dane/z_hurtowni/stany_magazynowe/karol/bealKa.csv', 'wb') as f:
c = csv.writer(f, delimiter=';')
hurtownia = 'beal'
for product in root.iter('product'):
qtt = [1]
code = product.get('code_on_card')
hurt_net = product.find('price').get('net')
for stock in product.find('sizes').find('size').getchildren():
qtt.append(stock.get('quantity'))
quantity = max(qtt)


line = 'beal-'+str(code), hurt_net, quantity
c.writerow(line)


somehow I'm getting
AttributeError: 'ElementTree' object has no attribute 'getchildren'
I've got Ele










share|improve this question




















  • 1




    It's because of the default namespace (see here on how to parse XML with namespaces). It would also be helpful if you added an example of what your output should look like.
    – Daniel Haley
    Nov 7 at 23:22










  • Another thing that would be helpful is code that will run as-is (see stackoverflow.com/help/mcve).
    – Daniel Haley
    Nov 7 at 23:25










  • default namespace - I think thats it - need time to bite into it.
    – Rafae Rafad
    Nov 8 at 0:23













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a problem parsing data from xml file. I'm using xml.etree.ElementTree to extract data from files and then save them into .csv. I have all the necessery modules installed on server.
I am aware that there is bs4 module with BeutifulSoup, yet I would like to know if is possible to parse this data/xml file using ElementTree. Sorry if the answear is easy or obvious, yet I'm still very much a beginner and with this problem I could not name the problem in a way to find an answear.



While running python script written below I have no errors and no outcome. I don't really know what should I change. I can not find solution. I tried using different child.tag or attributes but with no result.



The xml file that I have problem with.:



<?xml version="1.0" encoding="utf-8"?>
<offer file_format="IOF" version="2.6" extensions="yes" xmlns="http://www.iai-shop.com/developers/iof.phtml">
<product id="9" vat="23.0" code_on_card="BHA">
<producer id="1308137276" name="BEAL"/>
...
<price gross="175" net="142.28"/>
<sizes>
<size code_producer="3700288265272" code="9-uniw" weight="0">
<stock id="0" quantity="-1"/>
<stock id="1" quantity="4"/>
</size>
</sizes>
</product>
<product>
...
</product>
...


and script that I tried to use (here to extract code_on_card, price net, quantity).



(I am aware that there are two childs: stock and quantity, and I'm completely fine with the second one overwrting the first one)



import requests
import os,sys
import csv
import xml.etree.ElementTree as ET

reload(sys)
sys.setdefaultencoding('utf-8')

xml_path = '/file.xml'

xml = ET.parse(xml_path)

with open('/home/file.csv', 'wb') as f:
c = csv.writer(f, delimiter=';')
for product in xml.iter('product'):
product_id = product.attrib["code_on_card"]
for child in product:
if child.tag == 'price':
if child.attrib["net"] != None:
hurt_net = child.attrib["net"]
for size in product.iter('size'):
for stock in size.iter('stock'):
if 'quantity' in stock.attrib.keys():
quantity = stock.attrib["quantity"]

line = product_id, hurt_net, quantity
c.writerow(line)


Files that seem to me to be built on similar scheme work just fine (offer -> product ->child/attrib ), like this one:



<?xml version="1.0" encoding="UTF-8"?>
<offer file_format="IOF" version="2.5">
<product id="2">
<price gross="0.00" net="0.00" vat="23.0"/>
<srp gross="0.00" net="0" vat="23.0"/>
<sizes>
<size id="0" code="2-0" weight="0" >
</size>
</sizes>
</product>
...
</product>
...


EDIT:
Outcome should be .csv file containing multpile rows (each for each product in xml file) of code_on_card, price net, quantity. It should look like:



BC097B.50GD.O;70.81;37
BC097B.50.A;76.75;24
BC086C.50.B;76.75;29
BGRT.L;3;96.75;28
....


EDIT2
code as it is, after drec4s answear:



import requests
import os,sys
import csv
import xml.etree.cElementTree as ET

reload(sys)
sys.setdefaultencoding('utf-8')

xml_path = '/home/platne/serwer16373/dane/z_hurtowni/pobrane/beal2.xml'

root = ET.parse(xml_path)

ns = {'offer': 'http://www.iai-shop.com/developers/iof.phtml'}

products = root.getchildren()

with open('/home/platne/serwer16373/dane/z_hurtowni/stany_magazynowe/karol/bealKa.csv', 'wb') as f:
c = csv.writer(f, delimiter=';')
hurtownia = 'beal'
for product in root.iter('product'):
qtt = [1]
code = product.get('code_on_card')
hurt_net = product.find('price').get('net')
for stock in product.find('sizes').find('size').getchildren():
qtt.append(stock.get('quantity'))
quantity = max(qtt)


line = 'beal-'+str(code), hurt_net, quantity
c.writerow(line)


somehow I'm getting
AttributeError: 'ElementTree' object has no attribute 'getchildren'
I've got Ele










share|improve this question















I have a problem parsing data from xml file. I'm using xml.etree.ElementTree to extract data from files and then save them into .csv. I have all the necessery modules installed on server.
I am aware that there is bs4 module with BeutifulSoup, yet I would like to know if is possible to parse this data/xml file using ElementTree. Sorry if the answear is easy or obvious, yet I'm still very much a beginner and with this problem I could not name the problem in a way to find an answear.



While running python script written below I have no errors and no outcome. I don't really know what should I change. I can not find solution. I tried using different child.tag or attributes but with no result.



The xml file that I have problem with.:



<?xml version="1.0" encoding="utf-8"?>
<offer file_format="IOF" version="2.6" extensions="yes" xmlns="http://www.iai-shop.com/developers/iof.phtml">
<product id="9" vat="23.0" code_on_card="BHA">
<producer id="1308137276" name="BEAL"/>
...
<price gross="175" net="142.28"/>
<sizes>
<size code_producer="3700288265272" code="9-uniw" weight="0">
<stock id="0" quantity="-1"/>
<stock id="1" quantity="4"/>
</size>
</sizes>
</product>
<product>
...
</product>
...


and script that I tried to use (here to extract code_on_card, price net, quantity).



(I am aware that there are two childs: stock and quantity, and I'm completely fine with the second one overwrting the first one)



import requests
import os,sys
import csv
import xml.etree.ElementTree as ET

reload(sys)
sys.setdefaultencoding('utf-8')

xml_path = '/file.xml'

xml = ET.parse(xml_path)

with open('/home/file.csv', 'wb') as f:
c = csv.writer(f, delimiter=';')
for product in xml.iter('product'):
product_id = product.attrib["code_on_card"]
for child in product:
if child.tag == 'price':
if child.attrib["net"] != None:
hurt_net = child.attrib["net"]
for size in product.iter('size'):
for stock in size.iter('stock'):
if 'quantity' in stock.attrib.keys():
quantity = stock.attrib["quantity"]

line = product_id, hurt_net, quantity
c.writerow(line)


Files that seem to me to be built on similar scheme work just fine (offer -> product ->child/attrib ), like this one:



<?xml version="1.0" encoding="UTF-8"?>
<offer file_format="IOF" version="2.5">
<product id="2">
<price gross="0.00" net="0.00" vat="23.0"/>
<srp gross="0.00" net="0" vat="23.0"/>
<sizes>
<size id="0" code="2-0" weight="0" >
</size>
</sizes>
</product>
...
</product>
...


EDIT:
Outcome should be .csv file containing multpile rows (each for each product in xml file) of code_on_card, price net, quantity. It should look like:



BC097B.50GD.O;70.81;37
BC097B.50.A;76.75;24
BC086C.50.B;76.75;29
BGRT.L;3;96.75;28
....


EDIT2
code as it is, after drec4s answear:



import requests
import os,sys
import csv
import xml.etree.cElementTree as ET

reload(sys)
sys.setdefaultencoding('utf-8')

xml_path = '/home/platne/serwer16373/dane/z_hurtowni/pobrane/beal2.xml'

root = ET.parse(xml_path)

ns = {'offer': 'http://www.iai-shop.com/developers/iof.phtml'}

products = root.getchildren()

with open('/home/platne/serwer16373/dane/z_hurtowni/stany_magazynowe/karol/bealKa.csv', 'wb') as f:
c = csv.writer(f, delimiter=';')
hurtownia = 'beal'
for product in root.iter('product'):
qtt = [1]
code = product.get('code_on_card')
hurt_net = product.find('price').get('net')
for stock in product.find('sizes').find('size').getchildren():
qtt.append(stock.get('quantity'))
quantity = max(qtt)


line = 'beal-'+str(code), hurt_net, quantity
c.writerow(line)


somehow I'm getting
AttributeError: 'ElementTree' object has no attribute 'getchildren'
I've got Ele







python parsing elementtree






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 2:14

























asked Nov 7 at 22:41









Rafae Rafad

2014




2014








  • 1




    It's because of the default namespace (see here on how to parse XML with namespaces). It would also be helpful if you added an example of what your output should look like.
    – Daniel Haley
    Nov 7 at 23:22










  • Another thing that would be helpful is code that will run as-is (see stackoverflow.com/help/mcve).
    – Daniel Haley
    Nov 7 at 23:25










  • default namespace - I think thats it - need time to bite into it.
    – Rafae Rafad
    Nov 8 at 0:23














  • 1




    It's because of the default namespace (see here on how to parse XML with namespaces). It would also be helpful if you added an example of what your output should look like.
    – Daniel Haley
    Nov 7 at 23:22










  • Another thing that would be helpful is code that will run as-is (see stackoverflow.com/help/mcve).
    – Daniel Haley
    Nov 7 at 23:25










  • default namespace - I think thats it - need time to bite into it.
    – Rafae Rafad
    Nov 8 at 0:23








1




1




It's because of the default namespace (see here on how to parse XML with namespaces). It would also be helpful if you added an example of what your output should look like.
– Daniel Haley
Nov 7 at 23:22




It's because of the default namespace (see here on how to parse XML with namespaces). It would also be helpful if you added an example of what your output should look like.
– Daniel Haley
Nov 7 at 23:22












Another thing that would be helpful is code that will run as-is (see stackoverflow.com/help/mcve).
– Daniel Haley
Nov 7 at 23:25




Another thing that would be helpful is code that will run as-is (see stackoverflow.com/help/mcve).
– Daniel Haley
Nov 7 at 23:25












default namespace - I think thats it - need time to bite into it.
– Rafae Rafad
Nov 8 at 0:23




default namespace - I think thats it - need time to bite into it.
– Rafae Rafad
Nov 8 at 0:23












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










This is how I would go and parse an xml file with namespaces. As per official documentation, the easiest way is to define a dictionary specifying the namespace.



from xml.etree import cElementTree as ET

root = ET.fromstring("""
<offer file_format="IOF" version="2.6" extensions="yes" xmlns="http://www.iai-shop.com/developers/iof.phtml">
<product id="9" vat="23.0" code_on_card="BHA">
<producer id="1308137276" name="BEAL"/>
<price gross="175" net="142.28"/>
<sizes>
<size code_producer="3700288265272" code="9-uniw" weight="0">
<stock id="0" quantity="-1"/>
<stock id="1" quantity="4"/>
</size>
</sizes>
</product>
</offer>
""")

ns = {'offer': 'http://www.iai-shop.com/developers/iof.phtml'}

products = root.getchildren()

for p in products:
qtt = #to store all stock quantities
product_id = p.get('code_on_card')
hurt_net = p.find('offer:price', ns).get('net')
for stock in p.find('offer:sizes', ns).find('offer:size', ns).getchildren():
qtt.append(int(stock.get('quantity')))

quantity = max(qtt) #or sum

line = (product_id, hurt_net, quantity)
print(line)


Outputs:



('BHA', '142.28', 4)


Also, I did not understand what was the stock quantity that you needed to extract, since you were only getting the last children(stock) value (change the sum function to max or to whatever you need).






share|improve this answer





















  • I think that this is the answear, yet I can't seem to work it out. Somehow i'm getting AttributeError: 'ElementTree' object has no attribute 'getchildren'
    – Rafae Rafad
    Nov 8 at 2:01













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53199014%2felementtree-parse-xml-file-problem-with-parsing%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










This is how I would go and parse an xml file with namespaces. As per official documentation, the easiest way is to define a dictionary specifying the namespace.



from xml.etree import cElementTree as ET

root = ET.fromstring("""
<offer file_format="IOF" version="2.6" extensions="yes" xmlns="http://www.iai-shop.com/developers/iof.phtml">
<product id="9" vat="23.0" code_on_card="BHA">
<producer id="1308137276" name="BEAL"/>
<price gross="175" net="142.28"/>
<sizes>
<size code_producer="3700288265272" code="9-uniw" weight="0">
<stock id="0" quantity="-1"/>
<stock id="1" quantity="4"/>
</size>
</sizes>
</product>
</offer>
""")

ns = {'offer': 'http://www.iai-shop.com/developers/iof.phtml'}

products = root.getchildren()

for p in products:
qtt = #to store all stock quantities
product_id = p.get('code_on_card')
hurt_net = p.find('offer:price', ns).get('net')
for stock in p.find('offer:sizes', ns).find('offer:size', ns).getchildren():
qtt.append(int(stock.get('quantity')))

quantity = max(qtt) #or sum

line = (product_id, hurt_net, quantity)
print(line)


Outputs:



('BHA', '142.28', 4)


Also, I did not understand what was the stock quantity that you needed to extract, since you were only getting the last children(stock) value (change the sum function to max or to whatever you need).






share|improve this answer





















  • I think that this is the answear, yet I can't seem to work it out. Somehow i'm getting AttributeError: 'ElementTree' object has no attribute 'getchildren'
    – Rafae Rafad
    Nov 8 at 2:01

















up vote
2
down vote



accepted










This is how I would go and parse an xml file with namespaces. As per official documentation, the easiest way is to define a dictionary specifying the namespace.



from xml.etree import cElementTree as ET

root = ET.fromstring("""
<offer file_format="IOF" version="2.6" extensions="yes" xmlns="http://www.iai-shop.com/developers/iof.phtml">
<product id="9" vat="23.0" code_on_card="BHA">
<producer id="1308137276" name="BEAL"/>
<price gross="175" net="142.28"/>
<sizes>
<size code_producer="3700288265272" code="9-uniw" weight="0">
<stock id="0" quantity="-1"/>
<stock id="1" quantity="4"/>
</size>
</sizes>
</product>
</offer>
""")

ns = {'offer': 'http://www.iai-shop.com/developers/iof.phtml'}

products = root.getchildren()

for p in products:
qtt = #to store all stock quantities
product_id = p.get('code_on_card')
hurt_net = p.find('offer:price', ns).get('net')
for stock in p.find('offer:sizes', ns).find('offer:size', ns).getchildren():
qtt.append(int(stock.get('quantity')))

quantity = max(qtt) #or sum

line = (product_id, hurt_net, quantity)
print(line)


Outputs:



('BHA', '142.28', 4)


Also, I did not understand what was the stock quantity that you needed to extract, since you were only getting the last children(stock) value (change the sum function to max or to whatever you need).






share|improve this answer





















  • I think that this is the answear, yet I can't seem to work it out. Somehow i'm getting AttributeError: 'ElementTree' object has no attribute 'getchildren'
    – Rafae Rafad
    Nov 8 at 2:01















up vote
2
down vote



accepted







up vote
2
down vote



accepted






This is how I would go and parse an xml file with namespaces. As per official documentation, the easiest way is to define a dictionary specifying the namespace.



from xml.etree import cElementTree as ET

root = ET.fromstring("""
<offer file_format="IOF" version="2.6" extensions="yes" xmlns="http://www.iai-shop.com/developers/iof.phtml">
<product id="9" vat="23.0" code_on_card="BHA">
<producer id="1308137276" name="BEAL"/>
<price gross="175" net="142.28"/>
<sizes>
<size code_producer="3700288265272" code="9-uniw" weight="0">
<stock id="0" quantity="-1"/>
<stock id="1" quantity="4"/>
</size>
</sizes>
</product>
</offer>
""")

ns = {'offer': 'http://www.iai-shop.com/developers/iof.phtml'}

products = root.getchildren()

for p in products:
qtt = #to store all stock quantities
product_id = p.get('code_on_card')
hurt_net = p.find('offer:price', ns).get('net')
for stock in p.find('offer:sizes', ns).find('offer:size', ns).getchildren():
qtt.append(int(stock.get('quantity')))

quantity = max(qtt) #or sum

line = (product_id, hurt_net, quantity)
print(line)


Outputs:



('BHA', '142.28', 4)


Also, I did not understand what was the stock quantity that you needed to extract, since you were only getting the last children(stock) value (change the sum function to max or to whatever you need).






share|improve this answer












This is how I would go and parse an xml file with namespaces. As per official documentation, the easiest way is to define a dictionary specifying the namespace.



from xml.etree import cElementTree as ET

root = ET.fromstring("""
<offer file_format="IOF" version="2.6" extensions="yes" xmlns="http://www.iai-shop.com/developers/iof.phtml">
<product id="9" vat="23.0" code_on_card="BHA">
<producer id="1308137276" name="BEAL"/>
<price gross="175" net="142.28"/>
<sizes>
<size code_producer="3700288265272" code="9-uniw" weight="0">
<stock id="0" quantity="-1"/>
<stock id="1" quantity="4"/>
</size>
</sizes>
</product>
</offer>
""")

ns = {'offer': 'http://www.iai-shop.com/developers/iof.phtml'}

products = root.getchildren()

for p in products:
qtt = #to store all stock quantities
product_id = p.get('code_on_card')
hurt_net = p.find('offer:price', ns).get('net')
for stock in p.find('offer:sizes', ns).find('offer:size', ns).getchildren():
qtt.append(int(stock.get('quantity')))

quantity = max(qtt) #or sum

line = (product_id, hurt_net, quantity)
print(line)


Outputs:



('BHA', '142.28', 4)


Also, I did not understand what was the stock quantity that you needed to extract, since you were only getting the last children(stock) value (change the sum function to max or to whatever you need).







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 8 at 0:12









drec4s

1,5372621




1,5372621












  • I think that this is the answear, yet I can't seem to work it out. Somehow i'm getting AttributeError: 'ElementTree' object has no attribute 'getchildren'
    – Rafae Rafad
    Nov 8 at 2:01




















  • I think that this is the answear, yet I can't seem to work it out. Somehow i'm getting AttributeError: 'ElementTree' object has no attribute 'getchildren'
    – Rafae Rafad
    Nov 8 at 2:01


















I think that this is the answear, yet I can't seem to work it out. Somehow i'm getting AttributeError: 'ElementTree' object has no attribute 'getchildren'
– Rafae Rafad
Nov 8 at 2:01






I think that this is the answear, yet I can't seem to work it out. Somehow i'm getting AttributeError: 'ElementTree' object has no attribute 'getchildren'
– Rafae Rafad
Nov 8 at 2:01




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53199014%2felementtree-parse-xml-file-problem-with-parsing%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini