Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change text value with lxml

Tags:

python

lxml

I have an xml file - here is a snippet..

  <gmd_fileIdentifier>
    <gco_CharacterString>{0328cb65-b564-495a-b17e-e49e04864ab7}</gco_CharacterString>
 </gmd_fileIdentifier>
          <gmd_identifier>
            <gmd_RS_Identifier>
              <gmd_authority gco_nilReason="missing" />
              <gmd_code>
                <gco_CharacterString>0000</gco_CharacterString>
              </gmd_code>
              <gmd_codeSpace xmlns:gml="http://www.opengis.net/gml" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
                  <gco_CharacterString>test</gco_CharacterString>
                </gmd_codeSpace>
            </gmd_RS_Identifier>
          </gmd_identifier>

What I want to do is change the value of 0000 which is currently in the tag, to the gmd_fileIdentifier character string {0328cb65-b564-495a-b17e-e49e04864ab7}.

I can access the values, using the following python code, but how do I set the value? Current code

import os, sys
from lxml import etree

myXML = r"D:\test.xml"
tree = etree.parse(myXML)
root = tree.getroot()
root.xpath("//gmd_fileIdentifier/gco_CharacterString/text()")
print fileID
code = root.xpath("//gmd_identifier/gmd_RS_Identifier/gmd_code/gco_CharacterString/text()")
print code

Thanks

like image 937
MapMan Avatar asked Nov 14 '13 16:11

MapMan


1 Answers

You'll have to query the whole node instead of only its contents:

code = root.xpath('//gmd_identifier/gmd_RS_Identifier/gmd_code/gco_CharacterString')

Then, if it matched, just replace its text and save back to the XML file:

if code:
    # Replaces <gco_CharacterString> text
    code[0].text = '{0328cb65-b564-495a-b17e-e49e04864ab7}'
    # Save back to the XML file
    etree.ElementTree(root).write('D:\test.xml', pretty_print=True)

That's all! :)

like image 155
Paulo Freitas Avatar answered Oct 28 '22 04:10

Paulo Freitas