5

I just started python and was trying to parse the xml file using ElementTree. But the problem is I have one tag with CDATA which is removed after tree.write.

So basically I had this tag

<content><![CDATA[eclipse.ver=1&encoding/ <project>=UTF-8${line.sep}]]></content>

which is change to

<content>eclipse.ver=1&encoding/&lt;project&gt;=UTF-8${line.sep}</content>

I tried google it but was not very helpful. So can anyone help me, how can I get exact same content within the tag??

2
  • Possible duplicate of stackoverflow.com/questions/174890/… Commented Oct 7, 2016 at 3:43
  • 1
    I think this question is to output the xml file hardcoding the xml tags. But I want to edit my xml file which contains CDATA. Commented Oct 7, 2016 at 3:51

1 Answer 1

-4
from xml.etree.ElementTree import ElementTree
import re
l=[]
tree = ElementTree()
tree.parse("YOURFILE.xml")
root = tree.getroot()

for parent in root.findall('.//content/..'):
    for child in parent.findall("content"):
        if(child.text != None):
            l.append(child.text)        
Sign up to request clarification or add additional context in comments.

1 Comment

This does not answer the question.

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.