解析XML片段并更新ID Args: xml_content: mxCell的XML字符串 new_id: 新分配的ID Returns: 更新后的ET.Element,解析失败返回None
(self, xml_content: str, new_id: int)
| 373 | return mxfile |
| 374 | |
| 375 | def _parse_and_update_cell(self, xml_content: str, new_id: int) -> Optional[ET.Element]: |
| 376 | """ |
| 377 | 解析XML片段并更新ID |
| 378 | |
| 379 | Args: |
| 380 | xml_content: mxCell的XML字符串 |
| 381 | new_id: 新分配的ID |
| 382 | |
| 383 | Returns: |
| 384 | 更新后的ET.Element,解析失败返回None |
| 385 | """ |
| 386 | try: |
| 387 | # 清理XML字符串 |
| 388 | xml_content = xml_content.strip() |
| 389 | |
| 390 | # 如果不是以<mxCell开头,尝试提取mxCell部分 |
| 391 | if not xml_content.startswith('<mxCell'): |
| 392 | match = re.search(r'<mxCell[^>]*>.*?</mxCell>|<mxCell[^>]*/>', |
| 393 | xml_content, re.DOTALL) |
| 394 | if match: |
| 395 | xml_content = match.group() |
| 396 | else: |
| 397 | self._log(f"警告: 无法提取mxCell: {xml_content[:100]}...") |
| 398 | return None |
| 399 | |
| 400 | # 解析XML |
| 401 | cell = ET.fromstring(xml_content) |
| 402 | |
| 403 | # 更新ID |
| 404 | cell.set("id", str(new_id)) |
| 405 | |
| 406 | # 确保parent正确(除了特殊cell外,都应该是"1") |
| 407 | if cell.get("parent") not in ["0"]: |
| 408 | cell.set("parent", "1") |
| 409 | |
| 410 | return cell |
| 411 | |
| 412 | except ET.ParseError as e: |
| 413 | self._log(f"XML解析失败: {e}, 内容: {xml_content[:100]}...") |
| 414 | return None |
| 415 | |
| 416 | def _create_base_xml(self, canvas_width: int, canvas_height: int) -> ET.Element: |
| 417 | """创建DrawIO基础XML结构""" |
no test coverage detected