Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

How to re-organized XML file and call XML file from my software

Hello, everybody. Sorry to disturb you all.
Currently, I have a speech recording and playback software developed by Microsoft Visual C++ 6.0. The software is used to train children with speech problem and scores will be calculated and showed at the end of each practice session. The software is able to record and playback real-time.

Children use this software at home and save their practice records in a single floppy disk. Then, children will bring the floppy disk with practice records inside when they return to our centre.

I have two problems with the xml programming part. My software automatically created history file (in XML format) to display the scores of the childrens practices. The history file (in XML format) is saved by the children in floppy disk when they return to our centre.
But, the data or information in the XML is in a mess where all data are put together. Do you mind to tell me what should I do to create more organized XML format? The therapist needs to scroll in order to read the scores. I have the program codes of XML in four of my cpp files.

The second question is how to call or open XML file (the history file) from my software?
My codes are as below. After I execute the program, it is error free and the File Open dialog box is popped up but no XML file came out after I select the desired XML file. My codes are as below:

void CSpeechDlg::OnScore()
{
// TODO: Add your command handler code here
CFileDialog openfile(TRUE,NULL,NULL,OFN_OVERWRITEPROMPT,"XML File (*.xml)|*.xml|");
if (openfile.DoModal()==IDOK)
{
CString cs=openfile.GetFileName();
char* filename=new char[cs.GetLength()+1];
strcpy(filename,cs);
}

}

I promise I wont disturb you a lot, I promise I will do my best. Can you please help me?
I am totally new to this as the software is written by one of my previous friend who worked together with me in a voluntary speech rehabilitation centre. We need this software to train children in the centre. I work part time there.

Hope you can give me some guidances on how to do it. I promise I can learn as independent as possible but I really need your guidance at this very beginning, please.
Please, I really appreciate all your help.
[2320 byte] By [bella_11041988] at [2007-11-11 8:40:15]
# 1 Re: How to re-organized XML file and call XML file from my software
<Do you mind to tell me what should I do to create more organized XML format?>

It would help if you could show us some sample data (with the names changed of course). One common approach is to define a stylesheet that can be used to transform the raw XML to browser-friendly HTML.

<After I execute the program, it is error free and the File Open dialog box is popped up but no XML file came out after I select the desired XML file.>
Your program reads the filename to be used, but it does not write the file to disk. Is your program using standard C++ filestreams or the equivalent Windows file functions? For example, you could add the following to the if (openfile.DoModal()==IDOK) branch:

std::ofstream out(filename);
std::string xmlDocument("<root/>");
out << xmlDocument << std::endl;
out.close();

You would also need to include <iostream>, <fstream> and <string>.

Do let us know how you get on!
geralds at 2007-11-11 23:28:30 >
# 2 Re: How to re-organized XML file and call XML file from my software
<Do you mind to tell me what should I do to create more organized XML format?>

It would help if you could show us some sample data (with the names changed of course). One common approach is to define a stylesheet that can be used to transform the raw XML to browser-friendly HTML.

<After I execute the program, it is error free and the File Open dialog box is popped up but no XML file came out after I select the desired XML file.>
Your program reads the filename to be used, but it does not write the file to disk. Is your program using standard C++ filestreams or the equivalent Windows file functions? For example, you could add the following to the if (openfile.DoModal()==IDOK) branch:

std::ofstream out(filename);
std::string xmlDocument("<root/>");
out << xmlDocument << std::endl;
out.close();

You would also need to include <iostream>, <fstream> and <string>.

Do let us know how you get on!

Hi, Geralds, thank you very much for your reply, appreciate.

1. I failed to use the above codes to open a XML file, I dont know what's wrong with the codes.

2. I attached the XML file created by my program. As you can see, it is created in an unorganized format. I hope it can be created with spacing between sentences so that it can be more easier to read. And, I hope therapist does not need to scroll to the far right end in order to read the children's scoring. I am sorry, I dont understand your point of transformation, could you please elaborate a little bit more? Please forgive my stupidness. I promise to do my best.

Is your program using standard C++ filestreams or the equivalent Windows file functions?

Sorry, Geralds, I dont understand this question. Please dont scold me, I try to learn as much as I can.
bella_11041988 at 2007-11-11 23:29:35 >
# 3 Re: How to re-organized XML file and call XML file from my software
The first issue to address is that your file is not well-formed XML, that is, it cannot be processed by XML tools.

I don't know where this file is generated (presumably this is done from within the program), but that is where you would need to start.

The first problem is that the XML declaration is incorrectly expressed. Just add a question mark after the opening angle bracket and another before the closing angle bracket. (As the declaration does not specify an encoding you could also just leave the line out altogether.)

Secondly the file you attached does not contain a root element. XML files represent a tree structure in textual format: there can be only one root element in a document. You could solve this problem by adding <root> after the XML declaration and </root> at the end of the document. It would also be helpful to add <user> before and </user> after the chunk of elements relating to a particular user. If it's not clear where you would do this, just send more details of the source code and we can try to work something out. (There's also a C++ forum next door where you could work through C++-related difficulties.)

If you do this, most XML editors will help you make the text reasonably easy to read: once it is well-formed, the file can be pretty-printed (indented for easier reading) and syntax-highlighted (so element names, attributes and attribute values take different colours). Most importantly, Internet Explorer/Firefox/editors with folding will let you hide and show the results of one user at a time.

If this still isn't user-friendly enough, you can apply an XSL stylesheet to show the whole thing in a neat HTML table. If you want to pursue this route, let us know.
geralds at 2007-11-11 23:30:39 >
# 4 Re: How to re-organized XML file and call XML file from my software
The first issue to address is that your file is not well-formed XML, that is, it cannot be processed by XML tools.

