// AddNamespace bool COpcXmlDocument::AddNamespace(const COpcString& cPrefix, const COpcString& cNamespace) { // check for a valid root element. COpcXmlElement cElement(GetRoot()); if (cElement == NULL) { return false; } // check for an invalid prefix if (cPrefix.IsEmpty()) { return false; } // construct the new namespace attribute name. COpcString cAttributeName; cAttributeName += OPCXML_NAMESPACE_ATTRIBUTE; cAttributeName += ":"; cAttributeName += cPrefix; // add or update the namespace string. cElement.SetAttribute(cAttributeName, cNamespace); return true; }
// ----------------------------------------------------------------------------- // Create a new element and set to child of current stack element. // New element is placed on top of element stack. // ----------------------------------------------------------------------------- void SimXMLDocument::pushNewElement(const char* rName) { TiXmlElement cElement( rName ); TiXmlElement* pStackTop = 0; if(m_paNode.empty()) { pStackTop = dynamic_cast<TiXmlElement*> (m_qDocument->InsertEndChild( cElement ) ); } else { const int iFinalElement = m_paNode.size() - 1; TiXmlElement* pNode = m_paNode[iFinalElement]; if(!pNode) { return; } pStackTop = dynamic_cast<TiXmlElement*> (pNode->InsertEndChild( cElement )); } if(!pStackTop) { return; } m_paNode.push_back(pStackTop); }
// GetDefaultNamespace COpcString COpcXmlDocument::GetDefaultNamespace() { // check for a valid root element. COpcXmlElement cElement(GetRoot()); if (cElement == NULL) { return (LPCWSTR)NULL; } return cElement.GetNamespace(); }
// FindElements UINT COpcXmlDocument::FindElements(const COpcString& cXPath, COpcXmlElementList& cElements) { cElements.RemoveAll(); // check if document exists. if (m_ipDocument == NULL) { return 0; } // search for matching nodes. IXMLDOMNodeList* ipNodes = NULL; BSTR bstrQuery = SysAllocString(cXPath); HRESULT hResult = m_ipDocument->selectNodes(bstrQuery, &ipNodes); SysFreeString(bstrQuery); if (FAILED(hResult)) { return 0; } // add found nodes to element list. do { IXMLDOMNode* ipNode = NULL; hResult = ipNodes->nextNode(&ipNode); if (hResult != S_OK || ipNode == NULL) { break; } COpcXmlElement cElement(ipNode); ipNode->Release(); if (cElement != NULL) { cElements.Append(cElement); } } while (SUCCEEDED(hResult)); if (ipNodes != NULL) { ipNodes->Release(); } // return the number of elements found. return cElements.GetSize(); }
// ----------------------------------------------------------------------------- // Create a new element and set to child of current stack element. // New element is placed on top of element stack. // ----------------------------------------------------------------------------- void SimXMLDocument::addNewElement(const char* rName) { TiXmlElement cElement( rName ); TiXmlElement* pStackTop = 0; if(m_paNode.empty()) { pStackTop = dynamic_cast<TiXmlElement*> (m_qDocument->InsertEndChild( cElement )); if(!pStackTop) { return; } m_paNode.push_back(pStackTop); return; } const int iParentElement = m_paNode.size() - 2; if(iParentElement < 0) { pStackTop = dynamic_cast<TiXmlElement*> (m_qDocument->InsertEndChild( cElement )); if(!pStackTop) { return; } m_paNode.push_back(pStackTop); return; } else { TiXmlElement* pNode = m_paNode[iParentElement]; if(!pNode) { return; } pStackTop = dynamic_cast<TiXmlElement*> (pNode->InsertEndChild( cElement )); if(!pStackTop) { return; } // Overwrite top stack position. const int iFinalElement = m_paNode.size() - 1; m_paNode[iFinalElement] = pStackTop; //pNode = pStackTop; } }
// GetNamespaces void COpcXmlDocument::GetNamespaces(COpcStringMap& cNamespaces) { // clear the current set. cNamespaces.RemoveAll(); // check for a valid root element. COpcXmlElement cElement(GetRoot()); if (cElement == NULL) { return; } // add the namespace for the root element. COpcString cPrefix = cElement.GetPrefix(); if (!cPrefix.IsEmpty()) { cNamespaces[cPrefix] = cElement.GetNamespace(); } // fetch the attributes from the root element. COpcXmlAttributeList cAttributes; if (cElement.GetAttributes(cAttributes) > 0) { for (UINT ii = 0; ii < cAttributes.GetSize(); ii++) { if (cAttributes[ii].GetPrefix() == OPCXML_NAMESPACE_ATTRIBUTE) { COpcString cName = cAttributes[ii].GetQualifiedName().GetName(); // don't add the default namespace. if (!cName.IsEmpty()) { cNamespaces[cName] = cAttributes[ii].GetValue(); } } } } }