XScript 매뉴얼 · Chapter 35

XML 객체 — XML 문서 처리

스크립트에서 키워드 XML 으로 접근하는 XML 유틸. 한 프로세스에서 여러 개의 XML 문서를 이름으로 식별하며, 노드와 attribute 모두 path 표기법으로 다룹니다.

개념설명
xmlNameXML 문서 식별자 ("recipe", "settings" 등)
path노드 위치 — "/root/items/item" 같은 XPath 류
attributeName속성 이름 (옵션) — 없으면 노드 텍스트값
결과 / 에러모든 호출은 내부 결과 플래그 — GetResult / GetErrorMessage

기본 사용 흐름

// 1) 빈 문서 생성
XML.Create("recipe");
 
// 2) 노드 추가 + 값 설정
XML.AddNode("recipe", "/root", "");                 // <root/>
XML.SetString ("recipe", "/root/title",  "AlignRecipe");
XML.SetInteger("recipe", "/root/version", 2);
XML.SetDouble ("recipe", "/root/exposure_ms", 18.5);
XML.SetBool   ("recipe", "/root/use_filter",  true);
 
// 3) 디스크에 저장
XML.Save("recipe", "D:/Recipes/align.xml");
 
// 4) 정리
XML.Remove("recipe");

문서 라이프사이클

XML.Create("doc");                              // 빈 문서
XML.Parse ("doc", "<root><a>1</a></root>");     // 문자열 파싱
XML.Load  ("doc", "D:/Settings/sys.xml");       // 파일 로드
XML.Save  ("doc", "D:/Settings/sys.xml");       // 파일 저장
 
if( XML.Contains("doc") )  XML.Remove("doc");
XML.RemoveAll();

노드 추가 — AddNode / AddNodeCDATA

XML.AddNode("doc", "/root/items/item", "auto");
// <root><items><item>auto</item></items></root>
//   부모 path 가 없으면 자동 생성됩니다.
 
// CDATA 로 감싸야 하는 텍스트(HTML, 다중 행, < > & 포함 등)
XML.AddNodeCDATA("doc", "/root/desc", "<b>hello</b>\nworld");

같은 path 를 두 번 호출하면 노드가 1 개 추가되어 sibling 으로 쌓입니다.


노드 텍스트값 쓰기 — SetXxx

XML.SetString ("doc", "/root/name",     "Alice");
XML.SetInteger("doc", "/root/age",      30);
XML.SetDouble ("doc", "/root/weight",   62.4);
XML.SetBool   ("doc", "/root/active",   true);
 
// CDATA 로 감싸 저장
XML.SetStringCDATA("doc", "/root/desc", "<html>...</html>");

노드가 없으면 자동 생성됩니다. 단, 같은 path 가 여러 sibling 일 경우 가장 첫 번째 노드의 값이 갱신됩니다.


Attribute 쓰기 — SetXxxAttribute

XML.SetStringAttribute ("doc", "/root/name", "lang", "ko");
XML.SetIntegerAttribute("doc", "/root/age",  "ver", 2);
XML.SetDoubleAttribute ("doc", "/root/weight", "kg", 0.001);
XML.SetBoolAttribute   ("doc", "/root/active", "auto", true);

값 읽기 — GetXxx

// 노드 텍스트값 읽기
string s = XML.GetString ("doc", "/root/name");
int    i = XML.GetInteger("doc", "/root/age");
double d = XML.GetDouble ("doc", "/root/weight");
bool   b = XML.GetBool   ("doc", "/root/active");
 
// Attribute 읽기 (3 번째 인자)
string lang = XML.GetString ("doc", "/root/name", "lang");
int    ver  = XML.GetInteger("doc", "/root/age",  "ver");

3 번째 인자(attributeName) 가 비어있으면 노드 텍스트값을, 채워져 있으면 해당 attribute 값을 반환합니다.


자식 노드 개수 — GetCount

// /root/items 아래 자식 수 (모든 자식)
int total = XML.GetCount("doc", "/root/items");
 
// /root/items 아래 'item' 이름의 자식 수
int items = XML.GetCount("doc", "/root/items", "item");
 
for(i, 0, items - 1)
{
    string name = XML.GetString("doc", $"/root/items/item[{i}]/name");
    // ...
}

결과 / 에러 처리

XML.ResetResult("doc");
XML.SetInteger("doc", "/root/missing/path", 1);
if( XML.GetResult("doc") == false )
{
    LogError($"XML write failed : {XML.GetErrorMessage("doc")}");
}

여러 SetXxx 호출 후 한 번에 검증하는 패턴이 깔끔합니다.


자주 쓰는 패턴

시스템 설정 저장 / 불러오기

FUNCTION SaveSettings(string path)
{
    XML.Create("set");
    XML.AddNode("set", "/Settings", "");
    XML.SetInteger("set", "/Settings/Light",    Data::Light);
    XML.SetDouble ("set", "/Settings/Exposure", Data::Exposure);
    XML.SetBool   ("set", "/Settings/UseAuto",  Data::UseAuto);
 
    XML.Save("set", path);
    XML.Remove("set");
}
 
FUNCTION LoadSettings(string path)
{
    XML.Load("set", path);
    if( XML.GetResult("set") == false )
    {
        ShowMessage(EB_Ok, XML.GetErrorMessage("set"));
        return false;
    }
 
    Data::Light    = XML.GetInteger("set", "/Settings/Light");
    Data::Exposure = XML.GetDouble ("set", "/Settings/Exposure");
    Data::UseAuto  = XML.GetBool   ("set", "/Settings/UseAuto");
 
    XML.Remove("set");
    return true;
}

외부 도구가 만든 XML 파싱

XML.Load("rep", "D:/Reports/lot_001.xml");
 
int n = XML.GetCount("rep", "/Report/Items", "Item");
for(i, 0, n - 1)
{
    string name  = XML.GetString ("rep", $"/Report/Items/Item[{i}]/Name");
    int    count = XML.GetInteger("rep", $"/Report/Items/Item[{i}]/Count");
    Log($"#{i} {name} = {count}");
}
XML.Remove("rep");

함수 한눈 요약

분류함수
라이프사이클Create · Parse · Load · Save · Remove · RemoveAll · Contains
결과GetResult · GetErrorMessage · ResetResult
노드AddNode · AddNodeCDATA · GetCount
텍스트 쓰기SetString · SetInteger · SetDouble · SetBool · SetStringCDATA
Attribute 쓰기SetStringAttribute · SetIntegerAttribute · SetDoubleAttribute · SetBoolAttribute
값 읽기GetString · GetInteger · GetDouble · GetBool (path + 옵션 attributeName)