// Extract coordinates from a "(y,x)" string int EpiphanyXML::ExtractCoords(XMLElement* element, unsigned* yid, unsigned* xid) { int valuelen; // Given the attribute's name, find it in the element. XMLVariable* v = element->FindVariableZ("id"); if ((!v) || ((valuelen = v->GetValue(0, 0)) == 0)) { return -1; } else { int valuelen = v->GetValue(0, 0); char buf[valuelen+1]; v->GetValue(buf); return (sscanf(buf, "(%u,%u)", yid, xid) == 2) ? 0: -1; } }
// Extract a string value from an attribute int EpiphanyXML::ExtractAttr(XMLElement* element, char** value, char* attr) { // Given the attribute's name, find it in the element. XMLVariable* v = element->FindVariableZ(attr); // If the attribute could not be found, set *value to NULL and return an error. if (!v) { *value = NULL; return -1; } // Find the length of the attribute int valuelen = v->GetValue(0, 0); // Create a new string big enough to hold the attribute's value. *value = new char[valuelen+1]; // Copy the value of the attribute to the provided string pointer. v->GetValue(*value); return 0; }
// Extract an unsigned value from an attribute int EpiphanyXML::ExtractAttr(XMLElement* element, unsigned* uns_p, char* name) { char buf[128]; XMLVariable* v; v = element->FindVariableZ(name); // If the variable could not be found, return an error. if (!v) { return -1; } // Else, convert it to an unsigned. v->GetValue(buf); *uns_p = strtoul(buf, NULL, 0); return 0; }
int OpenXML(const char* XMLFile, char *attribute) { XML* xml = new XML(XMLFile); XMLElement* root = xml->GetRootElement(); unsigned int nodeCount = root->GetChildrenNum(); XMLElement** node = root->GetChildren(); for (unsigned int i = 0; i < nodeCount; i++) { unsigned int leafCount = node[i]->GetChildrenNum(); XMLElement** leaf = node[i]->GetChildren(); for (unsigned int j = 0; j < leafCount; j++) { XMLVariable* Att = leaf[j]->FindVariableZ(attribute); if (Att) { char Buf[255]; Att->GetValue(Buf); printf("Attribute %s has value %s\n", attribute, Buf); } } } delete xml; return 0; }