jjzjj

Python 2.6 xml.dom.ext 对象丢失?

coder 2024-06-27 原文

我正在使用我在网上找到的脚本通过解析一些 XML 来转换一些文件。该脚本是在 Python 2.6 中构建的,并且它使用的模块我认为通过我在网上阅读的内容不属于 2.6。我想知道是否有解决方法。我得到的错误是:

No Module name EXT

在下面的脚本中,我认为它在 import xml.dom.ext 上挂了,它似乎只在 PrettyPrint 的最后使用了这个对象(见最后的 Try 语句)我想知道 2.6 中是否有解决方法?我似乎找不到包含我可以导入的 EXT 对象的模块。

脚本是:

from xml.dom.minidom import Document
import xml.dom.ext
import string
import os
import arcpy

#Read input parameters from GP dialog
output = arcpy.GetParameterAsText(0)

#Create an output qgs file
f = open(output, "w")

# Create the minidom
doc = Document()

# Create the <qgis> base element
qgis = doc.createElement("qgis")
qgis.setAttribute("projectname", " ")
qgis.setAttribute("version", "1.6.0-Capiapo")
doc.appendChild(qgis)

# Create the <title> element
title = doc.createElement("title")
qgis.appendChild(title)

# Assign current document
mxd = arcpy.mapping.MapDocument("CURRENT")

print 'Converting mxd........'

# Dataframe elements

df = arcpy.mapping.ListDataFrames(mxd)[0]
unit = doc.createTextNode(df.mapUnits)
xmin1 = doc.createTextNode(str(df.extent.XMin))
ymin1 = doc.createTextNode(str(df.extent.YMin))
xmax1 = doc.createTextNode(str(df.extent.XMax))
ymax1 = doc.createTextNode(str(df.extent.YMax))
# srsid = doc.createTextNode
srid1 = doc.createTextNode(str(df.spatialReference.factoryCode))
srid2 = doc.createTextNode(str(df.spatialReference.factoryCode))
epsg1 = doc.createTextNode(str(df.spatialReference.factoryCode))
epsg2 = doc.createTextNode(str(df.spatialReference.factoryCode))
description1 = doc.createTextNode(str(df.spatialReference.name))
description2 = doc.createTextNode(str(df.spatialReference.name))
ellipsoidacronym1 = doc.createTextNode(str(df.spatialReference.name))
ellipsoidacronym2 = doc.createTextNode(str(df.spatialReference.name))
geographicflag1 = doc.createTextNode("true")
geographicflag2 = doc.createTextNode("true")

authid2 = doc.createTextNode("EPSG:"+str(df.spatialReference.factoryCode))
authid3 = doc.createTextNode("EPSG:"+str(df.spatialReference.factoryCode))

# Layerlist elements
lyrlist = arcpy.mapping.ListLayers(df)
count1 = str(len(lyrlist))

