Example #1
0
void XmlReader::readAttribute(char** ipp) {
    char* ip = *ipp;

    readAttributeName(&ip, attrName);
    char quote = ip[1];
    if (*ip != '=' || (quote != '\'' && quote != '"')) error("error in tag attribute, expected ' or \"");
    ip += 2;
    readAttributeValue(&ip, attrValue, quote);
    if (*ip != quote) 
        error(string("error in tag value, expected: ") + quote);
    ++ip;

    XmlNode* current = stack.top();
    if (current->hasAttribute(attrName)) error(string("duplicate attribute: ") + attrName);
        
    current->addAttribute(attrName, attrValue);
    *ipp = ip;
}
Example #2
0
///////////////////////////////////////////////////////////////////////////////////////////////////
//parse data schema from node
//src		data node
//dst		schema node
bool XmlSchema::parseNodeStruct(XmlNode* dst, XmlNode* src)
{
	assert(dst != NULL);
	assert(src != NULL);

	NodeIterator nodeIterator;
	AttributeIterator attriIterator;

	for (XmlAttribute* attribute = src->getFirstAttribute(attriIterator);
		attribute != NULL;
		attribute = src->getNextAttribute(attriIterator))
	{
		XmlNode* structure = dst->findChild(attribute->getName());
		if (structure == NULL)
		{
			//first time show up
			structure = dst->addChild(attribute->getName());
			structure->addAttribute(ATTR_TYPE, guessType(attribute->getString()));
			structure->addAttribute(ATTR_ATTRIBUTE, T("true"));
		}
	}

	for (XmlNode* child = src->getFirstChild(nodeIterator);
		  child != NULL;
		  child = src->getNextChild(nodeIterator))
	{
		if (child->getType() != ELEMENT)
		{
			continue;
		}
		XmlNode* structure = dst->findChild(child->getName());
		if (structure == NULL)
		{
			//first time show up
			bool recursive = false;
			const XmlNode* parent = dst;
			while (parent != NULL)
			{
				if (Strcmp(parent->getName(), child->getName()) == 0)
				{
					recursive = true;
					break;
				}
				parent = parent->getParent();
			}
			structure = dst->addChild(child->getName());
			if (recursive)
			{
				structure->addAttribute(ATTR_RECURSIVE, T("true"));
			}
			else if (!child->hasChild() && !child->hasAttribute())
			{
				//simple type, must have a type attribute
				structure->addAttribute(ATTR_TYPE, guessType(child->getString()));
			}
		}
		else if (structure->findAttribute(ATTR_ATTRIBUTE) != NULL)
		{
			//child and attribute can't have same name
			return false;
		}

		XmlAttribute* multiple = structure->findAttribute(ATTR_MULTIPLE);
		if (multiple == NULL || !multiple->getBool())
		{
			NodeIterator iter;
			if (src->findFirstChild(child->getName(), iter) != NULL
				&& src->findNextChild(child->getName(), iter) != NULL)
			{
				if (multiple == NULL)
				{
					multiple = structure->addAttribute(ATTR_MULTIPLE);
				}
				multiple->setBool(true);
			}
		}

		if (!structure->findAttribute(ATTR_RECURSIVE) && (child->hasChild() || child->hasAttribute()))
		{
			parseNodeStruct(structure, child);
		}
	}

	return true;
}