How to add multiple tags in xml?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















First time handling XML files in python.
I would like to know how to add multiple child elements from a dictionary, for example, I have a child element called country, but I would like to create 40 elements with country tag, same with cities:
I have a tupple like this:



import xml.etree.cElementTree as ET

dict={'Mexico':'CDMX','US':'Washington','France':'Paris','Japan':'Tokio'}


How can I create multiple tags with same reference and how can I pass the value from the dictionary?(I can create the xml file but just with one tag.)



I'm focusing on countries but I can't achieve to generate them by key this:



root = ET.Element("world")

country_tag = ET.SubElement(root, "country")
city = ET.SubElement(country_tag, "city")

for key,value in dict.items():
ET.Element.append(country)
country.text = str(key)
tree = ET.ElementTree(root)
tree.write("filename.xml", encoding="UTF-8",xml_declaration=True)


I would like to generate the following:



<world>
<country>Mexico</country>
<city>CDMX</city>
<country>US</country>
<city>Washington</city>
<country>France</country>
<city>Paris</Paris>
<country>Japan</country>
<city>Tokio</city>
</world>


I'm using python 3.7
I also need to know how to pass keys and values from my dictionary to xml tags. PLEASE, I NEED HELP!!!










share|improve this question

























  • So what is ET?

    – Red Cricket
    Nov 23 '18 at 18:58











  • ET stands for ElementTree, using xml ElementTree

    – Javier Ramirez
    Nov 23 '18 at 19:00


















0















First time handling XML files in python.
I would like to know how to add multiple child elements from a dictionary, for example, I have a child element called country, but I would like to create 40 elements with country tag, same with cities:
I have a tupple like this:



import xml.etree.cElementTree as ET

dict={'Mexico':'CDMX','US':'Washington','France':'Paris','Japan':'Tokio'}


How can I create multiple tags with same reference and how can I pass the value from the dictionary?(I can create the xml file but just with one tag.)



I'm focusing on countries but I can't achieve to generate them by key this:



root = ET.Element("world")

country_tag = ET.SubElement(root, "country")
city = ET.SubElement(country_tag, "city")

for key,value in dict.items():
ET.Element.append(country)
country.text = str(key)
tree = ET.ElementTree(root)
tree.write("filename.xml", encoding="UTF-8",xml_declaration=True)


I would like to generate the following:



<world>
<country>Mexico</country>
<city>CDMX</city>
<country>US</country>
<city>Washington</city>
<country>France</country>
<city>Paris</Paris>
<country>Japan</country>
<city>Tokio</city>
</world>


I'm using python 3.7
I also need to know how to pass keys and values from my dictionary to xml tags. PLEASE, I NEED HELP!!!










share|improve this question

























  • So what is ET?

    – Red Cricket
    Nov 23 '18 at 18:58











  • ET stands for ElementTree, using xml ElementTree

    – Javier Ramirez
    Nov 23 '18 at 19:00














0












0








0








First time handling XML files in python.
I would like to know how to add multiple child elements from a dictionary, for example, I have a child element called country, but I would like to create 40 elements with country tag, same with cities:
I have a tupple like this:



import xml.etree.cElementTree as ET

dict={'Mexico':'CDMX','US':'Washington','France':'Paris','Japan':'Tokio'}


How can I create multiple tags with same reference and how can I pass the value from the dictionary?(I can create the xml file but just with one tag.)



I'm focusing on countries but I can't achieve to generate them by key this:



root = ET.Element("world")

country_tag = ET.SubElement(root, "country")
city = ET.SubElement(country_tag, "city")

for key,value in dict.items():
ET.Element.append(country)
country.text = str(key)
tree = ET.ElementTree(root)
tree.write("filename.xml", encoding="UTF-8",xml_declaration=True)


I would like to generate the following:



<world>
<country>Mexico</country>
<city>CDMX</city>
<country>US</country>
<city>Washington</city>
<country>France</country>
<city>Paris</Paris>
<country>Japan</country>
<city>Tokio</city>
</world>


I'm using python 3.7
I also need to know how to pass keys and values from my dictionary to xml tags. PLEASE, I NEED HELP!!!










share|improve this question
















First time handling XML files in python.
I would like to know how to add multiple child elements from a dictionary, for example, I have a child element called country, but I would like to create 40 elements with country tag, same with cities:
I have a tupple like this:



import xml.etree.cElementTree as ET

dict={'Mexico':'CDMX','US':'Washington','France':'Paris','Japan':'Tokio'}


How can I create multiple tags with same reference and how can I pass the value from the dictionary?(I can create the xml file but just with one tag.)



I'm focusing on countries but I can't achieve to generate them by key this:



root = ET.Element("world")

country_tag = ET.SubElement(root, "country")
city = ET.SubElement(country_tag, "city")

for key,value in dict.items():
ET.Element.append(country)
country.text = str(key)
tree = ET.ElementTree(root)
tree.write("filename.xml", encoding="UTF-8",xml_declaration=True)


I would like to generate the following:



<world>
<country>Mexico</country>
<city>CDMX</city>
<country>US</country>
<city>Washington</city>
<country>France</country>
<city>Paris</Paris>
<country>Japan</country>
<city>Tokio</city>
</world>


I'm using python 3.7
I also need to know how to pass keys and values from my dictionary to xml tags. PLEASE, I NEED HELP!!!







python xml python-3.x dictionary tuples






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 19:04









petezurich

3,89081936




3,89081936










asked Nov 23 '18 at 18:53









Javier RamirezJavier Ramirez

406




406













  • So what is ET?

    – Red Cricket
    Nov 23 '18 at 18:58











  • ET stands for ElementTree, using xml ElementTree

    – Javier Ramirez
    Nov 23 '18 at 19:00



















  • So what is ET?

    – Red Cricket
    Nov 23 '18 at 18:58











  • ET stands for ElementTree, using xml ElementTree

    – Javier Ramirez
    Nov 23 '18 at 19:00

















So what is ET?

– Red Cricket
Nov 23 '18 at 18:58





So what is ET?

– Red Cricket
Nov 23 '18 at 18:58













ET stands for ElementTree, using xml ElementTree

– Javier Ramirez
Nov 23 '18 at 19:00





ET stands for ElementTree, using xml ElementTree

– Javier Ramirez
Nov 23 '18 at 19:00












0






active

oldest

votes












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',
autoActivateHeartbeat: false,
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%2f53451714%2fhow-to-add-multiple-tags-in-xml%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53451714%2fhow-to-add-multiple-tags-in-xml%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