solving XML problem by Python












-1















I have an xml file that contain a lot of information and tags.
For example I have this tag:



<SelectListMap SourceName="Document Type" SourceNumber="43" DestName="Document Type" DestNumber="43"/>


I have 40 other tags like this one with the same nodes, but the value of these nodes is different in each tag.
SourceName and DestName have the same value.



In some tags the DestName value is empty like this one:



<SelectListMap SourceName="Boolean Values" SourceNumber="73" DestName="" DestNumber="0" IsInternal="True"/>


So, I'm trying to give the empty DestName the value of Sourcename.



Here is my Python codes:



import re
import xml.etree.ElementTree as ET
tree = ET.parse("SPPID04A_BG3 - Copy - Copy.xml")
root = tree.getroot()
for SelectListMap in root.iter('SelectListMap'):
#DestName.text = str(DestName)
for node in tree.iter('SelectListMap'):
SourceName = node.attrib.get('SourceName')
SelectListMap.set('DestName', SourceName)
tree.write("SPPID04A_BG3 - Copy - Copy.xml")


This program is not working on the right way. any help or ideas?
Thanks!










share|improve this question













migrated from cs.stackexchange.com Jan 1 at 12:16


This question came from our site for students, researchers and practitioners of computer science.














  • 3





    Hi, could you please show the erroneous output or any other errors it is producing given an input?

    – Yuvraj Jaiswal
    Jan 1 at 12:20











  • When I run it I don't have error. But the DestName value in all tags change to a wrong value. My idea is give the empty DestName the same value of SourceName.

    – mashael
    Jan 1 at 13:25
















-1















I have an xml file that contain a lot of information and tags.
For example I have this tag:



<SelectListMap SourceName="Document Type" SourceNumber="43" DestName="Document Type" DestNumber="43"/>


I have 40 other tags like this one with the same nodes, but the value of these nodes is different in each tag.
SourceName and DestName have the same value.



In some tags the DestName value is empty like this one:



<SelectListMap SourceName="Boolean Values" SourceNumber="73" DestName="" DestNumber="0" IsInternal="True"/>


So, I'm trying to give the empty DestName the value of Sourcename.



Here is my Python codes:



import re
import xml.etree.ElementTree as ET
tree = ET.parse("SPPID04A_BG3 - Copy - Copy.xml")
root = tree.getroot()
for SelectListMap in root.iter('SelectListMap'):
#DestName.text = str(DestName)
for node in tree.iter('SelectListMap'):
SourceName = node.attrib.get('SourceName')
SelectListMap.set('DestName', SourceName)
tree.write("SPPID04A_BG3 - Copy - Copy.xml")


This program is not working on the right way. any help or ideas?
Thanks!










share|improve this question













migrated from cs.stackexchange.com Jan 1 at 12:16


This question came from our site for students, researchers and practitioners of computer science.














  • 3





    Hi, could you please show the erroneous output or any other errors it is producing given an input?

    – Yuvraj Jaiswal
    Jan 1 at 12:20











  • When I run it I don't have error. But the DestName value in all tags change to a wrong value. My idea is give the empty DestName the same value of SourceName.

    – mashael
    Jan 1 at 13:25














-1












-1








-1








I have an xml file that contain a lot of information and tags.
For example I have this tag:



<SelectListMap SourceName="Document Type" SourceNumber="43" DestName="Document Type" DestNumber="43"/>


I have 40 other tags like this one with the same nodes, but the value of these nodes is different in each tag.
SourceName and DestName have the same value.



In some tags the DestName value is empty like this one:



<SelectListMap SourceName="Boolean Values" SourceNumber="73" DestName="" DestNumber="0" IsInternal="True"/>


So, I'm trying to give the empty DestName the value of Sourcename.



Here is my Python codes:



import re
import xml.etree.ElementTree as ET
tree = ET.parse("SPPID04A_BG3 - Copy - Copy.xml")
root = tree.getroot()
for SelectListMap in root.iter('SelectListMap'):
#DestName.text = str(DestName)
for node in tree.iter('SelectListMap'):
SourceName = node.attrib.get('SourceName')
SelectListMap.set('DestName', SourceName)
tree.write("SPPID04A_BG3 - Copy - Copy.xml")


This program is not working on the right way. any help or ideas?
Thanks!










share|improve this question














I have an xml file that contain a lot of information and tags.
For example I have this tag:



