Parsing a XML file in C using MiniXML - c

I'm using the MiniXML library to parse a XML file in C, however when i try to read the node's value it returns NULL.
Here's the code:
FILE *fp;
mxml_node_t *tree;
fp = fopen("test.xml", "r");
tree = mxmlLoadFile(NULL, fp, MXML_TEXT_CALLBACK);
fclose(fp);
mxml_node_t *node;
for(node = mxmlFindElement(tree, tree,"node",NULL, NULL,MXML_DESCEND);
node != NULL;
node = mxmlFindElement(node, tree,"node",NULL, NULL, MXML_DESCEND)) {
printf("Text: %s\n", node->value.text.string);
}
The problem is that node->value.text.string is NULL. I've been reading the documentation and I don't know what im doing wrong. Has anybody run into this problem before?

Try changing your for-loop to specify:
node->child->value.text.string
instead of:
node->value.text.string
Does that work? It's just a guess, but I'm thinking it might be necessary to get the data for the "node" elements.
If that does not work, look at C++: Trouble loading long string from XML file using Mini-XML. This person says they needed to use MXML_DESCEND_FIRST instead of MXML_DESCEND to fix their problem. I'm not sure if it would help in your case.
If neither of these work, you might post your input XML as well so that we can try to recreate your problem.

I got the same error. Thanks for the link you posted. I can fixed the error by changing
node->child->value.text.string in node->child->value.opaque.
nothing to change when the file is opened because the type_cb function select the right case.

Minixml bug 502 - minixml mxmlLoad*() functions fail to load text with MXML_TEXT_CALLBACK.
To work around, define and use your own text callback

Related

Find and then show/hide an edje part

I have a Tizen Edje file which defines my layout. One part is an image with part name 'warning'. The item is set to visible in the edge file, and it shows as expected.
I want to hide this part using C code:
Evas_Object* image_NotSetYet = (Evas_Object *) edje_object_part_object_get(elm_layout_edje_get(wid->edjeLayout), "warning");
if (image_NotSetYet == NULL) {
dlog_print(DLOG_ERROR, LOG_TAG, "View: Unable to get warning image part");
return;
}
evas_object_hide(image_NotSetYet);
I have tried many different ways to get the Evas Object associated with this part name and hide it. After many hours I stumbled onto some code that I modeled after, and it seems to work. I can hide (and show) my image part now.
However, I later add an unrealted image to a swallow in this layout and show it. All of a suddent the 'warning' part image shows again. Why? Am I hiding the 'warning' part the wrong way? Is there something wrong with the above?
Alternatively, is there something wrong with the way I am adding an image to the swallow below? The image (from file) will show up, but suddenly my warning part above shows too:
Evas_Object *img = elm_image_add(wid->edjeLayout);
if (img == NULL) {
dlog_print(DLOG_ERROR, LOG_TAG, "View: Failed to add a image.");
return;
}
// Create an image and set contents to imagefile
char *imageFileName = barcode_filename();
bool isSet = elm_image_file_set(img, imageFileName, NULL);
dlog_print((isSet?DLOG_INFO:DLOG_ERROR), LOG_TAG, "View: %s file [%s] to image",(isSet==EINA_TRUE?"Set":"Failed to set"),imageFileName);
free(imageFileName);
evas_object_show(img);
// Assign the image to the swallow2 part
elm_object_part_content_set(wid->edjeLayout,"swallow2",img);
I tried adding the image to the 'window' instead of the 'layout' but that didn't seem to matter. (I've seen many contradictory examples so I don't know which is right)
I tried setting the image to the 'swallow2' part name many different ways (again, many contradictory ways show). Is this the problem?
Otherwise, can someone explain what is going wrong?
The image_NotSetYet is not an image object.
Evas_Object* image_NotSetYet = (Evas_Object *) edje_object_part_object_get(elm_layout_edje_get(wid->edjeLayout), "warning");
That refers to the "warning" swallow part object.
You should never modify the state of the returned object, because it's meant to be managed by Edje, solely.
If you want to get the image pointer from your layout as you expected, you could use following instead.
Evas_Object* image_NotSetYet = elm_object_part_content_get((wid->edjeLayout), "warning")
But as above link describes, the image object should be manged by Edje.
You might got the 2nd problem because it is managed by Edje. So please use edje_object_signal_emit to handle swallowed images.

Error about sending an image into bytea column in postgresql db with libpq library

