Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

search a value in my xml file ? #14

Closed
huitre39 opened this issue Mar 1, 2018 · 7 comments
Closed

search a value in my xml file ? #14

huitre39 opened this issue Mar 1, 2018 · 7 comments

Comments

@huitre39
Copy link

huitre39 commented Mar 1, 2018

Hello, i use your library for readxml file.
But i have a problem, i want to search a value by node name like this code :

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "./xml.h"


int main(int argc, char** argv) {

	/* XML source, could be read from disk
	 */
	uint8_t* source = ""
		"<Root>"
			"<Hello>World</Hello>"
            
            
            "<Functions>"
            
            "<Function>"
            "<as>testas</as>"
            "<os>testos</os>"
            "</Function>"
            
            "<Function>"
            "<is>testis</is>"
            "<us>testus</us>"
            "<ls>testls</ls>"
            "</Function>"
            
            "<Function>"
            "<mn>testmn</mn>"
            "</Function>"
            
            "</Functions>"
           
		"</Root>"
	;
    
	struct xml_document* document = xml_parse_document(source, strlen(source));

	if (!document) {
		printf("Could parse document\n");
		exit(EXIT_FAILURE);
	}
	struct xml_node* root = xml_document_root(document);

	struct xml_node* root_hello = xml_node_child(root, 0);
	struct xml_string* hello = xml_node_name(root_hello);
	struct xml_string* world = xml_node_content(root_hello);

	uint8_t* hello_0 = calloc(xml_string_length(hello) + 1, sizeof(uint8_t));
	uint8_t* world_0 = calloc(xml_string_length(world) + 1, sizeof(uint8_t));
	xml_string_copy(hello, hello_0, xml_string_length(hello));
	xml_string_copy(world, world_0, xml_string_length(world));
    
    

	printf("%s %s\n", hello_0, world_0);
	free(hello_0);
	free(world_0);

    
    struct xml_node* root_this = xml_easy_child(root, "Functions",0);
    
    
    unsigned long number_of_function=(unsigned long)xml_node_children(root_this);
	printf("Root/This has %lu children\n", number_of_function);
    
    int i=0;
    
    // i want to search value of node "us" for example
    for (i=0;i<number_of_function;i++) {
        printf("%i \n ",i);
        
        struct xml_node* TMP_function_node = xml_easy_child(root, "Functions", 0, i);
        struct xml_string* TMP_function = xml_node_content(TMP_function_node);
        uint8_t* TMP_function_0 = calloc(xml_string_length(TMP_function) + 1, sizeof(uint8_t));
        xml_string_copy(TMP_function, TMP_function_0, xml_string_length(TMP_function));
        printf("%s\n", TMP_function_0);
    }

	xml_document_free(document, false);
}

in this code, i want to get value of my node "us" for example, in root/Functions/function/us
but how can i make this, because i have many Function node.

thanks for advance for your help

@ooxi
Copy link
Owner

ooxi commented Mar 2, 2018

Do yo already know the path to the us node (like root/Functions/function/us in your example) or are you searching for all nodes with tag name us?

@huitre39
Copy link
Author

huitre39 commented Mar 5, 2018

I don't know the exact location (with indexes)
my problem is that I use easychild function, it returns me a segmentation fault

struct xml_node* myxml_node = xml_easy_child(root,"Functions","Function","as", 0);
struct xml_string* myxml = xml_node_content(myxml_node);

maybe xml_easy_child not support when i have 2 node with same name (in mu example i have 2 "function" node)

	uint8_t* source = ""
		"<Root>"
            "<Functions>"
            
            "<Function>"
            "<is>testas</is>"
            "</Function>"
            
            "<Function>"
            "<as>testas</as>"
            "<os>testos</os>"
            "</Function>"

            "</Functions>"
		"</Root>"
	;

if i have 1 node "function", it's work without error, but i have many "function" node
it's work if i remove

            "<Function>"
            "<is>testas</is>"
            "</Function>

like this:

	uint8_t* source = ""
		"<Root>"
            "<Functions>"
            
            "<Function>"
            "<as>testas</as>"
            "<os>testos</os>"
            "</Function>"

            "</Functions>"
		"</Root>"
	;

do you have a solution?

@ooxi
Copy link
Owner

ooxi commented Mar 5, 2018

xml_easy_child should not segfault, thanks for spotting this!

The solution to your problem will most likely be a recursive search. Let me hack something together for your :-)

@huitre39
Copy link
Author

huitre39 commented Mar 5, 2018

ok,Thank you very much for your help

ooxi added a commit that referenced this issue Mar 5, 2018
@ooxi
Copy link
Owner

ooxi commented Mar 5, 2018

Take a look at test-huitre39.c. The function get_node_by_name recursively searches all elements below a node (depth-first) for a node with the given name.

This is you you use this function in order to find your us node:

struct xml_node* us = get_node_by_name(root, "us");
if (us) {
	printf("Found `us!'\n");
} else {
	printf("Did not find `us' :-(\n");
}

I hope this solves your problem :-)

@huitre39
Copy link
Author

huitre39 commented Mar 5, 2018

it's work properly, thank you very much !!!
i think good idea to integrate get_node_by_name in library

@huitre39 huitre39 closed this as completed Mar 5, 2018
@ooxi
Copy link
Owner

ooxi commented Mar 5, 2018

i think good idea to integrate get_node_by_name in library

It's quite a special usecase and since it just returns the first element instead of all, I'm hesitant. I will however merge the test so it's ensured that the function will work properly in the future, too.

Glad to help and thank you for using xml.c!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants