合并多个XML文件 Args: xml_paths: XML文件路径列表 output_path: 输出路径 canvas_width: 画布宽度 canvas_height: 画布高度 Returns: 输出文件路径
(self,
xml_paths: List[str],
output_path: str,
canvas_width: int,
canvas_height: int)
| 457 | # ======================== 便捷方法 ======================== |
| 458 | |
| 459 | def merge_xml_files(self, |
| 460 | xml_paths: List[str], |
| 461 | output_path: str, |
| 462 | canvas_width: int, |
| 463 | canvas_height: int) -> str: |
| 464 | """ |
| 465 | 合并多个XML文件 |
| 466 | |
| 467 | Args: |
| 468 | xml_paths: XML文件路径列表 |
| 469 | output_path: 输出路径 |
| 470 | canvas_width: 画布宽度 |
| 471 | canvas_height: 画布高度 |
| 472 | |
| 473 | Returns: |
| 474 | 输出文件路径 |
| 475 | """ |
| 476 | self._log(f"合并{len(xml_paths)}个XML文件") |
| 477 | |
| 478 | all_cells = [] |
| 479 | |
| 480 | for xml_path in xml_paths: |
| 481 | if not os.path.exists(xml_path): |
| 482 | self._log(f"警告: 文件不存在 {xml_path}") |
| 483 | continue |
| 484 | |
| 485 | try: |
| 486 | tree = ET.parse(xml_path) |
| 487 | root = tree.getroot() |
| 488 | |
| 489 | # 提取mxCell元素 |
| 490 | root_elem = root.find(".//root") |
| 491 | if root_elem is not None: |
| 492 | for cell in root_elem: |
| 493 | cell_id = cell.get("id") |
| 494 | if cell_id not in ["0", "1"]: |
| 495 | all_cells.append(ET.tostring(cell, encoding='unicode')) |
| 496 | |
| 497 | except Exception as e: |
| 498 | self._log(f"解析失败 {xml_path}: {e}") |
| 499 | |
| 500 | # 转换为XMLFragment |
| 501 | fragments = [] |
| 502 | for i, cell_xml in enumerate(all_cells): |
| 503 | fragments.append(XMLFragment( |
| 504 | element_id=i, |
| 505 | xml_content=cell_xml, |
| 506 | layer_level=LayerLevel.OTHER.value # 无法判断层级时使用OTHER |
| 507 | )) |
| 508 | |
| 509 | # 使用标准流程合并 |
| 510 | context = ProcessingContext( |
| 511 | image_path="", |
| 512 | canvas_width=canvas_width, |
| 513 | canvas_height=canvas_height, |
| 514 | output_dir=os.path.dirname(output_path) or "." |
| 515 | ) |
| 516 | context.xml_fragments = fragments |
nothing calls this directly
no test coverage detected