solving XML problem by Python
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
migrated from cs.stackexchange.com Jan 1 at 12:16
This question came from our site for students, researchers and practitioners of computer science.
add a comment |
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
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
add a comment |
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
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
python
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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)
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
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
add a comment |
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)
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
add a comment |
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)
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)
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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

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