0

Updating multiple field attributes through Python in an XML metadata doc export. There are multiple fields, so the XML tags for each unique field are all the same:

      <attr>
        <attrlabl>STATE_NAME</attrlabl>
        <attrdef>State Name</attrdef>
        <attrdefs>HRSA GDW</attrdefs>
        <attrdomv>
          <udom>2010</udom>
        </attrdomv>
      </attr>
      <attr>
        <attrlabl>County</attrlabl>
        <attrdef>County Name</attrdef>
        <attrdefs>HRSA GDW</attrdefs>
        <attrdomv>
          <udom>2010</udom>
        </attrdomv>
      </attr>
      <attr>
        <attrlabl>STCOFIPS</attrlabl>
        <attrdef>State/County FIPS ID</attrdef>
        <attrdefs>HRSA GDW</attrdefs>
        <attrdomv>
          <udom>2010</udom>
        </attrdomv>
      </attr>

How do I call each unique field out in a python script to update the metadata in an XML. This is where I am:

import arcpy, sys  
from xml.etree.ElementTree import ElementTree  
from xml.etree.ElementTree import Element, SubElement  
from arcpy import env  
env.overwriteOutput = True  
fcpath = r""
translatorpath = r"...\Metadata\Translator\ARCGIS2FGDC.xml"
xmlfile = r"...\FullMetaFC.xml"
arcpy.ExportMetadata_conversion(fcpath, translatorpath, xmlfile)

tree = ElementTree()
tree.parse(xmlfile)

ele = tree.find("eainfo/detailed")
for e in ele.iter("attrlabl"):
    # loop through children pulling out nodes which have a attrlabl tag
    print e.text
    # do some sort if IF THEN logic check and update as necessary
    if e.text == "STATE_NAME":
        ele.iter("attrdefs") = "NAME OF THE STATE"

tree.write(xmlfile)

closed as off-topic by Hornbydd, Stev_k, Dan C, nmtoken, Jochen Schwarze Jul 26 '18 at 12:53

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions relating to general IT or with no clear GIS component, are off-topic here but can be researched/asked at Stack Overflow (software development), Super User (computing hardware and software), Database Administrators (relational databases) and other SE sites" – Hornbydd, Stev_k, Dan C, nmtoken, Jochen Schwarze
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Looks like you need to call the save method on tree to commit the change. Search python help on XML to understand the syntax used. – Hornbydd Jul 25 '18 at 22:36
  • I have added in the save method, but it's still not committing the change to the field attributes. I tested with other elements and those fields update just fine. I suspect the issue is with the IF/THEN statement, but can't figure it out without an error – Dave Jul 26 '18 at 12:08
  • I have updated my answer, your were traversing the tree structure a level too low for for iter() to pick up the correct level. – Hornbydd Jul 26 '18 at 14:08
  • Added the code updates to the script and it produces an error that: Runtime error: AttributeError: 'NoneType' object has no attribute 'text' – Dave Jul 26 '18 at 14:33
  • Check your spelling if NoneType is returned. – Hornbydd Jul 26 '18 at 14:34
2

Although your XML is derived from a spatial dataset metadata, this question is not really about GIS but about traversing an XML file. That said I think you need to grab the Element one level higher so you have a branch of it with all your child nodes then step through those "children" doing some sort of if/then logic check and updating as necessary.

Generic Example

import arcpy, sys
from xml.etree.ElementTree import ElementTree
from xml.etree.ElementTree import Element, SubElement
xmlfile = r"C:\Scratch\aaa.xml"
tree = ElementTree()
tree.parse(xmlfile)

ele = tree.find("eainfo/detailed")
for e in ele.iter("attrlabl"):
    # loop through children pulling out nodes which have a attrlabl tag
    print e.text
    # do some sort if IF THEN logic check and update as necessary

Specific Example using your data

import arcpy, sys
from xml.etree.ElementTree import ElementTree
from xml.etree.ElementTree import Element, SubElement
xmlfile = r"C:\Scratch\aaa.xml"
tree = ElementTree()
tree.parse(xmlfile)
ele = tree.find("eainfo/detailed")
for e in ele.iter("attr"):

    # do some sort if IF THEN logic check and update as necessary
    if e.find("attrlabl").text == "STATE_NAME":
        e.find("attrdefs").text = "NAME OF THE STATE"

tree.write(xmlfile)
  • Adding your helper code along with an IF/THEN doesn't produce any errors nor does it update the text: if e.text == "STATE_NAME": ele.iter("attrdefs") = "NAME OF THE STATE" – Dave Jul 25 '18 at 17:12
  • I was providing you a pointer to how to navigate the xlm dom. You have to save your changes. If you have amended your code and using save then suggest you edit your question and show the additional code? – Hornbydd Jul 25 '18 at 18:01
  • Added the amended part of the code – Dave Jul 25 '18 at 19:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.