1

I have a resx file that uses XML with a bunch of data that looks like this:

<data name="key_first" xml:space="preserve">
    <value>Text 1</value>
</data>
<data name="key_second" xml:space="preserve">
    <value>Text 2</value>
</data>
<data name="key_third" xml:space="preserve">
    <value>Text 3</value>
</data>

where the name values all share a word (eg: "key"). What I wanted to do in Python was take this 'data' name value ("key+anything that follows") and add it to the 'value' text, and then save the new file but I'm very new to the XML/Python world and I'm kind of lost. I want it to look something like this:

<data name="key_first" xml:space="preserve">
    <value>[key_first] Text 1</value>
</data>
<data name="key_second" xml:space="preserve">
    <value>[key_second] Text 2</value>
</data>
<data name="key_third" xml:space="preserve">
    <value>[key_third] Text 3</value>
</data>

How do I do this? Should I use minidom or Element Tree?

2
  • Technically, any xml parser will do the job - you could use a sax parser FWIW -, but no one ever got fired for choosing ElementTree. Commented Jul 18, 2016 at 15:27
  • Personally, I would use lxml with XSLT, and add custom callback functions if I needed to. Commented Jul 18, 2016 at 16:01

2 Answers 2

2

Which XML parser to choose is up to you, but here is how you can approach this problem with xml.etree.ElementTree: the idea is to iterate over all data nodes, get the name attribute value from the .attrib dictionary, locate the value element inside and set the .text appropriately:

import xml.etree.ElementTree as ET

data = """<parent>
    <data name="key_first" xml:space="preserve">
        <value>Text 1</value>
    </data>
    <data name="key_second" xml:space="preserve">
        <value>Text 2</value>
    </data>
    <data name="key_third" xml:space="preserve">
        <value>Text 3</value>
    </data>
</parent>"""

tree = ET.fromstring(data)

for data in tree.findall("data"):
    name = data.attrib["name"]
    value = data.find("value")
    value.text = "[%s] %s" % (name, value.text)

print(ET.tostring(tree))

Prints:

<parent>
    <data name="key_first" xml:space="preserve">
        <value>[key_first] Text 1</value>
    </data>
    <data name="key_second" xml:space="preserve">
        <value>[key_second] Text 2</value>
    </data>
    <data name="key_third" xml:space="preserve">
        <value>[key_third] Text 3</value>
    </data>
</parent>
Sign up to request clarification or add additional context in comments.

Comments

0

Use the ElementTree API to read the XML file and then you can use find to find the word you want to replace or use it as an index and then from there you can insert what you need to insert. See links below:

How to update/modify an XML file in python?

https://docs.python.org/2/library/xml.etree.elementtree.html

http://luisartola.com/software/2010/easy-xml-in-python/

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.