Skip to content

Instantly share code, notes, and snippets.

@gree2
Created August 3, 2012 03:07
Show Gist options
  • Save gree2/3243969 to your computer and use it in GitHub Desktop.
Save gree2/3243969 to your computer and use it in GitHub Desktop.
Python: Convert2Xaml
import re
import os
from lxml import etree
from copy import deepcopy
fileInput = "Input.xml"
fileOutput = "Output.xml"
def Indent(elem, level=0):
i = "\n" + level * " "
if len(elem):
if not elem.text or not elem.text.strip():
elem.text = i + " "
for e in elem:
Indent(e, level+1)
if not e.tail or not e.tail.strip():
e.tail = i
if level and (not elem.tail or not elem.tail.strip()):
elem.tail = i
return elem
def Step1ReplaceName():
# Read
xmlInput = open(fileInput, "r")
# Write
xmlOutput = open("step1.xml", "w")
line = xmlInput.readline()
while line:
line = line.replace("x:Name", "Name")
xmlOutput.write(line)
line = xmlInput.readline()
xmlInput.close()
xmlOutput.close()
print "Step1ReplaceName..."
def Step2IndentXml():
# Input xml tree
xmlInput = etree.parse("step1.xml")
rootInput = xmlInput.getroot()
# Indent xml
Indent(rootInput)
xmlFile = open("step2.xml", "w")
xmlInput.write(xmlFile)
print "Step2IndentXml..."
def RemoveComment(node):
for child in node.getchildren():
if "_Comment" == child.__class__.__name__:
# print repr(child.text), repr(child.tail)
node.remove(child)
else:
RemoveComment(child)
def Step3ClearComment():
tree = etree.parse("step2.xml")
root = tree.getroot()
RemoveComment(root)
xmalFile = open("step3.xml", "w")
tree.write(xmalFile)
print "Step3ClearComment..."
def Step4Packaging():
# Input xml tree
xmlInput = etree.parse("step3.xml")
rootInput = xmlInput.getroot()
# ModelVisual3D
rootOutput = etree.Element("ModelVisual3D")
rootOutput.attrib["Name"] = rootInput.attrib["Name"]
rootOutput.attrib["Transform"] = rootInput.attrib["Transform"]
# Output xml tree
xmlOutput = etree.ElementTree()
xmlOutput._setroot(rootOutput)
# Process
for childXml in rootInput.getchildren():
# Nothing to do
if len(childXml.attrib) == 0:
continue
# Wrap with InteractivelModeVisual3D
imv3D = etree.SubElement(rootOutput, "InteractivelModeVisual3D")
# InteractivelModeVisual3D.Model
imv3DModel = etree.SubElement(imv3D, "InteractivelModeVisual3D.Model")
if "Model3DGroup" == childXml.tag:
# Bind Events
imv3D.attrib["MouseDown"] = "OnMouseDown_MachineFront"
imv3D.attrib["MouseEnter"] = "OnMouseEnter_MachineFront"
imv3D.attrib["MouseLeave"] = "OnMouseLeave_MachineFront"
# Bind Name & Tag
imv3D.attrib["Name"] = "_" + childXml.attrib["Name"]
imv3D.attrib["Tag"] = childXml.attrib["Name"]
# Copy xml
imv3DModel.append(deepcopy(childXml))
if "GeometryModel3D" == childXml.tag:
# Model3DGroup
imv3DModelChild = etree.SubElement(imv3DModel, "Model3DGroup")
# Copy xml
imv3DModelChild.append(deepcopy(childXml))
xmalFile = open("step4.xml", "w")
Indent(rootOutput)
xmlOutput.write(xmalFile)
print "Step4Packaging..."
def Step5ReplaceName():
# Read
xmlInput = open("step4.xml", "r")
# Write
xmlOutput = open(fileOutput, "w")
line = xmlInput.readline()
while line:
line = line.replace("Name", "x:Name")
line = line.replace("DynamicResource", "StaticResource")
line = line.replace("InteractivelModeVisual3D", "local:InteractivelModeVisual3D")
xmlOutput.write(line)
line = xmlInput.readline()
xmlInput.close()
xmlOutput.close()
print "Step5ReplaceName..."
def Step6CleanUp():
os.remove("step1.xml")
os.remove("step2.xml")
os.remove("step3.xml")
os.remove("step4.xml")
print "Step6CleanUp..."
def ConvertToXaml():
Step1ReplaceName()
Step2IndentXml()
Step3ClearComment()
Step4Packaging()
Step5ReplaceName()
Step6CleanUp()
ConvertToXaml()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment