YedXml - An example

/*--------------------------------------
XML file is like this:

<?xml version='1.0'?>
<configuration>
<provatag attr1= "2" attr2 = 'ciao'/>
<description>this is a description that <d1>open another tag </d1>continues here </description>
<server>
<name>server1</name>
<type>LINUX</type>
</server>
<server>
<name>server2</name>
<type>BSD</type>
</server>
</configuration>

--------------------------------------*/


#include <stdio.h>
#include <yedstd.h>
#include <yedxml.h>

main()
  {
  int s,i,j;
  YedXml *p=New(YedXml);
  XML_ELEMENT *pX,pF;

  /* EXAMPLE OF DIRECTLY LOADING XML BUFFER */
  // p->Set_XML_Buffer(p,"<ciao><element attr="1">content</element></ciao>" ,XML_NOTPRESERVE);


  /* LOAD XML BUFFER FROM FILE */
  p->Get_XML_Buffer(p,"./prova.xml",XML_NOTPRESERVE);

  /* LOAD TREE FROM XML BUFFER */
  s=p->Load_Tree(p);
  if(s>=YPRSUCCESS)
    {
    printf("File prova.xml successfully parsed!!!\n\n");
    
    /* DISPLAY TREE INTO STDOUT */
    p->Display_Tree(p);
    }
  else printf("Error [%d] in parsing file prova.xml.\n",s);
  
  /* GET XML DECLARATION OF XML TREE */
  pX=p->Get_XML_Declaration(p);
  if(pX==NULL) printf("XML Declaration is missing!!!\n\n");
  else
    {
    printf("---------------------------------\nXML DECLARATION:\n");

    /* SHOWS ATTRIBUTE NAMES AND VALUES OF XML DECLARATION */
    for(s=0;s<pX->siNumAttrElement;s++)
       printf("Attribute [%s]=[%s]\n",
       pX->pstAttrElement[s].scbName,pX->pstAttrElement[s].scbValue);
    }

  /* COUNT XML ELEMENTS INTO XML TREE WITH TAG 'server' */
  i=p->Enum_Element(p,NULL,"server");
  if(i>0)
    {
    printf("---------------------------------\nFound %d occurrence of XML element <server>\n",i);

    /* CICLICALLY FIND AND EXTRACT XML ELEMENTS
       FROM XML TREE WITH TAG 'server' */

    for(j=1;j<=i;j++)
       {
       s=p->Find_Element(p,"server",j,NULL,&pF);
       if(s==YPRSUCCESS)
         {
         printf("XML element <server> number [%d] -                  content:\n%s\n",j,pF.pscContent);
         }
       else printf("Error [%s] in Find_Element method\n",s);
       }
    }
  else printf("Enum_Element returns [%d]\n",i);

/* RELEASE YedXml OBJECT */
Delete(YedXml,p);
}


http://yed.sourceforge.net