0

i want to add entire tag to xml, below is my XML format.

<?xml version="1.0" encoding="UTF-8"?>
<ca st="true" name="XMLConfig">
    <app>
        <!---   I want to add entire commneted tag to XML . ! 
        <ar ty="co" name="st">    
            <ly ty="pt">
                 <pt>value</pt>
            </Layout>
        </ar>                    -->

        <roll name="roll" fN="file.log" fP="logs.gz">
            <ly type="ptl">
                 <pt>value</pt>
            </ly>
            <po>
    <!--            Comment /> -->
                <si size="100 MB" />
        <!--        Comment /> -->
            </po>
            <de fI="max" max="10"/>   
        </roll>       
    </app>

as shown in above file i want to add this tag in file

    <ar ty="co" name="st">    
        <ly ty="pt">
             <pt>value</pt>
        </Layout>
    </ar>

this is where i reached so far..

for appenders in tree.xpath('//Appenders'):
    if appenders.getchildren():
        appenders.remove(appenders.getchildren()[0])
        appenders.insert(0, appenders.getparent().append(etree.fromstring('<ar ty="co" name="st"> <ly ty="pt"><pt>value</pt></Layout></ar>')))

this is removing all other content after new content. any help will be appreciated.!

2 Answers 2

1

In my opinion the first way you did it is way better. You just made some mistakes in your insert line, it should be this:

appenders.insert(0, etree.fromstring('<ar ty="co" name="st"> <ly ty="pt"><pt>value</pt></ly></ar>')))

I'm surprised it didn't throw an error for you because your insert line is basically this:

appenders.insert(0,None)

Also I noticed you do something in all of your questions:

  1. You leave out some line(s) of your xml file. (I mean why?)
  2. You shorten the tag names in your xml but you keep their long version in the code, which is kind of annoying because the person who wants to answer you have to change the code again to see if it is working.
Sign up to request clarification or add additional context in comments.

2 Comments

Agreed, I went back and checked on this. the first method is working as it should. Thanks for the reply,! On 1st : because xml file has more than 200 lines .. and i think all those details are not needed. On 2nd : I will take care this from now onwords.. !
yes, the first way i did is giving me appenders.insert(0,None) and throwing exception. but i swear it was working for me before(removing all other content after new content). I think i messed up something while posting the code (I should not make changes in variable at all )
0

I got it working, !

for apps in tree.xpath('//app'):  
    if appenders.tag == 'app':
        appenders.insert(0, etree.SubElement(appenders, 'ar', ty="Co", name="st"))
        for appender in tree.xpath('//ar'):
            appender.insert(0, etree.SubElement(appender, 'ly', ty="pt"))
            for layout in tree.xpath('//ly'):
                layout.insert(0, etree.SubElement(layout, 'pt'))
                for pattern in tree.xpath('//pt'):
                    pattern.text = 'value'


tree.write(r'C:\value.xml', xml_declaration=True, encoding='UTF-8')  

if anyone has better way to do this .. please let me know to so i can improve on this .!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.