我正在编写一个小的python工具来帮助我组织和分析一些数据。不幸的是,某些数据包含某些字符,这些字符似乎在xml元素中不起作用(例如'/')
是否有一种方法可以将该信息保留在字符串中,并且仍然使用该名称创建xml元素?
替换字符会更改数据并使其部分不可用
from xml.etree.ElementTree import Element, SubElement, Comment, tostring
from xml.dom import minidom
class Rule:
name: str
rule_type: str
usage: list
macros: list
features: list
local_functions: list
local_variables: list
return_value: str
info: str
def __init__(self, name, rule_type, usage, macros, features, local_functions, local_variables, return_value, info):
self.name = name
self.rule_type = rule_type
self.usage = [elem for elem in usage]
self.macros = [elem for elem in macros]
self.features = [elem for elem in features]
self.local_functions = local_functions
self.local_variables = [elem for elem in local_variables]
self.return_value = return_value
self.info = info
def rule_to_xml(self):
root = Element(self.name)
root.append(Comment(self.info))
输入“ foobar/2_barfoo
”之类的名称时,出现异常:
格式不正确(令牌无效):第1行,XYZ列
答案 0 :(得分:3)
不。 “ /”字符为not allowed in XML element names。一种可能的解决方案可能是将ERROR TypeError: res.forEach is not a function
at SafeSubscriber._next (dashboard.component.ts:130)
之类的字符串保留在元素的属性中,例如:
foobar/2_barfoo