Created
June 30, 2024 03:37
-
-
Save TomKing062/1c944e4f3661140bad8e90e3620a7387 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <libxml/parser.h> | |
| // A function to print the name and ID of a NVItem node | |
| void print_NVItem(xmlNode *node) { | |
| xmlNode *cur; | |
| xmlChar *name; | |
| xmlChar *id; | |
| // Get the name attribute of the node | |
| name = xmlGetProp(node, (const xmlChar *)"name"); | |
| for (cur = node->children; cur != NULL; cur = cur->next) { | |
| if(xmlStrcmp(cur->name, (const xmlChar *)"ID") == 0) { | |
| id = xmlNodeGetContent(cur); | |
| printf("name = %s, ID = %s\n", name, id); | |
| } | |
| } | |
| // Print the name and ID | |
| // Free the memory | |
| xmlFree(name); | |
| xmlFree(id); | |
| } | |
| // A function to traverse the XML tree recursively | |
| void traverse(xmlNode *node) { | |
| xmlNode *cur; | |
| // Check if the node is a NVItem element | |
| if (node->type == XML_ELEMENT_NODE && xmlStrEqual(node->name, (const xmlChar *)"NVItem")) { | |
| // Print the name and ID of the NVItem | |
| print_NVItem(node); | |
| } | |
| // Loop through the children of the node | |
| for (cur = node->children; cur != NULL; cur = cur->next) { | |
| // Traverse the child node recursively | |
| traverse(cur); | |
| } | |
| } | |
| int main(int argc, char **argv) { | |
| xmlDoc *doc; | |
| xmlNode *root; | |
| // Parse the XML file | |
| doc = xmlReadFile(argv[1], NULL, 0); | |
| if (doc == NULL) { | |
| printf("Failed to parse the XML file\n"); | |
| return 1; | |
| } | |
| // Get the root element | |
| root = xmlDocGetRootElement(doc); | |
| // Traverse the XML tree from the root | |
| traverse(root); | |
| // Free the document | |
| xmlFreeDoc(doc); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment