Skip to content

Commit 297537b

Browse files
authored
Merge pull request #29 from RockyCYG/master
feat: Remove possible incompatible characters for XML compatibility
2 parents fd0941a + ac625cb commit 297537b

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

hmdriver2/_xpath.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3+
import re
34
from typing import Dict
45
from lxml import etree
56
from functools import cached_property
@@ -33,15 +34,26 @@ def __call__(self, xpath: str) -> '_XMLElement':
3334

3435
return _XMLElement(None, self._d)
3536

37+
@staticmethod
38+
def _sanitize_text(text: str) -> str:
39+
"""Remove XML-incompatible control characters."""
40+
return re.sub(r'[\x00-\x1F\x7F]', '', text)
41+
3642
@staticmethod
3743
def _json2xml(hierarchy: Dict) -> etree.Element:
44+
"""Convert JSON-like hierarchy to XML."""
3845
attributes = hierarchy.get("attributes", {})
39-
tag = attributes.get("type", "orgRoot") or "orgRoot"
40-
xml = etree.Element(tag, attrib=attributes)
46+
47+
# 过滤所有属性的值,确保无非法字符
48+
cleaned_attributes = {k: _XPath._sanitize_text(str(v)) for k, v in attributes.items()}
49+
50+
tag = cleaned_attributes.get("type", "orgRoot") or "orgRoot"
51+
xml = etree.Element(tag, attrib=cleaned_attributes)
4152

4253
children = hierarchy.get("children", [])
4354
for item in children:
4455
xml.append(_XPath._json2xml(item))
56+
4557
return xml
4658

4759

0 commit comments

Comments
 (0)