-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml_entity.py
27 lines (23 loc) · 983 Bytes
/
xml_entity.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import json
from dataclasses import dataclass, field
from dacite import from_dict
@dataclass
class Entity:
id: str # уникальный идентификатор тега
title: str = "" # название тега
comment: str = "" # комментарий, идущий перед тегом
attributes: dict = field(default=dict) # аттрибуты тега
value: str | None = None # текст тега
child: list['Entity'] = field(default=list)
def to_string(self) -> str:
result = ""
if self.value == "nullValue":
result = f"<{self.title}"
for attrname in self.attributes:
result += f" {attrname}=\"{self.attributes[attrname]}\""
result += "/>"
return result
def read_json(filename: str) -> Entity:
with open(filename, encoding="utf-8") as f:
json_data = json.load(f)
return from_dict(data_class=Entity, data=json_data)