I'm coding in C and using Libpq library of Postgresql and I would like to store a PNG image into the database in "bytea" type. I have been searching on the net for hours and couldn't find a good example to handle this work, so wanted to write here and ask for your help.
I have 12 params to bind and one of them is PNG image. The rest are char*, and no problem with them.
Below is what I have tried so far. (I'm writing the necessary part of code):
PGresult *res;
PGconn *conn;
const char *paramValues[12];
int paramLengths[12];
int paramFormats[12];
const char* imageFrame=frameImageArray.data();// frameImageArray.data is const char*.
int imageSize=frameImageArray.size();
paramFormats[11]=1;
paramLengths[11]=imageSize;
paramValues[11]= imageFrame;
// insertplate is a function on db
res = PQexecParams(conn,
"SELECT insertplate($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12)",
12, // param number
NULL, // oid param type
paramValues, // param values
paramLengths, // param lengths
paramFormats, // params format, 1 for binary
1); //1 for binary result
It is compiled with no problem but when it comes to store the image to db on runtime, the classical runtime error occures :
"Unhandled exception at 0x6d3dc220 in ..._debug.exe: 0xC0000005: Access violation reading location 0x000000007f91e508."
Seems something about memory handling.
Whatever I tried, I couldn't make it run and I'm not able to see my error. Do I have to use Oids for sending binary data to db with PQexecParams? Or something else I'm missing ? I really appreciate if someone help me with this.
Thanks in advance.
Edit: I just realised that if I use Insert statement, it works well, but this function doesn't. Normally it works. Weird.
I finally find the error.
//paramFormats[0]=0;
//paramFormats[1]=0;
//paramFormats[2]=0;
//paramFormats[3]=0;
//paramFormats[4]=0;
//paramFormats[5]=0;
//paramFormats[6]=0;
//paramFormats[7]=0;
//paramFormats[8]=0;
//paramFormats[9]=0;
//paramFormats[10]=0;
//paramFormats[11]=1;
It is possible to leave the "parameter format" as NULL, but I wanted to set only "paramFormats[11]" as seen above. I have set the others to 0 as well and it worked. I didn't expect something like this.

Saving a Binary tree to a file in C

I am currently doing a Customers program when a user can add/edit/search/list customers. I decided to use a binary tree as the backbone of this program. My idea was to then save each item in the tree to "customers.dat" right before the program closes, then on start up load everything from the file to the tree. So far so good, however after finally managing to save the binary search tree into a file, I have one bug.
Lets say I add 3 customers the first time. I then close the program, and when I reopen it I will find the same 3 customers in the tree. however, the next time I open the file, it gives me an error from one of my predefined errors, which occurs when a node is not able to identity whether to go left or right, maybe because its empty or uncomparable. here are some code snippets. I aslo tried using other fileopen techniques other than a+b, and I had no such error, but by the way I designed my program, I need the append method or else only one record will save.
Customers are stored in Cstmr in the header:
typedef struct customer
{
char Name[MAXNAME];
char Surname[MAXNAME];
char ID[MAXID];
char Address[MAXADDRESS];
} Cstmr;
else:
void CustomerTreeToFile(Tree*pt)
{
if (TreeIsEmpty(pt))
puts("Nothing to save!");
else
Traverse(pt,saveItem); //Traverses each node, and appliess the function
//saveItem to each node
}
void saveItem(Cstmr C)
{
save = C;
customers = fopen("customers.dat","ab+");
fwrite(&C,sizeof (Cstmr), 1, customers);
fclose(customers);
}
The problem is that you are always appending all the data to the same file ... so everything from the previous run is duplicated.
One solution would be to delete (unlink) the file before saving the data with your current approach.
However, as Freezerburn pointed out earlier, it's more economic to not open and close the file for each item. Just open the file once in overwrite mode (i.e. not append), then write all the data, then close the file. Should be much faster, too.
Another problem is that you save your data in binary format. Try to define an easy to read textual format. That would have made the problem obvious ...

Adding to HIMageList failing, cannot figure out why

I'm confused because I cannot establish why my ImageList_Add call is failing. I know it must be something that I am doing wrong with the Image or maybe I'm calling it wrong but I have no idea how I can go about fixing it :S Any help you can offer would be appreciated! :)
The code i'm using is below. I'm getting output on the console saying it couldn't add to the image list. From the docs,ImageList_Add will return an indice of where in the imagelist it managed to add the image so -1 is returned if it cant.
Which is all well and good but I cannot find anywhere why/what causes the add to fail!
The code may have memory leaks, though at the moment,ive spent almost a day trying to figure out various issues with this so I just want to get it to work!
HIMAGELIST imageList = ImageList_Create(20,20,ILC_COLOR16,1,2 );
if (imageList == NULL)
{
printf("Error creating imagelist - dlg_create_dropdown_menu. Returning NULL\n");
return NULL;
}
HBITMAP currentImage = (HBITMAP) LoadImage(NULL,"active_mdoe_icn.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
if (currentImage == NULL)
{
if (GetLastError()== 2)
{
printf("File not found - dlg_create_dropdown_menu. Returning NULL.\n");
return NULL;
}
printf("Error loading image from file - dlg_create_dropdown_menu. Returning NULL.\n");
return NULL;
}
int imageIndex;
if ( (imageIndex = ImageList_Add(imageList,currentImage,NULL)) == -1 )
{
printf("Error adding to the image list - dlg_create_dropdown_menu. Returning NULL.\n");
return NULL;
}
Thanks all, any help would be greatly received! :)
Could this be a problem with the actual image being corrupt? I've read about that in a few places.. Might just be my luck if i'm not doing anything daft :)
After much arguing with myself and windows I didnt figure out why the add was not working.
Instead of trying using the imagelist I just load in each image one at a time at work with it that way. Not sure why I thought using the image list was better :s
I was trying to load images into a dropdown menu I was creating but I found a better way of doing it. Which was using the MENUITEMINFO struct and specifying MIIM_BITMAP | MIIM_STRING as the two flags to fMask :) which mean I could have an image and an text in each menu item :)
Also image names with 0 underscores somehow makes it easier for windows to find lol
Anyways, hope this helped someone :)

Retrieving a node original string

I'm a newbie with xml/libxml.
What I'm trying to do is simple.
I have a node like :
<tag attr="example" attr2="example2"/>
which is stored in a xmlNode. I want to get the following xmlChar* :
"<tag attr="example" attr2="example2"/>"
I tried xmlNodeGetContent and xmlNodeListGetString but none of them seem to do what I want.
I'm using libxml2 in C. The file xml version is 1.0
xmlNodeGetContent isn't what you're looking for : check this page for info on its behavior.
I think the function you're looking for is xmlNodeDump, which will of course require a valid buffer.

Resources