# mapcanvas
def map_canvas():
    # Create the <mapcanvas> element
    mapcanvas = doc.createElement("mapcanvas")
    qgis.appendChild(mapcanvas)

    # Create the <units> element
    units = doc.createElement("units")
    units.appendChild(unit)
    mapcanvas.appendChild(units)


    # Create the <extent> element
    extent = doc.createElement("extent")
    mapcanvas.appendChild(extent)

    # Create the <xmin> element
    xmin = doc.createElement("xmin")
    xmin.appendChild(xmin1)
    extent.appendChild(xmin)


    # Create the <ymin> element
    ymin = doc.createElement("ymin")
    ymin.appendChild(ymin1)
    extent.appendChild(ymin)


    # Create the <xmax> element
    xmax = doc.createElement("xmax")
    xmax.appendChild(xmax1)
    extent.appendChild(xmax)


    # Create the <ymax> element
    ymax = doc.createElement("ymax")
    ymax.appendChild(ymax1)
    extent.appendChild(ymax)


    # Create the <projections> element
    projections = doc.createElement("projections")
    mapcanvas.appendChild(projections)

    # Create the <destinationsrs> element
    destinationsrs = doc.createElement("destinationsrs")
    mapcanvas.appendChild(destinationsrs)

    # Create the <spatialrefsys> element
    spatialrefsys = doc.createElement("spatialrefsys")
    destinationsrs.appendChild(spatialrefsys)

    # Create the <proj4> element
    proj4 = doc.createElement("proj4")
    spatialrefsys.appendChild(proj4)

    # Create the <srsid> element
    srsid = doc.createElement("srsid")
    spatialrefsys.appendChild(srsid)

    # Create the <srid> element
    srid = doc.createElement("srid")
    srid.appendChild(srid1)
    spatialrefsys.appendChild(srid)



    # Create the <authid> element
    authid = doc.createElement("authid")
    authid.appendChild(authid2)
    spatialrefsys.appendChild(authid)


    # Create the <description> element
    description = doc.createElement("description")
    description.appendChild(description1)
    spatialrefsys.appendChild(description)


    # Create the <projectionacronym> element
    projectionacronym = doc.createElement("projectionacronym")
    spatialrefsys.appendChild(projectionacronym)

    # Create the <ellipsoidacronym element
    ellipsoidacronym = doc.createElement("ellipsoidacronym")
    ellipsoidacronym.appendChild(ellipsoidacronym1)
    spatialrefsys.appendChild(ellipsoidacronym)



    # Create the <geographicflag> element
    geographicflag = doc.createElement("geographicflag")
    geographicflag.appendChild(geographicflag1)
    spatialrefsys.appendChild(geographicflag)




# Legend
def legend_func():

    # Create the <legend> element
    legend = doc.createElement("legend")
    qgis.appendChild(legend)

    for lyr in lyrlist:
        if(lyr.isGroupLayer == False):

            # Create the <legendlayer> element
            legendlayer = doc.createElement("legendlayer")
            legendlayer.setAttribute("open", "true")
            legendlayer.setAttribute("checked", "Qt::Checked")
            legendlayer.setAttribute("name",str(lyr.name))

            legend.appendChild(legendlayer)

            # Create the <filegroup> element
            filegroup = doc.createElement("filegroup")
            filegroup.setAttribute("open", "true")
            filegroup.setAttribute("hidden", "false")
            legendlayer.appendChild(filegroup)

            # Create the <legendlayerfile> element
            legendlayerfile = doc.createElement("legendlayerfile")
            legendlayerfile.setAttribute("isInOverview", "0")
            legendlayerfile.setAttribute("layerid", str(lyr.name)+str(20110427170816078))
            legendlayerfile.setAttribute("visible", "1")
            filegroup.appendChild(legendlayerfile)

