Append Tags with attributes to XML with minidom





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







0















I am parsing a xml using minidom. What I want to achieve is find all the tags with a certain attribute and then append a new tag with different attribute value below it. For e.g



Lets say the tag is:



<PROPVAL ISFORMULA="N" LOCALIZABLE="false" NAME="EntDataRegion" VALUE="XYZ"/>


I found all tags that has attribute value NAME = "EntReg" . Now I want to append a tag below it.



<PROPVAL ISFORMULA="N" LOCALIZABLE="false" NAME="EntRoutingDataSelected" VALUE="yes"/>


The tag name and value will we static i.e I want to append same tag where ever the PROPVAL attribute NAME has value EntDataRegion.



So far I have written this and found all PROPVAL tags:



xmldoc = minidom.parse('test.xml')
prop_val = xmldoc.getElementsByTagName('PROPVAL')
for i in prop_val:
if i.attributes['NAME'].value == 'PB:EntDataRegion':
print(i.attributes['VALUE'].value)


Edit



The XML structure is like this:



<?xml version="1.0" encoding="UTF-8"?>
<MOD version="3">
<CLASS>
<CLASS COMMENT="" DEFAULTPRIORITY="50">
<PROPVAL ISFORMULA="N" LOCALIZABLE="false" NAME="EntDataRegion" VALUE="XYZ"/>
<RULE LOCALIZABLE="false"/>
<ITEM>
<PROPVAL ISFORMULA="N" LOCALIZABLE="false" NAME="HIDE" VALUE="XYZ"/>
</ITEM>
</CLASS>
<CLASS COMMENT="" DEFAULTPRIORITY="50">
<PROPVAL ISFORMULA="N" LOCALIZABLE="false" NAME="EntDataRegion" VALUE="XYZ"/>
<RULE LOCALIZABLE="false"/>
<ITEM>
<PROPVAL ISFORMULA="N" LOCALIZABLE="false" NAME="HIDE" VALUE="XYZ"/>
</ITEM>
</CLASS>
</CLASS>
<CLASS>
<ITEM>
<PROPVAL ISFORMULA="N" LOCALIZABLE="false" NAME="EntDataRegion" VALUE="XYZ"/>
<PROPVAL ISFORMULA="N" LOCALIZABLE="false" NAME="EntRoutingDataSelected" VALUE="yes"/>
</ITEM>
</CLASS>
</MOD>


The xml has two tags CLASS and <ITEM> . The class tag can have item tag as child tag. But before having item as child tag it will always have a bunch of <PROPVAL> tags.



The idea is to insert



<PROPVAL ISFORMULA="N" LOCALIZABLE="false" NAME="EntRoutingDataSelected" VALUE="yes"/>



before



<PROPVAL ISFORMULA="N" LOCALIZABLE="false" NAME="EntDataRegion" VALUE="XYZ"/>



The issue is I am able to insert the above tag. The fact that class tag which has item tag also takes this property. Whereas I only want ITEM tags to have this property if they have <PROPVAL NAME="EntDataRegion">



This is what I have tried:



from xml.dom import minidom
xmldoc = minidom.parse('test.xml')
prop_val = xmldoc.getElementsByTagName('PROPVAL')
class_xml = xmldoc.getElementsByTagName('CLASS')
item_xml = xmldoc.getElementsByTagName('ITEM')
newScript = xmldoc.createElement("PROPVAL")
newScript.setAttribute("ISFORMULA" , "N")
newScript.setAttribute("LOCALIZABLE", "false")
newScript.setAttribute("NAME", "EntRoutingDataSelected")
newScript.setAttribute("VALUE", "yes")
print(newScript.toxml())

for i in range(len(class_xml)):
item = class_xml[i]
item_chidren = item.childNodes
item.insertBefore(newScript, item_chidren[4])


with open('newtest.xml', 'w') as f:
xmldoc.writexml(f)