<SelectListMap SourceName="Document Type" SourceNumber="43" DestName="Document Type" DestNumber="43"/>


I have 40 other tags like this one with the same nodes, but the value of these nodes is different in each tag.
SourceName and DestName have the same value.



In some tags the DestName value is empty like this one:



<SelectListMap SourceName="Boolean Values" SourceNumber="73" DestName="" DestNumber="0" IsInternal="True"/>


So, I'm trying to give the empty DestName the value of Sourcename.



Here is my Python codes:



import re
import xml.etree.ElementTree as ET
tree = ET.parse("SPPID04A_BG3 - Copy - Copy.xml")
root = tree.getroot()
for SelectListMap in root.iter('SelectListMap'):
#DestName.text = str(DestName)
for node in tree.iter('SelectListMap'):
SourceName = node.attrib.get('SourceName')
SelectListMap.set('DestName', SourceName)
tree.write("SPPID04A_BG3 - Copy - Copy.xml")


This program is not working on the right way. any help or ideas?
Thanks!







python






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 1 at 10:46









mashaelmashael

11




11




migrated from cs.stackexchange.com Jan 1 at 12:16


This question came from our site for students, researchers and practitioners of computer science.









migrated from cs.stackexchange.com Jan 1 at 12:16


This question came from our site for students, researchers and practitioners of computer science.










  • 3





    Hi, could you please show the erroneous output or any other errors it is producing given an input?

    – Yuvraj Jaiswal
    Jan 1 at 12:20











  • When I run it I don't have error. But the DestName value in all tags change to a wrong value. My idea is give the empty DestName the same value of SourceName.

    – mashael
    Jan 1 at 13:25














  • 3





    Hi, could you please show the erroneous output or any other errors it is producing given an input?

    – Yuvraj Jaiswal
    Jan 1 at 12:20











  • When I run it I don't have error. But the DestName value in all tags change to a wrong value. My idea is give the empty DestName the same value of SourceName.

    – mashael
    Jan 1 at 13:25








3




3





Hi, could you please show the erroneous output or any other errors it is producing given an input?

– Yuvraj Jaiswal
Jan 1 at 12:20





Hi, could you please show the erroneous output or any other errors it is producing given an input?

– Yuvraj Jaiswal
Jan 1 at 12:20













When I run it I don't have error. But the DestName value in all tags change to a wrong value. My idea is give the empty DestName the same value of SourceName.

– mashael
Jan 1 at 13:25





When I run it I don't have error. But the DestName value in all tags change to a wrong value. My idea is give the empty DestName the same value of SourceName.

– mashael
Jan 1 at 13:25












1 Answer
1






active

oldest

votes


















2














You never check the if the DestName attribute is empty. If you replace the first for loop with the following, you should get what you want:



for SelectListMap in root.iter('SelectListMap'):
if SelectListMap.get("DestName") == "":
SourceName = SelectListMap.get("SourceName")
SelectListMap.set("DestName", SourceName)





share|improve this answer
























  • Yes, It's working now. Thank you so much!

    – mashael
    Jan 1 at 14:25











  • Now when I run it the empty DestName will have the right value. But the order of the tag changed. The tag will be like this: <SelectListMap DestName="Document Type" DestNumber="43" SourceName="Document Type" SourceNumber="43" /> So, how can I give the DestName the value but without changing the order of nodes inside the tag.

    – mashael
    Jan 3 at 4:49













  • As far as I understand, Etree always outputs attributes in alphabetic order. See bugs.python.org/issue34160 where preserving the order is discussed.

    – BenignBeppe
    Jan 5 at 10:18











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%2f53995361%2fsolving-xml-problem-by-python%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









2














You never check the if the DestName attribute is empty. If you replace the first for loop with the following, you should get what you want:



for SelectListMap in root.iter('SelectListMap'):
if SelectListMap.get("DestName") == "":
SourceName = SelectListMap.get("SourceName")
SelectListMap.set("DestName", SourceName)





share|improve this answer
























  • Yes, It's working now. Thank you so much!

    – mashael
    Jan 1 at 14:25











  • Now when I run it the empty DestName will have the right value. But the order of the tag changed. The tag will be like this: <SelectListMap DestName="Document Type" DestNumber="43" SourceName="Document Type" SourceNumber="43" /> So, how can I give the DestName the value but without changing the order of nodes inside the tag.

    – mashael
    Jan 3 at 4:49













  • As far as I understand, Etree always outputs attributes in alphabetic order. See bugs.python.org/issue34160 where preserving the order is discussed.

    – BenignBeppe
    Jan 5 at 10:18
