# Project Layers
def project_layers():

    # Create the <projectlayers> element
    projectlayers = doc.createElement("projectlayers")
    projectlayers.setAttribute("layercount", count1)
    qgis.appendChild(projectlayers)

    for lyr in lyrlist:

        if(lyr.isGroupLayer == False and lyr.isRasterLayer == False):
            geometry1 = arcpy.Describe(lyr)
            geometry2 = str(geometry1.shapeType)
            ds = doc.createTextNode(str(lyr.dataSource))

            name1 = doc.createTextNode(str(lyr.name)+str(20110427170816078))
            name2 = doc.createTextNode(str(lyr.name))


           # Create the <maplayer> element
            maplayer = doc.createElement("maplayer")
            maplayer.setAttribute("minimumScale", "0")
            maplayer.setAttribute("maximumScale", "1e+08")
            maplayer.setAttribute("minLabelScale", "0")
            maplayer.setAttribute("maxLabelScale", "1e+08")
            maplayer.setAttribute("geometry", geometry2)
            if(lyr.isRasterLayer == True):
                maplayer.setAttribute("type", "raster")
            else:
                maplayer.setAttribute("type", "vector")
            maplayer.setAttribute("hasScaleBasedVisibilityFlag", "0")
            maplayer.setAttribute("scaleBasedLabelVisibilityFlag", "0")
            projectlayers.appendChild(maplayer)

            # Create the <id> element
            id = doc.createElement("id")
            id.appendChild(name1)
            maplayer.appendChild(id)

            # Create the <datasource> element
            datasource = doc.createElement("datasource")
            datasource.appendChild(ds)
            maplayer.appendChild(datasource)

            # Create the <layername> element
            layername = doc.createElement("layername")
            layername.appendChild(name2)
            maplayer.appendChild(layername)

            # Create the <srs> element
            srs = doc.createElement("srs")
            maplayer.appendChild(srs)

            # Create the <spatialrefsys> element
            spatialrefsys = doc.createElement("spatialrefsys")
            srs.appendChild(spatialrefsys)

            # Create the <proj4> element
            proj4 = doc.createElement("proj4")
            spatialrefsys.appendChild(proj4)

            # Create the <srsid> element
            srsid = doc.createElement("srsid")
            spatialrefsys.appendChild(srsid)

            # Create the <srid> element
            srid = doc.createElement("srid")
            srid.appendChild(srid2)
            spatialrefsys.appendChild(srid)


            # Create the <authid> element
            authid = doc.createElement("authid")
            authid.appendChild(authid3)
            spatialrefsys.appendChild(authid)


            # Create the <description> element
            description = doc.createElement("description")
            description.appendChild(description2)
            spatialrefsys.appendChild(description)


            # Create the <projectionacronym> element
            projectionacronym = doc.createElement("projectionacronym")
            spatialrefsys.appendChild(projectionacronym)

            # Create the <ellipsoidacronym element
            ellipsoidacronym = doc.createElement("ellipsoidacronym")
            ellipsoidacronym.appendChild(ellipsoidacronym2)
            spatialrefsys.appendChild(ellipsoidacronym)


            # Create the <geographicflag> element
            geographicflag = doc.createElement("geographicflag")
            geographicflag.appendChild(geographicflag2)
            spatialrefsys.appendChild(geographicflag)

            # Create the <transparencyLevelInt> element
            transparencyLevelInt = doc.createElement("transparencyLevelInt")
            transparency2 = doc.createTextNode("255")
            transparencyLevelInt.appendChild(transparency2)
            maplayer.appendChild(transparencyLevelInt)

            # Create the <customproperties> element
            customproperties = doc.createElement("customproperties")
            maplayer.appendChild(customproperties)

            # Create the <provider> element
            provider = doc.createElement("provider")
            provider.setAttribute("encoding", "System")
            ogr = doc.createTextNode("ogr")
            provider.appendChild(ogr)
            maplayer.appendChild(provider)

            # Create the <singlesymbol> element
            singlesymbol = doc.createElement("singlesymbol")
            maplayer.appendChild(singlesymbol)

            # Create the <symbol> element
            symbol = doc.createElement("symbol")
            singlesymbol.appendChild(symbol)

            # Create the <lowervalue> element
            lowervalue = doc.createElement("lowervalue")
            symbol.appendChild(lowervalue)

            # Create the <uppervalue> element
            uppervalue = doc.createElement("uppervalue")
            symbol.appendChild(uppervalue)

            # Create the <label> element
            label = doc.createElement("label")
            symbol.appendChild(label)

            # Create the <rotationclassificationfieldname> element
            rotationclassificationfieldname = doc.createElement("rotationclassificationfieldname")
            symbol.appendChild(rotationclassificationfieldname)

            # Create the <scaleclassificationfieldname> element
            scaleclassificationfieldname = doc.createElement("scaleclassificationfieldname")
            symbol.appendChild(scaleclassificationfieldname)

            # Create the <symbolfieldname> element
            symbolfieldname = doc.createElement("symbolfieldname")
            symbol.appendChild(symbolfieldname)

             # Create the <outlinecolor> element
            outlinecolor = doc.createElement("outlinecolor")
            outlinecolor.setAttribute("red", "88")
            outlinecolor.setAttribute("blue", "99")
            outlinecolor.setAttribute("green", "37")
            symbol.appendChild(outlinecolor)

             # Create the <outlinestyle> element
            outlinestyle = doc.createElement("outlinestyle")
            outline = doc.createTextNode("SolidLine")
            outlinestyle.appendChild(outline)
            symbol.appendChild(outlinestyle)

             # Create the <outlinewidth> element
            outlinewidth = doc.createElement("outlinewidth")
            width = doc.createTextNode("0.26")
            outlinewidth.appendChild(width)
            symbol.appendChild(outlinewidth)

             # Create the <fillcolor> element
            fillcolor = doc.createElement("fillcolor")
            fillcolor.setAttribute("red", "90")
            fillcolor.setAttribute("blue", "210")
            fillcolor.setAttribute("green", "229")
            symbol.appendChild(fillcolor)

             # Create the <fillpattern> element
            fillpattern = doc.createElement("fillpattern")
            fill = doc.createTextNode("SolidPattern")
            fillpattern.appendChild(fill)
            symbol.appendChild(fillpattern)

             # Create the <texturepath> element
            texturepath = doc.createElement("texturepath")
            texturepath.setAttribute("null", "1")
            symbol.appendChild(texturepath)