share|improve this question

























  • Look at insertBefore: docs.python.org/2/library/…

    – Tomalak
    Jan 3 at 9:18













  • Looked and tried but didn't worked. Maybe I am doing something wrong

    – Duck_dragon
    Jan 3 at 9:21











  • Please provide a Minimal, Complete, and Verifiable example.

    – mzjn
    Jan 3 at 9:28











  • What's happening when you run your second code sample, and what did you expect that would happen?

    – Tomalak
    Jan 3 at 9:32











  • @Tomalak I have properly stated what I expect to happen and where it is going wrong

    – Duck_dragon
    Jan 3 at 12:06


















0















I am parsing a xml using minidom. What I want to achieve is find all the tags with a certain attribute and then append a new tag with different attribute value below it. For e.g



Lets say the tag is:



<PROPVAL ISFORMULA="N" LOCALIZABLE="false" NAME="EntDataRegion" VALUE="XYZ"/>


I found all tags that has attribute value NAME = "EntReg" . Now I want to append a tag below it.



<PROPVAL ISFORMULA="N" LOCALIZABLE="false" NAME="EntRoutingDataSelected" VALUE="yes"/>


The tag name and value will we static i.e I want to append same tag where ever the PROPVAL attribute NAME has value EntDataRegion.



So far I have written this and found all PROPVAL tags:



xmldoc = minidom.parse('test.xml')
prop_val = xmldoc.getElementsByTagName('PROPVAL')
for i in prop_val:
if i.attributes['NAME'].value == 'PB:EntDataRegion':
print(i.attributes['VALUE'].value)


Edit



The XML structure is like this:



<?xml version="1.0" encoding="UTF-8"?>
<MOD version="3">
<CLASS>
<CLASS COMMENT="" DEFAULTPRIORITY="50">
<PROPVAL ISFORMULA="N" LOCALIZABLE="false" NAME="EntDataRegion" VALUE="XYZ"/>
<RULE LOCALIZABLE="false"/>
<ITEM>
<PROPVAL ISFORMULA="N" LOCALIZABLE="false" NAME="HIDE" VALUE="XYZ"/>
</ITEM>
</CLASS>
<CLASS COMMENT="" DEFAULTPRIORITY="50">
<PROPVAL ISFORMULA="N" LOCALIZABLE="false" NAME="EntDataRegion" VALUE="XYZ"/>
<RULE LOCALIZABLE="false"/>
<ITEM>
<PROPVAL ISFORMULA="N" LOCALIZABLE="false" NAME="HIDE" VALUE="XYZ"/>
</ITEM>
</CLASS>
</CLASS>
<CLASS>
<ITEM>
<PROPVAL ISFORMULA="N" LOCALIZABLE="false" NAME="EntDataRegion" VALUE="XYZ"/>
<PROPVAL ISFORMULA="N" LOCALIZABLE="false" NAME="EntRoutingDataSelected" VALUE="yes"/>
</ITEM>
</CLASS>
</MOD>


The xml has two tags CLASS and <ITEM> . The class tag can have item tag as child tag. But before having item as child tag it will always have a bunch of <PROPVAL> tags.



The idea is to insert



<PROPVAL ISFORMULA="N" LOCALIZABLE="false" NAME="EntRoutingDataSelected" VALUE="yes"/>



before



<PROPVAL ISFORMULA="N" LOCALIZABLE="false" NAME="EntDataRegion" VALUE="XYZ"/>



The issue is I am able to insert the above tag. The fact that class tag which has item tag also takes this property. Whereas I only want ITEM tags to have this property if they have <PROPVAL NAME="EntDataRegion">



This is what I have tried:



