import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/** Membaca struktur FXML dengan parser DOM standar JDK. */
public class BacaFxml {
    static String FXML = """
            <?xml version="1.0" encoding="UTF-8"?>

            <?import javafx.scene.control.*?>
            <?import javafx.scene.layout.*?>

            <VBox xmlns:fx="http://javafx.com/fxml"
                  fx:controller="AplikasiKontroler"
                  spacing="10">
                <Label fx:id="judul" text="Data Siswa"/>
                <TextField fx:id="inputNama" promptText="Nama lengkap"/>
                <Button fx:id="tombolSimpan" text="Simpan"
                        onAction="#handleSimpan"/>
            </VBox>
            """;

    public static void main(String[] args) throws Exception {
        DocumentBuilderFactory pabrik = DocumentBuilderFactory.newInstance();
        Document dok = pabrik.newDocumentBuilder()
                .parse(new org.xml.sax.InputSource(new java.io.StringReader(FXML)));

        Element akar = dok.getDocumentElement();
        System.out.println("akar         = " + akar.getTagName());
        System.out.println("controller   = " + akar.getAttribute("fx:controller"));
        System.out.println("spacing      = " + akar.getAttribute("spacing"));

        NodeList anak = akar.getChildNodes();
        for (int i = 0; i < anak.getLength(); i++) {
            if (anak.item(i) instanceof Element e) {
                String fxId = e.getAttribute("fx:id");
                String aksi = e.getAttribute("onAction");
                System.out.printf("%-10s fx:id=%-14s text=%-14s onAction=%s%n",
                        e.getTagName(),
                        fxId.isEmpty() ? "-" : fxId,
                        e.getAttribute("text").isEmpty() ? "-" : e.getAttribute("text"),
                        aksi.isEmpty() ? "-" : aksi);
            }
        }
    }
}