map_canvas()
legend_func()
project_layers()


#  Write to qgis file

try:
    xml.dom.ext.PrettyPrint(doc, f)
finally:
    f.close()

print 'Done'

最佳答案

xml.dom.ext 模块从未添加到 Python 标准库中。

它只是 PyXML distribution 的一部分,但多年来没有看到任何更新,我怀疑它是否仍能在 Python 2.6 上运行。

相反,只需在您的文档上调用 minidom .toprettyxml() 方法来漂亮地打印输出,然后将该数据写入文件:

f.write(doc.toprettyxml())

关于Python 2.6 xml.dom.ext 对象丢失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15299683/

有关Python 2.6 xml.dom.ext 对象丢失?的更多相关文章

  1. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  2. python - 如何使用 Ruby 或 Python 创建一系列高音调和低音调的蜂鸣声? - 2

    关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。

  3. ruby-on-rails - 按天对 Mongoid 对象进行分组 - 2

    在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev

  4. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  5. ruby-on-rails - 如何验证非模型(甚至非对象)字段 - 2

    我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss

  6. Ruby 写入和读取对象到文件 - 2

    好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信

  7. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  8. ruby-on-rails - 未在 Ruby 中初始化的对象 - 2

    我在Rails工作并有以下类(class):classPlayer当我运行时bundleexecrailsconsole然后尝试:a=Player.new("me",5.0,"UCLA")我回来了:=>#我不知道为什么Player对象不会在这里初始化。关于可能导致此问题的操作/解释的任何建议?谢谢,马里奥格 最佳答案 havenoideawhythePlayerobjectwouldn'tbeinitializedhere它没有初始化很简单,因为你还没有初始化它!您已经覆盖了ActiveRecord::Base初始化方法,但您没有调

  9. ruby - 如何在 Rails 4 中使用表单对象之前的验证回调? - 2

    我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser

  10. ruby - 一个 YAML 对象可以引用另一个吗? - 2

    我想让一个yaml对象引用另一个,如下所示:intro:"Hello,dearuser."registration:$introThanksforregistering!new_message:$introYouhaveanewmessage!上面的语法只是它如何工作的一个例子(这也是它在thiscpanmodule中的工作方式。)我正在使用标准的ruby​​yaml解析器。这可能吗? 最佳答案 一些yaml对象确实引用了其他对象:irb>require'yaml'#=>trueirb>str="hello"#=>"hello"ir

随机推荐