---Update 3: I have got the script to update the required data into the xml files completed but the following code is being dropped from the written file. Why is this? how can I replace it?
---更新3:我有脚本将所需数据更新为已完成的xml文件,但以下代码将从写入的文件中删除。为什么是这样?我该怎么替换它?
<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet type='text/xsl' href='ANZMeta.xsl'?>
Current working code (except for issue mentioned above).
当前的工作代码(上述问题除外)。
import os, xml, arcpy, shutil
from xml.etree import ElementTree as et
path=os.getcwd()
arcpy.env.workspace = path
FileList = arcpy.ListFeatureClasses()
FileCount = len(FileList)
zone="_Zone"
for File in FileList:
FileDesc_obj = arcpy.Describe(File)
FileNm=FileDesc_obj.file
newMetaFile=FileNm+"_BaseMetadata.xml"
check_meta=os.listdir(path)
if FileNm+'.xml' in check_meta:
shutil.copy2(FileNm+'.xml', newMetaFile)
else:
shutil.copy2('L:\Data_Admin\QA\Metadata_python_toolset\Master_Metadata.xml', newMetaFile)
tree=et.parse(newMetaFile)
print "Processing: "+str(File)
for node in tree.findall('.//title'):
node.text = str(FileNm)
for node in tree.findall('.//northbc'):
node.text = str(FileDesc_obj.extent.YMax)
for node in tree.findall('.//southbc'):
node.text = str(FileDesc_obj.extent.YMin)
for node in tree.findall('.//westbc'):
node.text = str(FileDesc_obj.extent.XMin)
for node in tree.findall('.//eastbc'):
node.text = str(FileDesc_obj.extent.XMax)
for node in tree.findall('.//native/nondig/formname'):
node.text = str(os.getcwd()+"\\"+File)
for node in tree.findall('.//native/digform/formname'):
node.text = str(FileDesc_obj.featureType)
for node in tree.findall('.//avlform/nondig/formname'):
node.text = str(FileDesc_obj.extension)
for node in tree.findall('.//avlform/digform/formname'):
node.text = str(float(os.path.getsize(File))/int(1024))+" KB"
for node in tree.findall('.//theme'):
node.text = str(FileDesc_obj.spatialReference.name +" ; EPSG: "+str(FileDesc_obj.spatialReference.factoryCode))
print node.text
projection_info=[]
Zone=FileDesc_obj.spatialReference.name
if "GCS" in str(FileDesc_obj.spatialReference.name):
projection_info=[FileDesc_obj.spatialReference.GCSName, FileDesc_obj.spatialReference.angularUnitName, FileDesc_obj.spatialReference.datumName, FileDesc_obj.spatialReference.spheroidName]
print "Geographic Coordinate system"
else:
projection_info=[FileDesc_obj.spatialReference.datumName, FileDesc_obj.spatialReference.spheroidName, FileDesc_obj.spatialReference.angularUnitName, Zone[Zone.rfind(zone)-3:]]
print "Projected Coordinate system"
x=0
for node in tree.findall('.//spdom'):
for node2 in node.findall('.//keyword'):
print node2.text
node2.text = str(projection_info[x])
print node2.text
x=x+1
tree.write(newMetaFile)
---Update 1&2: Thanks to Aleyna I have the following basic code that works
---更新1和2:感谢Aleyna我有以下基本代码可行
import os, xml, arcpy, shutil
from xml.etree import ElementTree as et
CodeString=['northbc','southbc', '<nondig><formname>']
nondig='nondigital'
path=os.getcwd()
arcpy.env.workspace = path
xmlfile = path+"\\test.xml"
FileList = arcpy.ListFeatureClasses()
FileCount = len(FileList)
for File in FileList:
FileDesc_obj = arcpy.Describe(File)
FileNm=FileDesc_obj.file
newMetaFile=FileNm+"_Metadata.xml"
shutil.copy2('L:\Data_Admin\QA\Metadata_python_toolset\Master_Metadata.xml', newMetaFile)
tree=et.parse(newMetaFile)
for node in tree.findall('.//northbc'):
node.text = str(FileDesc_obj.extent.YMax)
for node in tree.findall('.//southbc'):
node.text = str(FileDesc_obj.extent.YMin)
for node in tree.findall('.//westbc'):
node.text = str(FileDesc_obj.extent.XMin)
for node in tree.findall('.//eastbc'):
node.text = str(FileDesc_obj.extent.XMax)
for node in tree.findall('.//native/nondig/formname'):
node.text = nondig
tree.write(newMetaFile)
The issue is with dealing with xml code like
问题在于处理像xml这样的代码
- <spdom>
<keyword thesaurus="">GDA94</keyword>
<keyword thesaurus="">GRS80</keyword>
<keyword thesaurus="">Transverse Mercator</keyword>
<keyword thesaurus="">Zone 55 (144E - 150E)</keyword>
</spdom>
As keyword thes...is not unique within the <spdom>
can we update these in a order from the values coming from
由于关键字thes ...在
FileDesc_obj.spatialReference.name
u'GCS_GDA_1994'
---ORIGINAL POST---
I am building up a program to generate xml metadata files from spatial files in our library. I have already created the scripts to extract the required spatial and attrib data from the files and create a shp and text file based index of the files but now I want to write this info to base metadata xml file that is written to anzlic standards by replacing the values held by common/static elements...
我正在构建一个程序来从我们库中的空间文件生成xml元数据文件。我已经创建了脚本来从文件中提取所需的空间和属性数据,并创建基于shp和文本文件的文件索引,但现在我想将此信息写入基本元数据xml文件,通过替换写入anzlic标准共同/静态元素所持有的价值......
So for example I want to replace the following xml code
所以例如我想替换下面的xml代码
<northbc>8097970</northbc>
<southbc>8078568</southbc>
with
<northbc> GeneratedValue_[desc.extent.XMax] /<northbc>
<southbc> GeneratedValue_[desc.extent.XMax] </southbc>
The issue is that obviously the number/value between and will not be the same.
问题是,显然数字/值之间和之间的数字/值不一样。
Similarly for xml tags like <title>, <nondig><formname>
etc...in the latter example both tags must be searched for together as formname appears multiple times (is not unique).
类似地,对于xml标签,例如
I am using the Python Regular Expression manual [here][1],
我正在使用Python正则表达式手册[这里] [1],
3 个解决方案
#1
2
Using the given tag(s) above:
使用上面给定的标签:
import os
import xml
from xml.etree import ElementTree as et
path = r"/your/path/to/xml.file"
tree = et.parse(path)
for node in tree.findall('.//northbc'):
node.text = "New Value"
tree.write(path)
Here, XPATH .//northbc returns all the 'northbc' nodes in the XML doc. You can tailor the code for your need easily.
这里,XPATH .//northbc返回XML文档中的所有“northbc”节点。您可以轻松地根据需要定制代码。
#2
1
If you're dealing with valid XML, use XPath to find the nodes of interest and the ElementTree api to manipulate the node.
如果您正在处理有效的XML,请使用XPath查找感兴趣的节点,使用ElementTree api来操作节点。
For instance, your xpath might be something like '//northbc' and you would just replace the text node inside it.
例如,您的xpath可能类似于“// northbc”,您只需替换其中的文本节点即可。
See http://docs.python.org/library/xml.etree.elementtree.html as well as http://pypi.python.org/pypi/lxml/2.2.8 for two different libraries that will help you get this done. Search google for XPath and see the w3c tutorial for a decent intro to XPath (I apparently can't post more than two links in a post or I'd link it too)
有关两个不同的库,请参阅http://docs.python.org/library/xml.etree.elementtree.html以及http://pypi.python.org/pypi/lxml/2.2.8。完成。搜索google for XPath并查看w3c教程以获得一个体面的XPath介绍(我显然不能在帖子中发布超过两个链接或我也链接它)
#3
0
I might be stating the obvious here, but did you consider using a DOM tree to parse and manipulate your XML?
我可能会在这里说明显而易见的,但您是否考虑过使用DOM树来解析和操作XML?
#1
2
Using the given tag(s) above:
使用上面给定的标签:
import os
import xml
from xml.etree import ElementTree as et
path = r"/your/path/to/xml.file"
tree = et.parse(path)
for node in tree.findall('.//northbc'):
node.text = "New Value"
tree.write(path)
Here, XPATH .//northbc returns all the 'northbc' nodes in the XML doc. You can tailor the code for your need easily.
这里,XPATH .//northbc返回XML文档中的所有“northbc”节点。您可以轻松地根据需要定制代码。
#2
1
If you're dealing with valid XML, use XPath to find the nodes of interest and the ElementTree api to manipulate the node.
如果您正在处理有效的XML,请使用XPath查找感兴趣的节点,使用ElementTree api来操作节点。
For instance, your xpath might be something like '//northbc' and you would just replace the text node inside it.
例如,您的xpath可能类似于“// northbc”,您只需替换其中的文本节点即可。
See http://docs.python.org/library/xml.etree.elementtree.html as well as http://pypi.python.org/pypi/lxml/2.2.8 for two different libraries that will help you get this done. Search google for XPath and see the w3c tutorial for a decent intro to XPath (I apparently can't post more than two links in a post or I'd link it too)
有关两个不同的库,请参阅http://docs.python.org/library/xml.etree.elementtree.html以及http://pypi.python.org/pypi/lxml/2.2.8。完成。搜索google for XPath并查看w3c教程以获得一个体面的XPath介绍(我显然不能在帖子中发布超过两个链接或我也链接它)
#3
0
I might be stating the obvious here, but did you consider using a DOM tree to parse and manipulate your XML?
我可能会在这里说明显而易见的,但您是否考虑过使用DOM树来解析和操作XML?