Poco::XML::Node* XMLConfiguration::findElement(int index, Poco::XML::Node* pNode, bool create)
{
	Poco::XML::Node* pRefNode = pNode;
	if (index > 0)
	{
		pNode = pNode->nextSibling();
		while (pNode)
		{
			if (pNode->nodeName() == pRefNode->nodeName())
			{
				if (--index == 0) break;
			}
			pNode = pNode->nextSibling();
		}
	}
	if (!pNode && create)
	{
		if (index == 1)
		{
			Poco::AutoPtr<Poco::XML::Element> pElem = pRefNode->ownerDocument()->createElement(pRefNode->nodeName());
			pRefNode->parentNode()->appendChild(pElem);
			return pElem;
		}
		else throw Poco::InvalidArgumentException("Element index out of range.");
	}
	return pNode;
}
void XMLConfiguration::removeRaw(const std::string& key)
{
	Poco::XML::Node* pNode = findNode(key);

	if (pNode)
	{
		if (pNode->nodeType() == Poco::XML::Node::ELEMENT_NODE)
		{
			Poco::XML::Node* pParent = pNode->parentNode();
			if (pParent)
			{
				pParent->removeChild(pNode);
			}
		}
		else if (pNode->nodeType() == Poco::XML::Node::ATTRIBUTE_NODE)
		{
			Poco::XML::Attr* pAttr = dynamic_cast<Poco::XML::Attr*>(pNode);
			Poco::XML::Element* pOwner = pAttr->ownerElement();
			if (pOwner)
			{
				pOwner->removeAttributeNode(pAttr);
			}
		}
	}
}
Esempio n. 3
0
bool ofXml::remove(const string& path) // works for both attributes and tags
{
    Poco::XML::Node *node;
    if(element) {
        node = element->getNodeByPath(path);
    } else {
        ofLogWarning("ofXml") << "remove(): no element set yet";
        return false;
    }
    
    if(node) {
        Poco::XML::Node *n = node->parentNode()->removeChild(node);
        n->release();
        return true;
    }
    return false;
}
Esempio n. 4
0
/**
 * Build dictionary {string : string } of all tags in the dictionary
 * Composed tags: / replaced by _
 *
 */
std::map<std::string, std::string>
XmlHandler::get_metadata(const std::string &tag_to_ignore) {
  std::map<std::string, std::string> metadata;

  Poco::XML::NodeIterator it(pDoc, Poco::XML::NodeFilter::SHOW_ELEMENT);
  Poco::XML::Node *pNode = it.nextNode();

  while (pNode) {
    if (pNode->childNodes()->length() == 1 &&
        pNode->nodeName() != tag_to_ignore) {
      std::string key =
          pNode->parentNode()->nodeName() + "/" + pNode->nodeName();
      std::string value = pNode->innerText();
      metadata.emplace(key, value);
    }
    pNode = it.nextNode();
  }
  return metadata;
}