from xml.dom import minidom
xmldoc = minidom.parse('test.xml')
prop_val = xmldoc.getElementsByTagName('PROPVAL')
class_xml = xmldoc.getElementsByTagName('CLASS')
item_xml = xmldoc.getElementsByTagName('ITEM')
newScript = xmldoc.createElement("PROPVAL")
newScript.setAttribute("ISFORMULA" , "N")
newScript.setAttribute("LOCALIZABLE", "false")
newScript.setAttribute("NAME", "EntRoutingDataSelected")
newScript.setAttribute("VALUE", "yes")
print(newScript.toxml())

for i in range(len(class_xml)):
item = class_xml[i]
item_chidren = item.childNodes
item.insertBefore(newScript, item_chidren[4])


with open('newtest.xml', 'w') as f:
xmldoc.writexml(f)









share|improve this question

























  • Look at insertBefore: docs.python.org/2/library/…

    – Tomalak
    Jan 3 at 9:18













  • Looked and tried but didn't worked. Maybe I am doing something wrong

    – Duck_dragon
    Jan 3 at 9:21











  • Please provide a Minimal, Complete, and Verifiable example.

    – mzjn
    Jan 3 at 9:28











  • What's happening when you run your second code sample, and what did you expect that would happen?

    – Tomalak
    Jan 3 at 9:32











  • @Tomalak I have properly stated what I expect to happen and where it is going wrong

    – Duck_dragon
    Jan 3 at 12:06














0












0








0








I am parsing a xml using minidom. What I want to achieve is find all the tags with a certain attribute and then append a new tag with different attribute value below it. For e.g



Lets say the tag is:



<PROPVAL ISFORMULA="N" LOCALIZABLE="false" NAME="EntDataRegion" VALUE="XYZ"/>


I found all tags that has attribute value NAME = "EntReg" . Now I want to append a tag below it.



<PROPVAL ISFORMULA="N" LOCALIZABLE="false" NAME="EntRoutingDataSelected" VALUE="yes"/>


The tag name and value will we static i.e I want to append same tag where ever the PROPVAL attribute NAME has value EntDataRegion.



So far I have written this and found all PROPVAL tags:



xmldoc = minidom.parse('test.xml')
prop_val = xmldoc.getElementsByTagName('PROPVAL')
for i in prop_val:
if i.attributes['NAME'].value == 'PB:EntDataRegion':
print(i.attributes['VALUE'].value)


Edit



The XML structure is like this:



<?xml version="1.0" encoding="UTF-8"?>
<MOD version="3">
<CLASS>
<CLASS COMMENT="" DEFAULTPRIORITY="50">
<PROPVAL ISFORMULA="N" LOCALIZABLE="false" NAME="EntDataRegion" VALUE="XYZ"/>
<RULE LOCALIZABLE="false"/>
<ITEM>
<PROPVAL ISFORMULA="N" LOCALIZABLE="false" NAME="HIDE" VALUE="XYZ"/>
</ITEM>
</CLASS>
<CLASS COMMENT="" DEFAULTPRIORITY="50">
<PROPVAL ISFORMULA="N" LOCALIZABLE="false" NAME="EntDataRegion" VALUE="XYZ"/>
<RULE LOCALIZABLE="false"/>
<ITEM>
<PROPVAL ISFORMULA="N" LOCALIZABLE="false" NAME="HIDE" VALUE="XYZ"/>
</ITEM>
</CLASS>
</CLASS>
<CLASS>
<ITEM>
<PROPVAL ISFORMULA="N" LOCALIZABLE="false" NAME="EntDataRegion" VALUE="XYZ"/>
<PROPVAL ISFORMULA="N" LOCALIZABLE="false" NAME="EntRoutingDataSelected" VALUE="yes"/>
</ITEM>
</CLASS>
</MOD>


The xml has two tags CLASS and <ITEM> . The class tag can have item tag as child tag. But before having item as child tag it will always have a bunch of <PROPVAL> tags.



The idea is to insert



<PROPVAL ISFORMULA="N" LOCALIZABLE="false" NAME="EntRoutingDataSelected" VALUE="yes"/>



