Příklady: Xml


Jmenné prostory

call XmlTest_2a;
call XmlTest_2b;
Exit

procedure XmlTest_2a
  dim hDoc      : Longint;
  dim hRootNode : Longint;
  dim hFluxNode : Longint;
  dim hAbcNode  : Longint;
  
  _XmlCreateDoc(hDoc, "<ROOT xmlns="+#34+"http://world.cz/XMLSchema"+#34+"/>");
  _XmlAddNamespace(hDoc, "w",  "http://world.cz/XMLSchema");
  hRootNode := _XmlGetNode(hDoc, 0, "w:ROOT");
  hFluxNode := _xmlAddElementNode(hDoc,hRootNode,"item","http://flux.cz/XMLSchema/Test","flux");
  hAbcNode  := _xmlAddElementNode(hDoc,hRootNode,"item","http://abc.cz/XMLSchema/Test","abc");
  _XmlSaveDoc(hDoc, "C:\Test.xml");
  XmlReleaseDoc(hDoc);
endproc

procedure XmlTest_2b
  dim hDoc  : Longint;
  dim hNode : Longint;
  dim Value : StringZ;
  
  _XmlOpenDoc(hDoc, "C:\Test.xml");
  _XmlAddNamespace(hDoc, "w", "http://world.cz/XMLSchema");
  _XmlAddNamespace(hDoc, "f", "http://flux.cz/XMLSchema/Test");
  _XmlAddNamespace(hDoc, "a", "http://abc.cz/XMLSchema/Test");
  hNode := _XmlGetNode(hDoc,0,"w:ROOT/f:item");
  if(hNode<>0)then
    _XmlGetNodeValue(hNode, Value);
    _(Value)
  endif
  hNode := _XmlGetNode(hDoc,0,"w:ROOT/a:item");
  if(hNode<>0)then
    _XmlGetNodeValue(hNode, Value);
    _(Value)
  endif
  XmlReleaseDoc(hDoc);
endproc

Výsledek:

 

Uložení části Xml do souboru

procedure UlozCastXml
    dim sVal  : StringZ;
    dim hDoc  : Longint;
    dim hNode : Longint;

    _XmlOpenDoc(hDoc, "c:\Test.xml");
    _XmlAddNamespace(hDoc,"c", "http://www.govtalk.gov.uk/CM/envelope"); 
    _XmlAddNamespace(hDoc,"e", "http://www.cssz.cz/XMLSchema/reldp/ErrorEnvelope"); 	
    hNode := _XmlGetNode(hDoc,0,"c:GovTalkMessage/c:Body");
    _XmlGetNodeXmlText(hNode,sVal);
    XmlReleaseDoc(hDoc);
    XmlReleaseNode(hNode);

    _XmlCreateDoc(hDoc, sVal);
    _XmlSaveDoc(hDoc, "W:\error_body");
    XmlReleaseDoc(hDoc);
    XmlReleaseNode(hNode);
endproc 

Ulož Xml do databáze

dim g_uid            : StringZ;
dim g_business_error : StringZ;

procedure UlozXml_DoDatabaze(    sGuid : StringZ)
    dim db   : Longint;
    dim set  : Longint;
    dim hDoc : longint;

    { Otevřeme Xml dokument }
    if(_XmlOpenDoc(hDoc, "D:\Social\eldp\xml\business_error_1.xml"))then
        { Vytvoříme spojení s tabulkou }
        SQLDBOpen(db, ...); 
        SQLOpen(db, set);
        SQLBind(set, "uid",            SQL_VARCHAR,        g_uid);
        SQLBind(set, "business_error", SQL_LONGVARCHAR,-1, g_business_error);
        { Najdeme požadovanou větu }
        SQLSelect(Set, "PVS_SUB_STATUS", "uid='" + sGuid + "'", "uid");
        if _SQLFirst(Set) then
            { Věta byla nalezena - vložíme XML }
            if(_SQLEdit(set))then
                _XmlGetDocXmlText(hDoc, g_business_error);
                g_business_error := _StrToBase64(g_business_error, CP_UTF8);
                if(_SQLUpdate(set)=false)then
                    _MsgBox(1,"Chyba při _SQLUpdate!",0);
                endif
            else
                _MsgBox(1,"Chyba při _SQLAddNew!",0);
            endif
        else
            _MsgBox(1,"Věta s Guidem " + sGuid + " nebyla nalezena!",0);
        endif
        SQLClose(set);
        SQLDBClose(db);	
    else
        _MsgBox(1,"Chyba při otevírání Xml dokumentu!",0);
    endif
endproc { UlozXml_DoDatabaze }