Browse Source

xml-tidy

master
Ulrich Carmesin 3 years ago
parent
commit
893dfde0bb
  1. 94
      test/test_xml.py
  2. 44
      utils/xml_tool.py

94
test/test_xml.py

@ -0,0 +1,94 @@
import unittest
#import basic.program
import utils.xml_tool
import utils.xml_tool
class MyTestCase(unittest.TestCase):
def xtest_xmlTool(self):
#job = basic.program.Job("unit")
args = {"application": "TEST", "application": "ENV01", "modus": "unit", "loglevel": "debug",
"tool": "job_tool", "tdtyp": "csv", "tdsrc": "implement", "tdname": "firstunit",
"modus": "unit"}
beispiel_json = {'root': {'@attr': 'xyz', '$': 'inhalt', "b": "bold"}}
tree = {}
tree["root"] = args
xml = utils.xml_tool.dict2xml(tree)
print(xml)
xml = utils.xml_tool.dict2xml(beispiel_json)
print(xml)
self.assertEqual(1, 1)
f = utils.xml_tool.fcts()
def xtest_addSingle(self):
tree = {}
# tree = utils.xml_tool.fcts.addMerkmal(tree, '/root/datensegment/satz[@klasse="4711x"]/mm[@name="NAME"]/wert', 2, "abc")
tree = utils.xml_tool.fcts.addMerkmal(tree, '/root/datensegment/kratz/mm[@name="NAME"]/wert', 2, "abc")
self.assertEqual(tree["kratz"]["mm"][0]["wert"], "abc")
self.assertEqual(tree["kratz"]["mm"][0]["@name"], "NAME")
def xtest_addTwo(self):
# a-b-b
# c-a-a c-a-b
tree = {}
print("--------------------------------------------------------------------------------")
tree = utils.xml_tool.fcts.addMerkmal(tree, '/root/datensegment/kratz/mm[@name="NAME"]/wert', 2, "abc")
print("--------------------------------------------------------------------------------")
tree = utils.xml_tool.fcts.addMerkmal(tree, '/root/datensegment/kratz/mm[@name="LAND"]/wert', 2, "xyz")
baum = {}
baum["root"] = tree
print("<------"+str(baum["root"]))
self.assertEqual(tree["kratz"]["mm"][0]["wert"], "abc")
self.assertEqual(tree["kratz"]["mm"][0]["@name"], "NAME")
self.assertEqual(tree["kratz"]["mm"][1]["wert"], "xyz")
self.assertEqual(tree["kratz"]["mm"][1]["@name"], "LAND")
def xtest_addOnePaths(self):
tree = {}
print("--------------------------------------------------------------------------------")
tree = utils.xml_tool.fcts.setMerkmal(tree, '/root/datensegment/satz[@klasse="4711x"]/mm[@name="NAME"]/wert', "abc")
baum = {}
baum["root"] = tree
print("<------"+str(baum["root"]))
self.assertEqual(tree["root"]["datensegment"]["satz"][0]["mm"][0]["wert"], "abc")
self.assertEqual(tree["root"]["datensegment"]["satz"][0]["mm"][0]["@name"], "NAME")
self.assertEqual(tree["root"]["datensegment"]["satz"][0]["@klasse"], "4711x")
def xtest_addTwoPaths(self):
tree = {}
print("--------------------------------------------------------------------------------")
tree = utils.xml_tool.fcts.setMerkmal(tree, '/root/datensegment/satz[@klasse="4711x"]/mm[@name="NAME"]/wert', "abc")
print("--------------------------------------------------------------------------------")
tree = utils.xml_tool.fcts.setMerkmal(tree, '/root/datensegment/satz[@klasse="4711x"]/mm[@name="LAND"]/wert', "xyz")
baum = {}
baum["root"] = tree
print("<------"+str(baum["root"]))
self.assertEqual(tree["root"]["datensegment"]["satz"][0]["mm"][0]["wert"], "abc")
self.assertEqual(tree["root"]["datensegment"]["satz"][0]["mm"][0]["@name"], "NAME")
self.assertEqual(tree["root"]["datensegment"]["satz"][0]["@klasse"], "4711x")
def test_tidy(self):
tree_dict = { "eins": "zwei", "drei": "vier" }
tree_dict3 = { "eins": { "zwei": { "drei": "vier" } }}
tree_list = { "eins": [ {"zwei": 2, "drei": 3, "vier": 4 } ] }
tree_attr = { "eins": [ {"zwei": 2, "drei": 3, "vier": { "@name": "attr"} } ] }
tree_attrx = { "eins": [ {"zwei": 2, "drei": 3, "vier": { "@name": "attr", "fuenf": 5} } ] }
c = utils.xml_tool.fcts()
xml = c.tidy(tree_dict, 0)
print("-------------------------------------")
print(xml)
xml = c.tidy(tree_dict3, 0)
print("-------------------------------------")
print(xml)
xml = c.tidy(tree_list, 0)
print("-------------------------------------")
print(xml)
xml = c.tidy(tree_attr, 0)
print("-------------------------------------")
print(xml)
xml = c.tidy(tree_attrx, 0)
print("-------------------------------------")
print(xml)
if __name__ == '__main__':
unittest.main()

44
utils/xml_tool.py

@ -178,6 +178,50 @@ class fcts:
return array
array.append(elem)
def getTidyTab(self, depth=0):
tab = ""
for i in range(0, depth):
tab += "\t"
return tab
def tidy(self, tree, depth=0):
# tree_dict = { "eins": "zwei", "drei": "vier" }
xml = ""
tab = self.getTidyTab(depth)
print("tidy "+str(type(tree)))
if isinstance(tree, dict):
print("dict")
for x in tree:
print(x)
if isinstance(tree[x], dict):
newTree = {}
attr = ""
for y in tree[x]:
if y[0:1] == "@":
attr = " "+y[1:]+"=\""+tree[x][y]+"\""
else:
newTree[y] = tree[x][y]
if len(newTree) > 0:
xml += tab+"<"+x+attr+">\n"+self.tidy(newTree, depth+1)+tab+"</"+x+">\n"
else:
xml += tab+"<"+x+attr+"/>\n"
elif isinstance(tree[x], list):
xml += tab+"<"+x+">\n"+self.tidy(tree[x], depth+1)+tab+"</"+x+">\n"
else:
xml += tab+"<"+x+">"+str(tree[x])+"</"+x+">\n"
elif isinstance(tree, list):
# tree_list = { "eins": [ {"zwei": 2, "drei": 3, "vier": 4 } ] }
print("list")
for x in tree:
if isinstance(x, dict):
xml += self.tidy(x, depth)
else:
xml += tab+"<"+x+">\n"+self.tidy(x, depth)+tab+"</"+x+">"
else:
xml += "<"+str(tree)+">"
return xml
def addMerkmal(baum, pfad, index, wert):
"""
Eingabe:

Loading…
Cancel
Save