Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I need to generate xml of attribute data of a feature, but I have only Arcview license so, I can't use Export XML tool. What are my options?

What is wrong with this code (after applying search cursor writing the attributes of node):

 for row in rows:
  order.attrib['name'] = row.getValue('Id')
  order.attrib['UTM'] = row.getValue('UTM')

I am not able to see any addition to order tag. missing something... Thanks

Ok - just realized there was some issue with the feature: Here is the code and error message after the change (So something is broken fundamentally):

>>> import xml.etree.ElementTree as xml
>>> import arcpy
>>> fclass= arcpy.SearchCursor(r"..\SiteMarker_Project")
>>> order = xml.Element('Order')
>>> site = xml.Element('Site')
>>> order.append(site)
>>> rows = fclass
>>> for row in rows:
  order.attrib['name'] = row.getValue('Id')
  order.attrib['UTM'] = row.getValue('UTM')

Traceback (most recent call last):
  File "<pyshell#8>", line 3, in <module>
    order.attrib['UTM'] = row.getValue('UTM')
  File "C:\Program Files\ArcGIS\Desktop10.1\arcpy\arcpy\arcobjects\arcobjects.py", line 1011, in getValue
    **return convertArcObjectToPythonObject(self._arc_object.GetValue(*gp_fixargs(args)))
RuntimeError: ERROR 999999: Error executing function.**

Thanks

share|improve this question
    
is exporting to csv or txt an option? –  Mapperz Aug 7 '12 at 21:21
    
I can check it! probably after writing text, I can use another script. –  Deep Aug 8 '12 at 13:19
    
I can use xml.etree.ElementTree or other Python libraries to write XML. The key is to how to interact with feature data. –  Deep Aug 9 '12 at 20:01
1  
So the problem isn't with the writing of the XML then, but with extracting the feature data to push to XML? If so, then show us some more of the code you are using to get the feature data out with your searchcursor. –  Chad Cooper Aug 16 '12 at 16:31
    
After editing the post, I realized there was a problem with my feature and there was no values under cursor (It was stored as table instead of feature; not sure if that causes it ). After modification, I am getting the error message mentioned above! So it boils down to fundamental. –  Deep Aug 16 '12 at 16:52

1 Answer 1

up vote 1 down vote accepted

Give this a shot:

import xml.etree.ElementTree as xml
import arcpy
rows = arcpy.SearchCursor(r"<try full path to>\SiteMarker_Project")
for row in rows:
    print row.getValue('Id')
    print row.getValue('UTM')

That should help you out with getting your data out of the table. Not sure about the way you are setting your Elements, though.

Here is a snippet of how I write XML that I pull from a featureclass. I put all the attribute info from the featureclass into a dictionary object (params in this case), which looks something like this:

{'kop_array': [[1, -253.92773828679765, 2437.0978505349062, 156.07947108761041]], 
 'project_dir': 'DEV',
 'project_name': 'DEV',  
 'wtg_array': [[1, 6068.6743197736623, 5592.7541754087933], 
              [2, 5134.2411379873774, 6330.8755526905206]]}

kop_array and wtg_array are both lists of attributes from a featureclass. Each list element in the next list is a record from the attribute table, a feature.

I then pull those out by key name and assign them to XML elements, then write the XML out to a file:

class VueXmlWriter(object):
    def __init__(self):
        pass

    def prettify(self, elem):
        """Return a pretty-printed XML string for the element"""
        rough_string = xml.etree.ElementTree.tostring(elem, 'utf-8')
        reparsed = minidom.parseString(rough_string)
        return reparsed.toprettyxml(indent="  ")    

    def create_config_xml(self, path, name, params):
        """Write out elements to XML config file"""
        viesore_element = xml.etree.ElementTree.Element("VIESORE.Configuration")     
        project_element = xml.etree.ElementTree.SubElement(viesore_element, "ProjectDirectory")
        project_element.text = params["project_dir"]
        project_element = xml.etree.ElementTree.SubElement(viesore_element, "ProjectName")
        project_element.text = params["project_name"]
        scene_element = xml.etree.ElementTree.SubElement(viesore_element, "SceneFile")
    # WTG elements
    wtg_array_element = xml.etree.ElementTree.SubElement(viesore_element, "WTGArray")
    wtgs = params["wtg_array"]
    for wtg in wtgs:
        wtg_element = xml.etree.ElementTree.SubElement(wtg_array_element, "WTG")
        wtg_id_element =  xml.etree.ElementTree.SubElement(wtg_element, "WTGID")
        wtg_id_element.text = str(wtg[0])            
        wtg_vue_n_element =  xml.etree.ElementTree.SubElement(wtg_element, "VueNorthing")
        wtg_vue_n_element.text = str(wtg[1])            
        wtg_vue_e_element = xml.etree.ElementTree.SubElement(wtg_element, "VueEasting")
        wtg_vue_e_element.text = str(wtg[2])            
    # Get rid of extra line returns in prettified XML
    uglyXml = self.prettify(viesore_element)
    text_re = re.compile('>\n\s+([^<>\s].*?)\n\s+</', re.DOTALL)    
    prettyXml = text_re.sub('>\g<1></', uglyXml)
    f = open(os.path.join(path, name + ".xml"), "w")
    f.write(prettyXml)
    f.close()

So in the case of the WTG elements, I just pull out that value for the wtg_array key and iterate though the list, putting each one into its own subelement, which gives me something along the lines of this as my XML:

<?xml version="1.0" ?>
<VIESORE.Configuration>
  <ProjectDirectory>DEV</ProjectDirectory>
  <ProjectName>DEV</ProjectName>
  <WTGArray>
    <WTG>
      <WTGID>1</WTGID>
      <VueNorthing>6068.67431977</VueNorthing>
      <VueEasting>5592.75417541</VueEasting>
    </WTG>
    <WTG>
      <WTGID>2</WTGID>
      <VueNorthing>5134.24113799</VueNorthing>
      <VueEasting>6330.87555269</VueEasting>
    </WTG>
  </WTGArray>
</VIESORE.Configuration>

Where each <WTG></WTG> is a feature from the featureclass.

share|improve this answer
    
I have tried this. That's how I came to know it was empty. UTM value is the output of calculate UTM tool- not sure if that's the reason of 'convertArcObjectToPythonObject' error. I did not see the error message after removing UTM line but somehow other elements are not getting populated either. –  Deep Aug 17 '12 at 13:25
    
OK there was some issue with the data type too. Finally I can see the values but XML is not getting iterated and all I got is last record. –  Deep Aug 17 '12 at 19:58
    
@user8732 - see my edit on writing out XML –  Chad Cooper Aug 17 '12 at 22:02
    
Hi Chad, Thanks for the reply. Can you elaborate on Params to pass the feature attributes? I have thought to pass attributes of two different features under two subelements. <Order_Detail> <Order_ID>12345</Order_ID> <Site> <Lat_Y>44.2</Lat_Y> <Lat_X>-79.1</Lat_X> </Site> <Site> <Lat_Y>43.9908<Lat_Y> <Lat_X>-79.1333<Lat_X> </Site> </Order_Detail> So parsing objects to site is where I am struggling right now. –  Deep Aug 20 '12 at 17:33
    
What I am not sure how this params will store all the values and delimiters. –  Deep Aug 20 '12 at 18:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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