File tree Expand file tree Collapse file tree 1 file changed +14
-2
lines changed Expand file tree Collapse file tree 1 file changed +14
-2
lines changed Original file line number Diff line number Diff line change 11# -*- coding: utf-8 -*-
22
3+ import re
34from typing import Dict
45from lxml import etree
56from 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
You can’t perform that action at this time.
0 commit comments