How use XML?

hi all:
I need use xml to strore a data struct,for exeample:
struct A{
long,x;
long y;
};
A a;
a.x=1;
a.y=2;
how can i do???

thank

Hi,

In the context of ROOT, you can do this simply by generating the dictionary for struct A and use a TXMLFile. Or in other word, use the typical ROOT I/O mechanism except that when creating the output file use the class TXMLFile instead of TFile.

Cheers,
Philippe.

hi:
thank for your answering this question.
i do a test,it can be saved to a xml file,but can"t save the struct member,there is a erroe “Error in TClass::BuildRealData: can not find any ShowMembers function for TICFCmd!”.

TICFCmd is a struct,i define it as :
struct TICFCmd{
long x;
long y;
//ClassDef(TICFCmd,1);
};
the code is :
TXMLFile* fXMLfile=new TXMLFile(“exp.xml”,“recreate”);
TICFCmd* tempcmd=new TICFCmd;
(*tempcmd).x=1;
(*tempcmd).y=2;
printf("%d,%d\n",(*tempcmd).x,(*tempcmd).x);
fXMLfile->WriteObjectAny(tempcmd,“TICFCmd”,“tempcmd”);
what is problem???
thank

Hi,
you need a dictionary, and you should use a class, not a struct, with a ClassDef macro invocation. See the users guide on “Adding your own class”.
Axel.

hi:
I know the class could be stored by XML,i want know if struct could be stored and how.
thank.

zhangchi

You can handle struct exactly the same way as classes. (Unless what you meant what interpreted vs compiled, the I/O can currently only handle compiled classes and struct).

Philippe.

yes, when i change "struct"to “class”,it can be stored by XML,and it isn’t different to struct in using.
class TICFCmd{
public:
long x;
long y;
ClassDef(TICFCmd,1)
};

thank, and i has another question:
TXMLfile convert the class to the XML file, but i need a point to char,how can i convert the class to a piont-- char*.
zhangchi

Hi,
I assume what you want (but that’s just plain guessing, as I don’t really understand what you want) is write the TTXMLFile, delete (and close) it, and then open a ifstream, which allows you to access the plain written XML, e.g. using strings.
Cheers, Axel.

hi
yes, this way can convert the data to a string,but it would access the disk and low efficiency.can i directly convert the data to XML string? i want convert the data to xml string and transfers the string by network.because the data maybe be changed,so i want transfer the xml string, the interface needn’t be changed.
zhangchi

Hi

It was not forseen to convert separate object to string, which contains xml tags of that object. Normally structures of complete TXMLFile is converted when you write it to disk.

Can you explain little bit, why do you need such functionality?
What do you plan to do with such string? Search there for some xml tags?
In principle, it is not difficult to implement such feature, but I need more information from your side.

Can be, that you not need to convert object to string, but you can work with
XML structures, which created by obj->Write() method and kept in memory until you write file data to disk. These structures are very similar to libxml2 structures and can be access via TKeyXML::KeyNode() method.

Regards, Sergy

hi
I explain what i want.
we neef transfer some data by network,but the data could bu changed and we don’t konw its struct.so we convert it to xml string to send to the accepter.and the accepter can parse it by xml.

hi
And I have a another question,how can i get a object from a xml file?
the code:
[color=darkred]
TXMLFile* fXMLFile=new TXMLFile(“temp.xml”,“read”);
TList* fList=new TList();
fList=fXMLFile->GetStreamerInfoList();
TICFCmd* obj=(TICFCmd*)(fList->FindObject(“TICFCmd”));[/color]

and the code:

[color=darkred]TXMLFile* fXMLFile=new TXMLFile(“temp.xml”,“read”);
TICFCmd* obj=(TICFCmd*)(fXMLGile->GetObjectUnchecked(“tempCmd;1”));
[/color]
are all failed.

[color=darkred]class TICFCmd:public TObject{
public:
long x;
long y;
ClassDef(TICFCmd,1)
};
[/color]

[quote=“zhch777”]hi
And I have a another question,how can i get a object from a xml file?
the code:
[color=darkred]
TXMLFile* fXMLFile=new TXMLFile(“temp.xml”,“read”);
TList* fList=new TList();
fList=fXMLFile->GetStreamerInfoList();
TICFCmd* obj=(TICFCmd*)(fList->FindObject(“TICFCmd”));[/color]

and the code:

[color=darkred]TXMLFile* fXMLFile=new TXMLFile(“temp.xml”,“read”);
TICFCmd* obj=(TICFCmd*)(fXMLGile->GetObjectUnchecked(“tempCmd;1”));
[/color]
are all failed.

[color=darkred]class TICFCmd:public TObject{
public:
long x;
long y;
ClassDef(TICFCmd,1)
};
[/color][/quote]

Hi

First of all, you not need to access SteramerInfoList(). It used
internally in TFile/TXMLFile classes for schema evolution.

Normally, to open TXMLFile is better to use TFile::Open(“file.xml”) call,
but your code also should work.

Before read object from file, can you make

fXMLFile->ls();

to see list of all keys. In your code is misstype (fXMLGile instead of fXMLFile). I guess, just do following:

[color=darkred]
TFile* fXMLFile = TFile::Open(“temp.xml”);
fXMLFile->ls();
TICFCmd* obj = 0;
fXMLFile->GetObject(“tempCmd;1”, obj);
[/color]

If it is still not woking, can you sent me (S.Linev@gsi.de)
produced xml file and classes that I can test them.

And about reading of data on accepter side. The easiest solution for
you just use ROOT and automatic schema evolution if class structure is
changing. If you cannot run ROOT but still able to use C++ code, there
is a way to read ROOT xml files without ROOT. Look into TXMLPlayer class.

Regards,
Sergey