I don't know where this file is generated (presumably this is done from within the program), but that is where you would need to start.

The first problem is that the XML declaration is incorrectly expressed. Just add a question mark after the opening angle bracket and another before the closing angle bracket. (As the declaration does not specify an encoding you could also just leave the line out altogether.)

Secondly the file you attached does not contain a root element. XML files represent a tree structure in textual format: there can be only one root element in a document. You could solve this problem by adding <root> after the XML declaration and </root> at the end of the document. It would also be helpful to add <user> before and </user> after the chunk of elements relating to a particular user. If it's not clear where you would do this, just send more details of the source code and we can try to work something out. (There's also a C++ forum next door where you could work through C++-related difficulties.)

If you do this, most XML editors will help you make the text reasonably easy to read: once it is well-formed, the file can be pretty-printed (indented for easier reading) and syntax-highlighted (so element names, attributes and attribute values take different colours). Most importantly, Internet Explorer/Firefox/editors with folding will let you hide and show the results of one user at a time.

If this still isn't user-friendly enough, you can apply an XSL stylesheet to show the whole thing in a neat HTML table. If you want to pursue this route, let us know.

Hi, Geralds, thank you very much for your reply, appreciate.

I have changed the XML format (the ? symbol) to the correct one but the program still could not open the XML file. For the XML prolog and document issue, I still cant figure out how should I change in the program codes in order to obtain the format as stated in your post. Sorry.

Is it very difficult to do the XSLT?

Just asking, forgive me if I am wrong. Is it possible to copy the data in the XML file to a MFC dialog box? The data in the XML file is updated from time to time. I am just asking, I dont know whether it can be done or not. But, I just hope my problem can be solved using the simplest method as I am so stupid and I do not have enough knowledge due to my short experiences with programming (just about 2++ months) plus I am slow learner. Please forgive me. But, I promise I will try my best. I just need some advices at this very beginning. It is not necessary that the history must bein XML file format. We can accept any file format as long as it can show or display the chldren's scoring to the therapist.

I attached the source codes in this post. I hope I am sending the files needed. If you need more files, I am most willing to send them.
bella_11041988 at 2007-11-11 23:31:41 >
# 5 Re: How to re-organized XML file and call XML file from my software
Yes, it helps to see the files. One difficulty I have is that I don't use MS Visual C++, so I don't have access to the application framework (Microsoft Foundation Classes) used by your application.

The program seems to work with five files, the names of which are read from an XML file in Setting::OnLoad. Quite possibly this is where you get a "Failed to load XML file" error.

The file that is opened here has to be well-formed. Otherwise the line:

bool loadOkay=doc.LoadFile(filename);

will return false. It also needs to contain children with 'technique' attributes. This is one problem with your XML file, which has 'Technique' attributes with capital initials. XML is case-sensitive, so the first change I would make is to change the XML attributes to technique.

In short, I would suggest: (a) ensure that the file is well-formed. Any XML editor (check http://en.wikipedia.org/wiki/XML_editor for examples) will help you achieve this. If you no longer get "Failed to load XML file", you've done this. (This will mean that TinyXML has successfully parsed the XML file.)

(b) you need to change the Technique attributes to technique. A simple global replace will do this. Basically you need to tweak the XML file until you no longer get "Wrong XML file" messsages.

With any luck this should get you to the next stage.

Once you've got everything in place, writing an appropriate XSL transform won't be a problem, but there's little point doing this until the application accepts your XML document.

I hope this helps! If not, let us know.
geralds at 2007-11-11 23:32:35 >
# 6 Re: How to re-organized XML file and call XML file from my software
Hi, Geralds, thank you very much for your reply, appreciate.

You gave me a very userful information. I will check the xml files to make sure it is well-formed and change it to Technique.

I will try my best and will tell you how I am going.
Just hope slow learner like me can learn faster :(
bella_11041988 at 2007-11-11 23:33:38 >
# 7 Re: How to re-organized XML file and call XML file from my software
HI, Geralds, thank you very much for your post, appreciate.

I have been reading for the XML tutorial for the past few days. I realized that I have two problems to be overcome.

1. My XML is not in a well-formed format. I would like to find out which section is wrong. Is there any web software to test or validate the XML? I tried the link below but I dont know why it cant work. Is there any other link to be used?
http://www.w3schools.com/dom/dom_validate.asp

2. From what I read, it specified that I should try to avoid the use of attributes if information feels like data, Use child elements if the information feels like data. In my case, many attributes are used and the information to be displayed are scoring data, means I should change the use of attributes to child elements? Do you mind to elaborate on how to change the use of attributes to child elements in the codes?
bella_11041988 at 2007-11-11 23:34:37 >
# 8 Re: How to re-organized XML file and call XML file from my software
1. You probably don't need to validate your data: a well-formedness check will do.

Well-formedness means that your document fulfils the basic rules of XML: there is exactly one document element, all other documents have exactly one parent element, and so on.

To be valid, a document also has to conform to a document type or schema that expresses further constraints (the root element is called aaa and contains a sequence of elements bbb that take an optional attribute ccc, etc.).

I suggest you download a free XML editor to do this work. http://en.wikipedia.org/wiki/XML_editor lists some useful free editors. Personally I'm using this editor (http://xml-copy-editor.sourceforge.net), but as I'm working on this project I'm not an impartial guide here!

2. I think you can safely ignore this advice at this point. As long as your data is well-formed, your existing program should work ok. Changing the structure (e.g. moving the scores out of attribute values to text nodes) would mean changing your program as well.

If any of this isn't working, just give us a shout!
geralds at 2007-11-11 23:35:37 >