package cn.edu.whu.boot.photozip.util;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
public class XmlUtil {

    /**
     * 解析 XML 文件，提取 <table> 标签的 key
     * @param filePath XML 文件路径
     * @return table 标签中的 key 值
     * @throws Exception 解析错误时抛出
     */
    public static String getTableKey(String filePath) throws Exception {
        File xmlFile = new File(filePath);
        if (!xmlFile.exists()) {
            throw new FileNotFoundException("XML 文件不存在: " + filePath);
        }
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(xmlFile);
        doc.getDocumentElement().normalize();

        NodeList tableNodes = doc.getElementsByTagName("table");
        if (tableNodes.getLength() == 0) {
            throw new RuntimeException("XML 中未找到 <table> 标签");
        }
        Element tableElement = (Element) tableNodes.item(0);
        return tableElement.getAttribute("key");
    }
}
