|
#include "XmlNode.h"
#include "XmlAttribute.h"
#include "XmlNodeList.h"
CXmlNode::CXmlNode(void)
//:m_pNode(NULL)
{
}
CXmlNode::CXmlNode(xmlNode* pNode)
//:m_pNode(NULL)
{
m_sName = "";
if ( pNode != NULL )
{
m_sName.append( (char*)pNode->name );
if ( pNode != NULL )
{
CreateDocumentChildNodes( pNode );
}
}
}
CXmlNode::CXmlNode(std::string sName)
//:m_pNode(NULL)
{
m_sName = sName;
}
CXmlNode::~CXmlNode(void)
{
if ( HasChild() )
RemoveAllChilds();
if ( HasAttributes() )
RemoveAllAttributes();
}
//Operations
void CXmlNode::CreateDocumentNodes(xmlNode *pRoot )
{
xmlNode *cur_node = NULL;
xmlNode *child_node = NULL;
m_sName= "";
m_sName.append( (char*) pRoot->name );
if ( HasChild() )
RemoveAllChilds();
if ( pRoot->children != NULL )
{
for (cur_node = pRoot->children; cur_node; cur_node = cur_node->next)
{
if (cur_node->type == XML_ELEMENT_NODE)
{
CXmlNode* pCurNode = new CXmlNode(cur_node);
m_ListChilds.push_back( pCurNode );
}
}
}
}
void CXmlNode::CreateDocumentChildNodes(xmlNode *pNode)
{
xmlNode * child_node = NULL;
for( xmlAttr* x = pNode->properties; x; x = x->next )
{
if ( x->type == XML_ATTRIBUTE_NODE )
{
std::string strAtribName = ""; strAtribName.append((char*) x->name );
std::string strAtribValue = ""; strAtribValue.append( (char*) x->children->content );
CXmlAttribute *attr = new CXmlAttribute(strAtribName, strAtribValue);
m_ListAtts.push_back( attr );
}
}
if ( pNode->children!= NULL)
{
if ( pNode->children->type == XML_TEXT_NODE )
{
m_sValue = "";
m_sValue.append( (char*)pNode->children->content );
}
//if (pNode->children->next != NULL )
if(pNode != NULL)
{
//for (child_node = pNode->children->next; child_node; child_node = child_node->next)
for (child_node = pNode->children; child_node; child_node = child_node->next)
{
if (child_node->type == XML_ELEMENT_NODE)
{
CXmlNode* pCurNode = new CXmlNode(child_node);
m_ListChilds.push_back( pCurNode );
}
}
}
}
}
void CXmlNode::AddAttribute(std::string name, std::string value)
{
CXmlAttribute *attr = new CXmlAttribute(name, value);
m_ListAtts.push_back( attr );
}
void CXmlNode::AppendChild(CXmlNode *pNode)
{
if ( pNode )
m_ListChilds.push_back( pNode );
}
void CXmlNode::RemoveAllAttributes()
{
for( vector<int>::size_type i = 0; i < m_ListAtts.size(); ++i )
{
delete m_ListAtts[i];
m_ListAtts[i] = NULL;
}
}
void CXmlNode::RemoveAllChilds()
{
for( vector<int>::size_type i = 0; i < m_ListChilds.size(); ++i )
{
delete m_ListChilds[i];
m_ListChilds[i] = NULL;
}
}
CXmlNode* CXmlNode::GetLastChild()
{
CXmlNode* pCurNode = NULL;
if ( HasChild() )
pCurNode = m_ListChilds[ m_ListChilds.size() - 1 ];
return pCurNode;
}
CXmlNode* CXmlNode::GetNode(std::string strNodeName)
{
CXmlNode* pCurNode = NULL;
if ( HasChild() )
{
for( vector<int>::size_type i = 0; i < m_ListChilds.size(); ++i )
{
CXmlNode* pNode = m_ListChilds[i];
if ( pNode->GetNodeName().compare( strNodeName ) == 0 )
{
pCurNode = pNode;
break;
}
}
}
return pCurNode;
}
CXmlNodeList* CXmlNode::GetNodeList()
{
CXmlNodeList* pNodeList = NULL;
if ( HasChild() )
{
pNodeList = new CXmlNodeList();
for( vector<int>::size_type i = 0; i < m_ListChilds.size(); ++i )
{
pNodeList->AddNode( m_ListChilds[i] );
}
}
return pNodeList;
}
std::string CXmlNode::GetAttribute(std::string strAttributeName)
{
std::string sAttribValue = "";
if ( HasAttributes() )
{
for( vector<int>::size_type i = 0; i < m_ListAtts.size(); ++i )
{
CXmlAttribute* pAttrib = m_ListAtts[i];
if ( pAttrib->GetName().compare( strAttributeName ) == 0 )
{
sAttribValue = pAttrib->GetValue();
break;
}
}
}
return sAttribValue;
}
std::string CXmlNode::GetNodeName()
{
//if ( m_pNode )
//{
// m_sName = "";
// m_sName.append( (char*)m_pNode->name );
//}
return m_sName;
}
bool CXmlNode::HasChild()
{
return ( m_ListChilds.size() > 0 );
}
bool CXmlNode::HasAttributes()
{
return ( m_ListAtts.size() > 0 );
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I live in Matão, a small city in Brazil. I studied as Programmer in a College for Software Development in Database.
After finishing the College I have been working with java, c# and Computer Graphics with searches for OpenGL.