1

I am trying to parse a xml file where I had wanted to grab the string of objlocation and change the contents of the string.

This are the contents of the xml files I have:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<publish show="STATE">

    <pubgroup objtype="ELE" location="/user_data/STATE/ITEM/character/ANM/ANM_rig_WALK_sg_v001/ANM_rig_WALK_sg_v001.xml">

        <member objidx="15283942" objlabel="anm" objlocation="/user_data/STATE/ITEM/character/ANM/ANM_rig_WALK_sg_v001/ANM_rig_WALK_sg_v001.anm"/>

        <member objidx="15283952" objlabel="fbx" objlocation="/user_data/STATE/ITEM/character/ANM/ANM_rig_WALK_sg_v001/ANM_rig_WALK_sg_v001_M_WALK_None.fbx"/>

        <member objidx="15283962" objlabel="mov" objlocation="/user_data/STATE/ITEM/character/ANM/ANM_rig_WALK_sg_v001/ANM_rig_WALK_sg_v001.mov"/>

        <member objidx="15283972" objlabel="libraryinfo" objlocation="/user_data/STATE/ITEM/character/ANM/ANM_rig_WALK_sg_v001/ANM_rig_WALK_sg_v001.json"/>

        <member objidx="15283982" objlabel="thumbnail" objlocation="/user_data/STATE/ITEM/character/ANM/ANM_rig_WALK_sg_v001/ANM_rig_WALK_sg_v001.mng"/>

    </pubgroup>
</publish>

I tried .firstChild or .childNodes[], it is printing the out contents as my xml files. This is of the list of xml files that I am trying to parse where its format is about the same.

I am trying to do this is pythonic way

2
  • 1
    How are you parsing this file? Can you share your code please? If you are using the minidom, are you aware of the warning at the top of the documentation? You'd be far better of using the ElementTree API instead. Commented Feb 2, 2015 at 9:53
  • I was actually using minidom that I grab off from an online tutorial. My bad on this.. Commented Feb 2, 2015 at 11:18

2 Answers 2

2

You can easily modify your xml file using the ElementTree API

from xml.etree.ElementTree import parse
doc = parse('data.xml')
root = doc.getroot()
for t in root.iterfind('pubgroup/member'):
    t.attrib['objlocation'] = "spam"

doc.write('output.xml', xml_declaration=True)

The iterfind method returns a generator function instead of list which is very convenient if your have xml file is very large

Output

<?xml version='1.0' encoding='us-ascii'?>
<publish show="STATE">

    <pubgroup location="/user_data/STATE/ITEM/character/ANM/ANM_rig_WALK_sg_v001/ANM_rig_WALK_sg_v001.xml" objtype="ELE">

        <member objidx="15283942" objlabel="anm" objlocation="spam" />

        <member objidx="15283952" objlabel="fbx" objlocation="spam" />

        <member objidx="15283962" objlabel="mov" objlocation="spam" />

        <member objidx="15283972" objlabel="libraryinfo" objlocation="spam" />

    <member objidx="15283982" objlabel="thumbnail" objlocation="spam" />

</pubgroup>

Here spam is objlocation new value.

Sign up to request clarification or add additional context in comments.

2 Comments

just a question, say will it be possible if in the event, I had wanted to add/delete/edit the contents of a line? Eg. <member objidx="15283942" objlabel="anm" objlocation="spam" />?
@dissidia you can insert/remove or edit any tag you want. You may need to read the documentation for more infos docs.python.org/3/library/xml.etree.elementtree.html#tutorial
2

The shortest code I can suggest:

from xml.etree.ElementTree import ElementTree
tree = ElementTree()
root = tree.parse('test.txt') # root represents <publish> tag

for member in root.findall('pubgroup/member'):
    print member.attrib['objlocation']

Output:

/user_data/STATE/ITEM/character/ANM/ANM_rig_WALK_sg_v001/ANM_rig_WALK_sg_v001.anm
/user_data/STATE/ITEM/character/ANM/ANM_rig_WALK_sg_v001/ANM_rig_WALK_sg_v001_M_WALK_None.fbx
/user_data/STATE/ITEM/character/ANM/ANM_rig_WALK_sg_v001/ANM_rig_WALK_sg_v001.mov
/user_data/STATE/ITEM/character/ANM/ANM_rig_WALK_sg_v001/ANM_rig_WALK_sg_v001.json
/user_data/STATE/ITEM/character/ANM/ANM_rig_WALK_sg_v001/ANM_rig_WALK_sg_v001.mng

To make changes:

for member in root.findall('pubgroup/member'):
    member.attrib['objlocation'] = 'changed'
tree.write('output.txt')

1 Comment

Hi, sorry for the late reply. Was wondering if it is possible to edit just the objlocation of objlabel="libraryinfo", while leaving the rest as it is?

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.