How do I create a new gbXML file?
Using the create_gbxml function
The create_gbXML()
function is used to create a new xgbxml.xgbxml.gbXML
instance.
import xgbxml
gbxml=xgbxml.create_gbXML()
print(type(gbxml))
# prints "<class 'xgbxml.xgbxml.gbXML'>"
print(gbxml.tostring())
# prints "<gbXML xmlns="http://www.gbxml.org/schema" version="6.01" temperatureUnit="C"
# lengthUnit="Meters" areaUnit="SquareMeters" volumeUnit="CubicMeters"
# useSIUnitsForResults="true"/>
print(type(gbxml.getroottree()))
# prints "<class 'lxml.etree._ElementTree'>"
Note, to access the element tree the lxml method getroottree
can be used.
Adding more elements
Adding new elements to the gbxml root note is done in xgbxml using the add_[...]
methods.
import xgbxml
gbxml=xgbxml.create_gbXML()
campus=gbxml.add_Campus(id='my_campus')
building1=campus.add_Building(id='my_building1')
print(gbxml.tostring())
# prints ...
# <gbXML xmlns="http://www.gbxml.org/schema" version="6.01" temperatureUnit="C" lengthUnit="Meters" areaUnit="SquareMeters" volumeUnit="CubicMeters" useSIUnitsForResults="true">
# <Campus id="my_campus">
# <Building id="my_building1"/>
# </Campus>
# </gbXML>
Note how the id
arguments are passed into add_Campus
and add_Building
. This can be done for all of the XML attributes associated with these nodes.
Saving the gbXML file
This is done with the standard lxml process using the write
method of the etree instance.
import xgbxml
gbxml=xgbxml.create_gbXML()
tree=gbxml.getroottree()
tree.write('new_gbxml_file.xml')