mimiller

Help generating an XML file from a python script?

Discussion created by mimiller on May 11, 2012
I have a sizable Python script that geoprocesses a bunch of excel tables, creates File GDB tables, creates point events, applies symbology, adds layers to map document and publishes to a map service.  Now that that script is working, I though it would be nice to generate the xml file (flex viewer) to display the data. I'm using a previously created config.xml file as a template and am stuck on how to go about populating the various operationallayers in the xml file. In the script I've captured the layer names in a dictionary and would like to loop through it and populate the layers of the XML, etc.

I've searched the web and come across a few things that sorta point me in the direction. But being a python newbie I'm kinda lost. Maybe I'm in way over my head.

Any ideas or hints?

Below is the code I've assembled to this point.

import arcpy
import os
arcpy.env.overwriteOutput = 1

mxd = arcpy.mapping.MapDocument(r"\\itdhq1apt50\GIS_Data\Test_MM\Crash_05_10v2.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]

# update counter and create list of map layers
layerList = arcpy.mapping.ListLayers(mxd, "", df)
counter = 0
mapLayerList = {}
for layer in layerList:
    mapLayer = str(layer)
    mapLayerList[counter] = mapLayer
    counter = counter + 1

#print mapLayerList

arcpy.env.workspace = r"C:\inetpub\wwwroot\flexviewers\ScriptCrash_05_10v2"
#arcpy.env.workspace = r"\\itdhq1apt50\GIS_Data\Test_MM\CrashScriptTesting"
fileLoc = arcpy.env.workspace
print fileLoc

from xml.dom.minidom import parseString

doc = parseString("""<xml>
<configuration>
    <title>Fatal Crash Data 2005 - 2010 v2</title>
    <subtitle>IDAHO TRANSPORTATION DEPARTMENT</subtitle>
    <logo>assets/images/ITD_officialLogo_Color2.jpg</logo>
    <style>
        <colors>0x444444,0xEBF4FF,0xEBF4FF,0xDEEBFF,0x444444</colors><!-- SKY BLUE -->
        <alpha>0.9</alpha>
    </style>
    <!-- replace the following url with your own geometryservice -->
    <geometryservice url="http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer" useproxy="false"/>
    <bing key="Aid71RzdYmlhBMs05qwJaMdd9xgExdRsehTEhqud4kpJJSsBB1oKr9_jhvhGmZ3k"/>
    <!-- UI elements -->
    <!-- widgets organized into widget containers that manage close/open etc -->
    <!-- supported layout property options: horizontal(default)|float|vertical|fix-->
    <widgetcontainer layout="horizontal" bottom="220">
        <widget label="Street View" icon="widgets/FrStreetView/assets/images/i_gsv.png" config="widgets/FrStreetView/FrStreetView.xml" url="widgets/FrStreetView/FrStreetView.swf"/>
        <widgetgroup label="Attribute Help" icon="assets/images/i_resources.png">
            <widget label="Crash Data Attribute Help" icon="assets/images/i_help.png" config="widgets/Link/LinkWidget_CrashAttributeHelp.xml" url="widgets/Link/LinkWidget.swf"/>
        </widgetgroup>
    </widgetcontainer>
    <widgetcontainer layout="vertical" top="90">
        <widget label="Crash Time Slider" icon="assets/images/i_clock.png" config="widgets/eTime/eTimeWidget_Crash.xml" url="widgets/eTime/eTimeWidget.swf"/>
        <widget label="Find an address" icon="assets/images/04_maps.png" config="widgets/Locate/LocateWidget_US.xml" url="widgets/Locate/LocateWidget.swf"/>
        <widget label="Draw and Measure" icon="assets/images/i_draw2.png" config="widgets/eDraw/DrawWidget.xml" url="widgets/eDraw/DrawWidget.swf"/>
        <widget label="Print" icon="assets/images/i_print.png" config="widgets/Print/PrintWidget.xml" url="widgets/Print/PrintWidget.swf"/>
        <widget label="Location Bookmarks" icon="assets/images/i_bookmark.png" config="widgets/Bookmark/BookmarkWidget.xml" url="widgets/Bookmark/BookmarkWidget.swf"/>
        <widget label="Map Coordinates" icon="widgets/CoordinateMenu/images/coordinateIcon.png" config="widgets/CoordinateMenu/CoordinateMenuWidget.xml"  url="widgets/CoordinateMenu/CoordinateMenuWidget_GUI.swf"/>
        <widget label="Legend" icon="assets/images/Legend32.png" config="widgets/Legend/LegendWidget_Legend.xml" url="widgets/Legend/LegendWidget.swf"/>
        <widget label="Select Features" icon="assets/images/i_info.png" config="widgets/Selection/SelectionWidget_ITD.xml" url="widgets/Selection/SelectionWidget.swf"/>
        <widget label="Enhanced Search" icon="assets/images/i_search.png" config="widgets/eSearch/SearchWidget_ITD.xml" url="widgets/eSearch/SearchWidget.swf" preload="minimized"/>
        <widget label="Crash Layers" icon="assets/images/i_layers.png" config="widgets/TOC/TOCWidget.xml" url="widgets/TOC/TOCWidget.swf" preload="minimized"/>
        <widget url="widgets/ExportMap/exportMap.swf" config="widgets/ExportMap/exportMap.xml" label="Export to JPG" icon="assets/images/i_map_jpg.png"/>
    </widgetcontainer>
    <!--<widget url="widgets/GoogleAnalytics/GoogleAnalyticsWidget.swf" config="widgets/GoogleAnalytics/GoogleAnalyticsWidget.xml" />-->
    <widget url="widgets/eSearch/SearchWidgetFixedDG.swf" left="0" right="0" bottom="0"/>
    <widget url="widgets/CoordinateMenu/CoordinateMenuWidget.swf" config="widgets/CoordinateMenu/CoordinateMenuWidget.xml" left="0"  top="0"/>
    <widget url="widgets/OverviewMap/OverviewMapWidget.swf" config="widgets/OverviewMap/OverviewMapWidget.xml" right="-2" bottom="-2"/>
    <widget url="widgets/eMapSwitcher/MapSwitcherWidget.swf" config="widgets/eMapSwitcher/MapSwitcherWidget.xml" right="15" top="55"/>
    <widget url="widgets/HeaderController/HeaderControllerWidget.swf" config="widgets/HeaderController/HeaderControllerWidget.xml" left="0" top="0"/>
    <widget url="widgets/Coordinate/CoordinateWidget.swf" config="widgets/Coordinate/CoordinateWidget.xml" label="Coordinate" icon="assets/images/i_globe.png" left="3;" bottom="200"/>
    
    <map esrilogovisible="true" openhandcursorvisible="false" scalebarvisible="true" zoomslidervisible="true" wraparound180="true" center="-12767957.671 5539872.238" scale="6943500" right="0" top="0" bottom="0">
        <basemaps>
            <layer label="Bing Streets" type="bing" alpha="1.0" visible="true" style="road" culture="en-US"/>
            <layer label="Bing Aerial" type="bing" alpha="1.0" visible="false" style="aerial" culture="en-US"/>
            <layer label="Bing Hybrid" type="bing" alpha="1.0" visible="false" style="aerialWithLabels" culture="en-US"/>
            <layer label="ESRI Streets" type="tiled"  alpha="1.0" visible="false" url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"/>
            <layer label="ESRI Topo" type="tiled" alpha="1.0" visible="false" url="http://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer"/>
        </basemaps>
        <operationallayers>
            <layer type="feature" label="Test" url="http://itdhq1apt50/ArcGISTest/rest/services/MM_Test/TestScriptCrash/MapServer/78" visible="true"/>
        </operationallayers>
    </map>

</configuration>
</xml>""")
outFile = fileLoc + os.sep + "config.xml"
print outFile
with open(outFile, "w") as f:
    f.write( doc.toxml() )

Outcomes