before



<PROPVAL ISFORMULA="N" LOCALIZABLE="false" NAME="EntDataRegion" VALUE="XYZ"/>



The issue is I am able to insert the above tag. The fact that class tag which has item tag also takes this property. Whereas I only want ITEM tags to have this property if they have <PROPVAL NAME="EntDataRegion">



This is what I have tried:



from xml.dom import minidom
xmldoc = minidom.parse('test.xml')
prop_val = xmldoc.getElementsByTagName('PROPVAL')
class_xml = xmldoc.getElementsByTagName('CLASS')
item_xml = xmldoc.getElementsByTagName('ITEM')
newScript = xmldoc.createElement("PROPVAL")
newScript.setAttribute("ISFORMULA" , "N")
newScript.setAttribute("LOCALIZABLE", "false")
newScript.setAttribute("NAME", "EntRoutingDataSelected")
newScript.setAttribute("VALUE", "yes")
print(newScript.toxml())

for i in range(len(class_xml)):
item = class_xml[i]
item_chidren = item.childNodes
item.insertBefore(newScript, item_chidren[4])


with open('newtest.xml', 'w') as f:
xmldoc.writexml(f)









share|improve this question
















I am parsing a xml using minidom. What I want to achieve is find all the tags with a certain attribute and then append a new tag with different attribute value below it. For e.g



Lets say the tag is:



<PROPVAL ISFORMULA="N" LOCALIZABLE="false" NAME="EntDataRegion" VALUE="XYZ"/>


I found all tags that has attribute value NAME = "EntReg" . Now I want to append a tag below it.



<PROPVAL ISFORMULA="N" LOCALIZABLE="false" NAME="EntRoutingDataSelected" VALUE="yes"/>


The tag name and value will we static i.e I want to append same tag where ever the PROPVAL attribute NAME has value EntDataRegion.



So far I have written this and found all PROPVAL tags:



xmldoc = minidom.parse('test.xml')
prop_val = xmldoc.getElementsByTagName('PROPVAL')
for i in prop_val:
if i.attributes['NAME'].value == 'PB:EntDataRegion':
print(i.attributes['VALUE'].value)


Edit



The XML structure is like this:



<?xml version="1.0" encoding="UTF-8"?>
<MOD version="3">
<CLASS>
<CLASS COMMENT="" DEFAULTPRIORITY="50">
<PROPVAL ISFORMULA="N" LOCALIZABLE="false" NAME="EntDataRegion" VALUE="XYZ"/>
<RULE LOCALIZABLE="false"/>
<ITEM>
<PROPVAL ISFORMULA="N" LOCALIZABLE="false" NAME="HIDE" VALUE="XYZ"/>
</ITEM>
</CLASS>
<CLASS COMMENT="" DEFAULTPRIORITY="50">
<PROPVAL ISFORMULA="N" LOCALIZABLE="false" NAME="EntDataRegion" VALUE="XYZ"/>
<RULE LOCALIZABLE="false"/>
<ITEM>
<PROPVAL ISFORMULA="N" LOCALIZABLE="false" NAME="HIDE" VALUE="XYZ"/>
</ITEM>
</CLASS>
</CLASS>
<CLASS>
<ITEM>
<PROPVAL ISFORMULA="N" LOCALIZABLE="false" NAME="EntDataRegion" VALUE="XYZ"/>
<PROPVAL ISFORMULA="N" LOCALIZABLE="false" NAME="EntRoutingDataSelected" VALUE="yes"/>
</ITEM>
</CLASS>
</MOD>


The xml has two tags CLASS and <ITEM> . The class tag can have item tag as child tag. But before having item as child tag it will always have a bunch of <PROPVAL> tags.



The idea is to insert



<PROPVAL ISFORMULA="N" LOCALIZABLE="false" NAME="EntRoutingDataSelected" VALUE="yes"/>



before