2














You never check the if the DestName attribute is empty. If you replace the first for loop with the following, you should get what you want:



for SelectListMap in root.iter('SelectListMap'):
if SelectListMap.get("DestName") == "":
SourceName = SelectListMap.get("SourceName")
SelectListMap.set("DestName", SourceName)





share|improve this answer
























  • Yes, It's working now. Thank you so much!

    – mashael
    Jan 1 at 14:25











  • Now when I run it the empty DestName will have the right value. But the order of the tag changed. The tag will be like this: <SelectListMap DestName="Document Type" DestNumber="43" SourceName="Document Type" SourceNumber="43" /> So, how can I give the DestName the value but without changing the order of nodes inside the tag.

    – mashael
    Jan 3 at 4:49













  • As far as I understand, Etree always outputs attributes in alphabetic order. See bugs.python.org/issue34160 where preserving the order is discussed.

    – BenignBeppe
    Jan 5 at 10:18














2












2








2







You never check the if the DestName attribute is empty. If you replace the first for loop with the following, you should get what you want:



for SelectListMap in root.iter('SelectListMap'):
if SelectListMap.get("DestName") == "":
SourceName = SelectListMap.get("SourceName")
SelectListMap.set("DestName", SourceName)





share|improve this answer













You never check the if the DestName attribute is empty. If you replace the first for loop with the following, you should get what you want:



for SelectListMap in root.iter('SelectListMap'):
if SelectListMap.get("DestName") == "":
SourceName = SelectListMap.get("SourceName")
SelectListMap.set("DestName", SourceName)






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 1 at 13:51









BenignBeppeBenignBeppe

1565




1565













  • Yes, It's working now. Thank you so much!

    – mashael
    Jan 1 at 14:25











  • Now when I run it the empty DestName will have the right value. But the order of the tag changed. The tag will be like this: <SelectListMap DestName="Document Type" DestNumber="43" SourceName="Document Type" SourceNumber="43" /> So, how can I give the DestName the value but without changing the order of nodes inside the tag.

    – mashael
    Jan 3 at 4:49













  • As far as I understand, Etree always outputs attributes in alphabetic order. See bugs.python.org/issue34160 where preserving the order is discussed.

    – BenignBeppe
    Jan 5 at 10:18



















  • Yes, It's working now. Thank you so much!

    – mashael
    Jan 1 at 14:25











  • Now when I run it the empty DestName will have the right value. But the order of the tag changed. The tag will be like this: <SelectListMap DestName="Document Type" DestNumber="43" SourceName="Document Type" SourceNumber="43" /> So, how can I give the DestName the value but without changing the order of nodes inside the tag.

    – mashael
    Jan 3 at 4:49













  • As far as I understand, Etree always outputs attributes in alphabetic order. See bugs.python.org/issue34160 where preserving the order is discussed.

    – BenignBeppe
    Jan 5 at 10:18

















Yes, It's working now. Thank you so much!

– mashael
Jan 1 at 14:25





Yes, It's working now. Thank you so much!

– mashael
Jan 1 at 14:25













Now when I run it the empty DestName will have the right value. But the order of the tag changed. The tag will be like this: <SelectListMap DestName="Document Type" DestNumber="43" SourceName="Document Type" SourceNumber="43" /> So, how can I give the DestName the value but without changing the order of nodes inside the tag.

– mashael
Jan 3 at 4:49







Now when I run it the empty DestName will have the right value. But the order of the tag changed. The tag will be like this: <SelectListMap DestName="Document Type" DestNumber="43" SourceName="Document Type" SourceNumber="43" /> So, how can I give the DestName the value but without changing the order of nodes inside the tag.

– mashael
Jan 3 at 4:49















As far as I understand, Etree always outputs attributes in alphabetic order. See bugs.python.org/issue34160 where preserving the order is discussed.

– BenignBeppe
Jan 5 at 10:18





As far as I understand, Etree always outputs attributes in alphabetic order. See bugs.python.org/issue34160 where preserving the order is discussed.

– BenignBeppe
Jan 5 at 10:18




















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%2f53995361%2fsolving-xml-problem-by-python%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







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith