我知道有几个问题已经回答了这个主题,但由于某种原因,我无法在阅读文件时使其工作。
这是我的代码
~/.vimrc
这是原始的xml文件结构
import xml.etree.ElementTree as etree
import copy
etree.register_namespace("","http://www.w3.org/2001/XMLSchema")
etree.register_namespace("","http://www.w3.org/2001/XMLSchema-instance")
#Estract the search set thats necesary
tree=etree.ElementTree()
tree.parse('Selection Sets.xml')
root = tree.getroot()
#QTO with one set name and properties as needed, same properties will be applied to the new elements
itree=etree.ElementTree()
itree.parse('New QTO one set.xml')
iroot=itree.getroot()
print (etree.tostring(iroot))
#----Extract the names to be used in the new sets
catcher=[]
temp=tree.findall('selectionsets/selectionset')
for child in temp:
catcher.append(str(child.get("name")))
# print (catcher)
#----create new elements inside the QTO xml
'''itemp=iroot.findall("Takeoff/Catalog")
for isets in itemp:
for icatch in catcher:
col = copy.deepcopy(isets)
col.set('Name','%s'%(icatch))
iroot.find('Takeoff/Catalog/Item').append(col)'''
print (catcher[0])
otree = etree.ElementTree(iroot)
otree.write('Set QTO.xml')
到目前为止的部分结果
<?xml version="1.0" encoding="utf-8"?>
<Takeoff xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://download.autodesk.com/us/navisworks/schemas/nw-TakeoffCatalog-10.0.xsd">
<Catalog>
<Item Name="Column Location D" WBS="1" Transparency="0.3" Color="-3887691" LineThickness="0.1" CatalogId="f9f30bfc-6c43-4b7b-a335-5c5c093b4165">
<VariableCollection>
<Variable Name="Length" Formula="=ModelLength" Units="Meter" />
<Variable Name="Width" Formula="=ModelWidth" Units="Meter" />
<Variable Name="Thickness" Formula="=ModelThickness" Units="Meter" />
<Variable Name="Height" Formula="=ModelHeight" Units="Meter" />
<Variable Name="Perimeter" Formula="=ModelPerimeter" Units="Meter" />
<Variable Name="Area" Formula="=ModelArea" Units="SquareMeter" />
<Variable Name="Volume" Formula="=ModelVolume" Units="CubicMeter" />
<Variable Name="Weight" Formula="=ModelWeight" Units="Kilogram" />
<Variable Name="Count" Formula="=1" />
</VariableCollection>
</Item>
</Catalog>
<ConfigFile>
<Workbook xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://download.autodesk.com/us/navisworks/schemas/nw-TakeoffConfiguration-10.0.xsd" xmlns="">
<GlobalConfiguration>
<ConfigureFileType>ItemResources</ConfigureFileType>
<UnitsSystem>Metric</UnitsSystem>
<Currency>
<Name></Name>
<Code></Code>
<Symbol></Symbol>
</Currency>
<Column Name="Object">
<Type>String</Type>
<Purpose>Input</Purpose>
<Formula varies="0"></Formula>
<Value varies="1"></Value>
<Units varies="0" group="any"></Units>
</Column>
<Column Name="Description1">
<Type>String</Type>
<Purpose>Input</Purpose>
<Formula varies="0"></Formula>
<Value varies="1"></Value>
<Units varies="0" group="any"></Units>
</Column>
<Column Name="Description2">
<Type>String</Type>
<Purpose>Input</Purpose>
<Formula varies="0"></Formula>
<Value varies="1"></Value>
<Units varies="0" group="any"></Units>
</Column>
<Column Name="ModelLength">
<Type>Number</Type>
<Purpose>Input</Purpose>
<Formula varies="0"></Formula>
<Value varies="1"></Value>
<Units varies="0" group="length">meter</Units>
</Column>
<Column Name="ModelWidth">
<Type>Number</Type>
<Purpose>Input</Purpose>
<Formula varies="0"></Formula>
<Value varies="1"></Value>
<Units varies="0" group="length">meter</Units>
</Column>
<Column Name="ModelThickness">
<Type>Number</Type>
<Purpose>Input</Purpose>
<Formula varies="0"></Formula>
<Value varies="1"></Value>
<Units varies="0" group="length">meter</Units>
</Column>
<Column Name="ModelHeight">
<Type>Number</Type>
<Purpose>Input</Purpose>
<Formula varies="0"></Formula>
<Value varies="1"></Value>
<Units varies="0" group="length">meter</Units>
</Column>
<Column Name="ModelPerimeter">
<Type>Number</Type>
<Purpose>Input</Purpose>
<Formula varies="0"></Formula>
<Value varies="1"></Value>
<Units varies="0" group="length">meter</Units>
</Column>
<Column Name="ModelArea">
<Type>Number</Type>
<Purpose>Input</Purpose>
<Formula varies="0"></Formula>
<Value varies="1"></Value>
<Units varies="0" group="area">squaremeter</Units>
</Column>
<Column Name="ModelVolume">
<Type>Number</Type>
<Purpose>Input</Purpose>
<Formula varies="0"></Formula>
<Value varies="1"></Value>
<Units varies="0" group="volume">cubicmeter</Units>
</Column>
<Column Name="ModelWeight">
<Type>Number</Type>
<Purpose>Input</Purpose>
<Formula varies="0"></Formula>
<Value varies="1"></Value>
<Units varies="0" group="weight">kilogram</Units>
</Column>
<Column Name="Length">
<Type>Number</Type>
<Purpose>Calculation</Purpose>
<Formula varies="1">=ModelLength</Formula>
<Value varies="1"></Value>
<Units varies="1" group="length">meter</Units>
</Column>
<Column Name="Width">
<Type>Number</Type>
<Purpose>Calculation</Purpose>
<Formula varies="1">=ModelWidth</Formula>
<Value varies="1"></Value>
<Units varies="1" group="length">meter</Units>
</Column>
<Column Name="Thickness">
<Type>Number</Type>
<Purpose>Calculation</Purpose>
<Formula varies="1">=ModelThickness</Formula>
<Value varies="1"></Value>
<Units varies="1" group="length">meter</Units>
</Column>
<Column Name="Height">
<Type>Number</Type>
<Purpose>Calculation</Purpose>
<Formula varies="1">=ModelHeight</Formula>
<Value varies="1"></Value>
<Units varies="1" group="length">meter</Units>
</Column>
<Column Name="Perimeter">
<Type>Number</Type>
<Purpose>Calculation</Purpose>
<Formula varies="1">=ModelPerimeter</Formula>
<Value varies="1"></Value>
<Units varies="1" group="length">meter</Units>
</Column>
<Column Name="Area">
<Type>Number</Type>
<Purpose>Calculation</Purpose>
<Formula varies="1">=ModelArea</Formula>
<Value varies="1"></Value>
<Units varies="1" group="area">squaremeter</Units>
</Column>
<Column Name="Volume">
<Type>Number</Type>
<Purpose>Calculation</Purpose>
<Formula varies="1">=ModelVolume</Formula>
<Value varies="1"></Value>
<Units varies="1" group="volume">cubicmeter</Units>
</Column>
<Column Name="Weight">
<Type>Number</Type>
<Purpose>Calculation</Purpose>
<Formula varies="1">=ModelWeight</Formula>
<Value varies="1"></Value>
<Units varies="1" group="weight">kilogram</Units>
</Column>
<Column Name="Count">
<Type>Number</Type>
<Purpose>RollUp</Purpose>
<Formula varies="1"></Formula>
<Value varies="1"></Value>
<Units varies="0" group="any">count</Units>
</Column>
<Column Name="PrimaryQuantity">
<Type>Number</Type>
<Purpose>RollUp</Purpose>
<Formula varies="1"></Formula>
<Value varies="1"></Value>
<Units varies="1" group="any"></Units>
</Column>
</GlobalConfiguration>
<Table Name="ObjectResource">
<ColumnRef Name="Object" />
<ColumnRef Name="Description1" />
<ColumnRef Name="Description2" />
<ColumnRef Name="ModelLength" />
<ColumnRef Name="ModelWidth" />
<ColumnRef Name="ModelThickness" />
<ColumnRef Name="ModelHeight" />
<ColumnRef Name="ModelPerimeter" />
<ColumnRef Name="ModelArea" />
<ColumnRef Name="ModelVolume" />
<ColumnRef Name="ModelWeight" />
<ColumnRef Name="Length">
<Formula varies="1"></Formula>
<Units varies="1" group="length"></Units>
</ColumnRef>
<ColumnRef Name="Width">
<Formula varies="1"></Formula>
<Units varies="1" group="length"></Units>
</ColumnRef>
<ColumnRef Name="Thickness">
<Formula varies="1"></Formula>
<Units varies="1" group="length"></Units>
</ColumnRef>
<ColumnRef Name="Height">
<Formula varies="1"></Formula>
<Units varies="1" group="length"></Units>
</ColumnRef>
<ColumnRef Name="Perimeter">
<Formula varies="1"></Formula>
<Units varies="1" group="length"></Units>
</ColumnRef>
<ColumnRef Name="Area">
<Formula varies="1"></Formula>
<Units varies="1" group="area"></Units>
</ColumnRef>
<ColumnRef Name="Volume">
<Formula varies="1"></Formula>
<Units varies="1" group="volume"></Units>
</ColumnRef>
<ColumnRef Name="Weight">
<Formula varies="1"></Formula>
<Units varies="1" group="weight"></Units>
</ColumnRef>
<ColumnRef Name="Count">
<Purpose>Calculation</Purpose>
</ColumnRef>
<ColumnRef Name="PrimaryQuantity">
<Purpose>Calculation</Purpose>
</ColumnRef>
</Table>
<Table Name="StepResource">
<ColumnRef Name="Length">
<Purpose>RollUp</Purpose>
<Formula varies="1"></Formula>
<Units varies="1" group="length"></Units>
</ColumnRef>
<ColumnRef Name="Width">
<Purpose>RollUp</Purpose>
<Formula varies="1"></Formula>
<Units varies="1" group="length"></Units>
</ColumnRef>
<ColumnRef Name="Thickness">
<Purpose>RollUp</Purpose>
<Formula varies="1"></Formula>
<Units varies="1" group="length"></Units>
</ColumnRef>
<ColumnRef Name="Height">
<Purpose>RollUp</Purpose>
<Formula varies="1"></Formula>
<Units varies="1" group="length"></Units>
</ColumnRef>
<ColumnRef Name="Perimeter">
<Purpose>RollUp</Purpose>
<Formula varies="1"></Formula>
<Units varies="1" group="length"></Units>
</ColumnRef>
<ColumnRef Name="Area">
<Purpose>RollUp</Purpose>
<Formula varies="1"></Formula>
<Units varies="1" group="area"></Units>
</ColumnRef>
<ColumnRef Name="Volume">
<Purpose>RollUp</Purpose>
<Formula varies="1"></Formula>
<Units varies="1" group="volume"></Units>
</ColumnRef>
<ColumnRef Name="Weight">
<Purpose>RollUp</Purpose>
<Formula varies="1"></Formula>
<Units varies="1" group="weight"></Units>
</ColumnRef>
<ColumnRef Name="Count" />
<ColumnRef Name="PrimaryQuantity" />
</Table>
<Table Name="ObjectStep">
<ColumnRef Name="Object" />
<ColumnRef Name="ModelLength" />
<ColumnRef Name="ModelWidth" />
<ColumnRef Name="ModelThickness" />
<ColumnRef Name="ModelHeight" />
<ColumnRef Name="ModelPerimeter" />
<ColumnRef Name="ModelArea" />
<ColumnRef Name="ModelVolume" />
<ColumnRef Name="ModelWeight" />
<ColumnRef Name="Length">
<Formula varies="1"></Formula>
<Units varies="1" group="length"></Units>
</ColumnRef>
<ColumnRef Name="Width">
<Formula varies="1"></Formula>
<Units varies="1" group="length"></Units>
</ColumnRef>
<ColumnRef Name="Thickness">
<Formula varies="1"></Formula>
<Units varies="1" group="length"></Units>
</ColumnRef>
<ColumnRef Name="Height">
<Formula varies="1"></Formula>
<Units varies="1" group="length"></Units>
</ColumnRef>
<ColumnRef Name="Perimeter">
<Formula varies="1"></Formula>
<Units varies="1" group="length"></Units>
</ColumnRef>
<ColumnRef Name="Area">
<Formula varies="1"></Formula>
<Units varies="1" group="area"></Units>
</ColumnRef>
<ColumnRef Name="Volume">
<Formula varies="1"></Formula>
<Units varies="1" group="volume"></Units>
</ColumnRef>
<ColumnRef Name="Weight">
<Formula varies="1"></Formula>
<Units varies="1" group="weight"></Units>
</ColumnRef>
</Table>
<Table Name="Step">
<ColumnRef Name="Length">
<Purpose>RollUp</Purpose>
</ColumnRef>
<ColumnRef Name="Width">
<Purpose>RollUp</Purpose>
</ColumnRef>
<ColumnRef Name="Thickness">
<Purpose>RollUp</Purpose>
</ColumnRef>
<ColumnRef Name="Height">
<Purpose>RollUp</Purpose>
</ColumnRef>
<ColumnRef Name="Perimeter">
<Purpose>RollUp</Purpose>
</ColumnRef>
<ColumnRef Name="Area">
<Purpose>RollUp</Purpose>
</ColumnRef>
<ColumnRef Name="Volume">
<Purpose>RollUp</Purpose>
</ColumnRef>
<ColumnRef Name="Weight">
<Purpose>RollUp</Purpose>
</ColumnRef>
</Table>
<Table Name="Object">
<ColumnRef Name="Object" />
<ColumnRef Name="Description1" />
<ColumnRef Name="Description2" />
<ColumnRef Name="ModelLength" />
<ColumnRef Name="ModelWidth" />
<ColumnRef Name="ModelThickness" />
<ColumnRef Name="ModelHeight" />
<ColumnRef Name="ModelPerimeter" />
<ColumnRef Name="ModelArea" />
<ColumnRef Name="ModelVolume" />
<ColumnRef Name="ModelWeight" />
<ColumnRef Name="Length">
<Formula varies="1"></Formula>
<Units varies="1" group="length"></Units>
</ColumnRef>
<ColumnRef Name="Width">
<Formula varies="1"></Formula>
<Units varies="1" group="length"></Units>
</ColumnRef>
<ColumnRef Name="Thickness">
<Formula varies="1"></Formula>
<Units varies="1" group="length"></Units>
</ColumnRef>
<ColumnRef Name="Height">
<Formula varies="1"></Formula>
<Units varies="1" group="length"></Units>
</ColumnRef>
<ColumnRef Name="Perimeter">
<Formula varies="1"></Formula>
<Units varies="1" group="length"></Units>
</ColumnRef>
<ColumnRef Name="Area">
<Formula varies="1"></Formula>
<Units varies="1" group="area"></Units>
</ColumnRef>
<ColumnRef Name="Volume">
<Formula varies="1"></Formula>
<Units varies="1" group="volume"></Units>
</ColumnRef>
<ColumnRef Name="Weight">
<Formula varies="1"></Formula>
<Units varies="1" group="weight"></Units>
</ColumnRef>
<ColumnRef Name="Count">
<Purpose>Calculation</Purpose>
</ColumnRef>
<ColumnRef Name="PrimaryQuantity">
<Purpose>Calculation</Purpose>
</ColumnRef>
</Table>
<Table Name="Item">
<ColumnRef Name="Length">
<Purpose>RollUp</Purpose>
</ColumnRef>
<ColumnRef Name="Width">
<Purpose>RollUp</Purpose>
</ColumnRef>
<ColumnRef Name="Thickness">
<Purpose>RollUp</Purpose>
</ColumnRef>
<ColumnRef Name="Height">
<Purpose>RollUp</Purpose>
</ColumnRef>
<ColumnRef Name="Perimeter">
<Purpose>RollUp</Purpose>
</ColumnRef>
<ColumnRef Name="Area">
<Purpose>RollUp</Purpose>
</ColumnRef>
<ColumnRef Name="Volume">
<Purpose>RollUp</Purpose>
</ColumnRef>
<ColumnRef Name="Weight">
<Purpose>RollUp</Purpose>
</ColumnRef>
<ColumnRef Name="Count">
<Formula varies="1">=1</Formula>
</ColumnRef>
<ColumnRef Name="PrimaryQuantity" />
</Table>
<Table Name="ItemGroup" />
<Table Name="Resource">
<ColumnRef Name="Length">
<Purpose>RollUp</Purpose>
</ColumnRef>
<ColumnRef Name="Width">
<Purpose>RollUp</Purpose>
</ColumnRef>
<ColumnRef Name="Thickness">
<Purpose>RollUp</Purpose>
</ColumnRef>
<ColumnRef Name="Height">
<Purpose>RollUp</Purpose>
</ColumnRef>
<ColumnRef Name="Perimeter">
<Purpose>RollUp</Purpose>
</ColumnRef>
<ColumnRef Name="Area">
<Purpose>RollUp</Purpose>
</ColumnRef>
<ColumnRef Name="Volume">
<Purpose>RollUp</Purpose>
</ColumnRef>
<ColumnRef Name="Weight">
<Purpose>RollUp</Purpose>
</ColumnRef>
<ColumnRef Name="Count">
<Formula varies="1">=1</Formula>
</ColumnRef>
<ColumnRef Name="PrimaryQuantity" />
</Table>
<Table Name="ResourceGroup" />
</Workbook>
</ConfigFile>
</Takeoff>
答案 0 :(得分:0)
我现在理解了这个问题,你的输出中有不需要的ns0前缀。为了避免它们,您可能需要使用相应的前缀注册所有名称空间:
etree.register_namespace("xs","http://www.w3.org/2001/XMLSchema")
etree.register_namespace("xsi","http://www.w3.org/2001/XMLSchema-instance")
etree.register_namespace("","http://download.autodesk.com/us/navisworks/schemas/nw-TakeoffCatalog-10.0.xsd")