7

I need the name of each layer in an .msd, but I could not find support using arcpy (ArcGIS 10.0). Is there a method I missed?

In the current workflow, since I am publishing the document, I use the REST service:

descriptionUrl = url + "/ArcGIS/rest/services/" + mapServer + "/MapServer?f=json"  
restResponseFp = urllib.urlopen(descriptionUrl)
mapDescription =json.load(restResponseFp)
layerNames = [str(layer["name"]) for layer in mapDescription["layers"]]
PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Michael Jackson
  • 678
  • 4
  • 12

1 Answers1

7
import zipfile
from xml.etree.cElementTree import iterparse

zz = zipfile.ZipFile('C:\\Temp\\Untitled.msd')

serviceNames = []
for name in zz.namelist():
    if name == "DocumentInfo.xml" or name == "layers/layers.xml":
        pass
    else:
        for _event, elem in iterparse(zz.open(name)):
            if elem.tag == "Name":
                serviceNames.append(elem.text)
                break
zz.close()

Files associated with layers are named like this: layers/featureclassname.xml

Long service names are truncated to make the file name, so we must parse the file contents to handle all cases.

Michael Jackson
  • 678
  • 4
  • 12
theJones
  • 561
  • 2
  • 9
  • Thanks! Actually, I notice that since my service names are very long, the xml file name is truncated. Each file associated with a layer must be parsed for the "Name" element to reliably handle long service names. – Michael Jackson Nov 01 '11 at 05:28
  • @theJones is there any way to get the service from the DocumentInfo.xml? Do you know any property that can print the service name? – Gurminder Bharani Aug 17 '17 at 23:04