<PROPVAL ISFORMULA="N" LOCALIZABLE="false" NAME="EntDataRegion" VALUE="XYZ"/>



The issue is I am able to insert the above tag. The fact that class tag which has item tag also takes this property. Whereas I only want ITEM tags to have this property if they have <PROPVAL NAME="EntDataRegion">



This is what I have tried:



from xml.dom import minidom
xmldoc = minidom.parse('test.xml')
prop_val = xmldoc.getElementsByTagName('PROPVAL')
class_xml = xmldoc.getElementsByTagName('CLASS')
item_xml = xmldoc.getElementsByTagName('ITEM')
newScript = xmldoc.createElement("PROPVAL")
newScript.setAttribute("ISFORMULA" , "N")
newScript.setAttribute("LOCALIZABLE", "false")
newScript.setAttribute("NAME", "EntRoutingDataSelected")
newScript.setAttribute("VALUE", "yes")
print(newScript.toxml())

for i in range(len(class_xml)):
item = class_xml[i]
item_chidren = item.childNodes
item.insertBefore(newScript, item_chidren[4])


with open('newtest.xml', 'w') as f:
xmldoc.writexml(f)






python xml elementtree minidom






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 12:45







Duck_dragon

















asked Jan 3 at 8:29









Duck_dragonDuck_dragon

838




838













  • Look at insertBefore: docs.python.org/2/library/…

    – Tomalak
    Jan 3 at 9:18













  • Looked and tried but didn't worked. Maybe I am doing something wrong

    – Duck_dragon
    Jan 3 at 9:21











  • Please provide a Minimal, Complete, and Verifiable example.

    – mzjn
    Jan 3 at 9:28











  • What's happening when you run your second code sample, and what did you expect that would happen?

    – Tomalak
    Jan 3 at 9:32











  • @Tomalak I have properly stated what I expect to happen and where it is going wrong

    – Duck_dragon
    Jan 3 at 12:06



















  • Look at insertBefore: docs.python.org/2/library/…

    – Tomalak
    Jan 3 at 9:18













  • Looked and tried but didn't worked. Maybe I am doing something wrong

    – Duck_dragon
    Jan 3 at 9:21











  • Please provide a Minimal, Complete, and Verifiable example.

    – mzjn
    Jan 3 at 9:28











  • What's happening when you run your second code sample, and what did you expect that would happen?

    – Tomalak
    Jan 3 at 9:32











  • @Tomalak I have properly stated what I expect to happen and where it is going wrong

    – Duck_dragon
    Jan 3 at 12:06

















Look at insertBefore: docs.python.org/2/library/…

– Tomalak
Jan 3 at 9:18







Look at insertBefore: docs.python.org/2/library/…

– Tomalak
Jan 3 at 9:18















Looked and tried but didn't worked. Maybe I am doing something wrong

– Duck_dragon
Jan 3 at 9:21





Looked and tried but didn't worked. Maybe I am doing something wrong

– Duck_dragon
Jan 3 at 9:21













Please provide a Minimal, Complete, and Verifiable example.

– mzjn
Jan 3 at 9:28





Please provide a Minimal, Complete, and Verifiable example.

– mzjn
Jan 3 at 9:28













What's happening when you run your second code sample, and what did you expect that would happen?

– Tomalak
Jan 3 at 9:32





What's happening when you run your second code sample, and what did you expect that would happen?

– Tomalak
Jan 3 at 9:32













@Tomalak I have properly stated what I expect to happen and where it is going wrong

– Duck_dragon
Jan 3 at 12:06





@Tomalak I have properly stated what I expect to happen and where it is going wrong

– Duck_dragon
Jan 3 at 12:06












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%2f54018714%2fappend-tags-with-attributes-to-xml-with-minidom%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%2f54018714%2fappend-tags-with-attributes-to-xml-with-minidom%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

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

Npm cannot find a required file even through it is in the searched directory