Browse Source

compose xml-tidy-text

master
Ulrich Carmesin 3 years ago
parent
commit
4511b4a559
  1. 12
      test/test_xml.py
  2. 112
      utils/xml_tool.py

12
test/test_xml.py

@ -72,6 +72,8 @@ class MyTestCase(unittest.TestCase):
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} } ] }
tree_attr2 = { "eins": [ {"zwei": 2, "drei": 3, "vier": [ { "@name": "attr", "fuenf": 5} ] } ] }
tree_attr3 = { "eins": [ {"zwei": 2, "drei": 3, "vier": [ { "@name": "attr", "#text": "text" } ] } ] }
c = utils.xml_tool.fcts()
xml = c.tidy(tree_dict, 0)
print("-------------------------------------")
@ -83,10 +85,16 @@ class MyTestCase(unittest.TestCase):
print("-------------------------------------")
print(xml)
xml = c.tidy(tree_attr, 0)
print("-------------------------------------")
print("------ tree_attr -------------------------------")
print(xml)
xml = c.tidy(tree_attrx, 0)
print("-------------------------------------")
print("------ tree_attrx -------------------------------")
print(xml)
xml = c.tidy(tree_attr2, 0)
print("------ tree_attr2 -------------------------------")
print(xml)
xml = c.tidy(tree_attr3, 0)
print("------ tree_attr3 -------------------------------")
print(xml)
if __name__ == '__main__':

112
utils/xml_tool.py

@ -180,24 +180,89 @@ class fcts:
def getTidyTab(self, depth=0):
tab = ""
# print("depth "+str(depth))
for i in range(0, depth):
tab += "\t"
return tab
def tidy(self, tree, depth=0):
# tree_dict = { "eins": "zwei", "drei": "vier" }
def hasTidyAttr(self, tree):
first = True
if isinstance(tree, dict):
for x in tree:
if first and isinstance(tree[x], dict):
return self.hasTidyAttr(tree[x])
elif first and isinstance(tree[x], list):
return self.hasTidyAttr(tree[x])
elif x[0:1] == "@":
return True
else:
return False
if isinstance(tree, list):
for x in tree:
if first and isinstance(x, dict):
return self.hasTidyAttr(x)
elif first and isinstance(x, list):
return self.hasTidyAttr(x)
elif x[0:1] == "@":
return True
else:
return False
elif tree[0:1] == "@":
return True
return False
def getTidyAttr(self, tree, x, depth=0):
xml = ""
attr = ""
if isinstance(tree, dict):
for y in tree:
if isinstance(y, dict):
return ""
elif isinstance(y, list):
attr = self.getTidyAttr(y, x, depth)
elif y[0:1] == "@":
attr += " "+y[1:]+"="+tree[y]
elif len(attr) > 1:
xml = "<"+x+attr+">"+y+"</"+x+">"
return xml
else:
return ""
elif isinstance(tree, dict):
for y in tree:
pass
return ""
def getTidyDict(self, tree, x, depth=0):
xml = ""
tab = self.getTidyTab(depth)
newTree = {}
attr = ""
for y in tree:
if y[0:1] == "@":
attr = " " + y[1:] + "=\"" + tree[y] + "\""
else:
newTree[y] = tree[y]
if len(newTree) > 0:
xml += tab + "<" + x + attr + ">\n" + self.tidy(newTree, depth + 1) + tab + "</" + x + ">\n"
else:
xml += tab + "<" + x + attr + "/>\n"
return xml
print("tidy "+str(type(tree)))
def tidy(self, tree, depth=0):
verify = 0
xml = ""
tab = self.getTidyTab(depth)
hasa = self.hasTidyAttr(tree)
if verify: print("getTidy "+str(type(tree))+" hasAttr "+str(hasa))
if isinstance(tree, dict):
print("dict")
for x in tree:
print(x)
if verify: print("dict " + str(x))
if isinstance(tree[x], dict):
hasa = self.hasTidyAttr(tree[x])
newTree = {}
attr = ""
for y in tree[x]:
if verify: print("y-for "+str(y))
if y[0:1] == "@":
attr = " " + y[1:] + "=\"" + tree[x][y] + "\""
else:
@ -207,21 +272,52 @@ class fcts:
else:
xml += tab + "<" + x + attr + "/>\n"
elif isinstance(tree[x], list):
hasa = self.hasTidyAttr(tree[x])
if hasa:
xml += tab + "<" + x + self.tidy(tree[x], depth) + "</" + x + ">\n"
else:
xml += tab + "<" + x + ">\n" + self.tidy(tree[x], depth + 1) + tab + "</" + x + ">\n"
elif x[0:1] == "@":
attr = " " + x[1:] + "=\"" + tree[x] + "\""
elif x == "#text":
xml = str(tree[x])
return xml
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 verify: print("list " + str(x))
if isinstance(x, dict):
xml += self.tidy(x, depth)
newTree = {}
attr = ""
text = False
hasa = self.hasTidyAttr(x)
for y in x:
if verify: print("y-for "+str(y))
if y[0:1] == "@":
attr = " " + y[1:] + "=\"" + x[y] + "\""
elif y == "#text":
text = x[y]
else:
newTree[y] = x[y]
if text and len(newTree) > 0 and len(attr) > 0:
xml += "" + attr + ">" + self.tidy(newTree, depth + 1)
elif len(newTree) > 0 and len(attr) > 0:
xml += "" + attr + ">\n" + self.tidy(newTree, depth + 1) + tab
elif len(attr) > 0:
if not isinstance(x, dict):
xml += tab + "<296" + x + attr + "/>\n"
else:
xml += attr + ">"+text
else:
xml += self.tidy(newTree, 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