Logo
~Sockets~
~Examples~
~Contact~


XmlNode Class Reference

#include <XmlNode.h>

List of all members.


Public Member Functions

 XmlNode ()
 XmlNode (XmlDocument &)
 XmlNode (XmlDocument &, const std::string &nodepath)
 XmlNode (const XmlNode &, const std::string &nodepath)
 XmlNode (xmlDocPtr, xmlNodePtr ptr)
 XmlNode (XmlDocument &, xmlNodePtr ptr)
 ~XmlNode ()
 operator xmlNodePtr () const
XmlNode operator[] (const std::string &name) const
 operator std::string () const
void operator++ () const
xmlDocPtr GetDocument () const
bool Valid () const
xmlNodePtr GetRootElement () const
 Assign document root element to 'current node'.
std::string GetProperty (const std::string &propname) const
 Return property 'propname' for 'current node'.
bool PropertyExists (const std::string &propname) const
xmlNodePtr GetChildrenNode () const
 Assign first child node of 'current node' to 'current node'.
xmlNodePtr GetNextNode () const
 Assign next non-blank (xmlIsBlankNode) node of 'current node' to 'current node'.
const std::string & GetNodeName () const
 Return nodename of 'current node'.
const std::string & GetContent () const
 Return content of 'current node'.
void SetCurrent (xmlNodePtr p) const
 Sets 'current node' that the following methods act on: GetProperty GetNodeName GetContent GetNodeNs GetNodeNsPrefix GetNodeNsHref.
xmlNsPtr GetNodeNs () const
const std::string & GetNodeNsPrefix () const
const std::string & GetNodeNsHref () const
xmlNodePtr GetFirstElement (const std::string &name) const
 Search element named 'name' in Root element child nodes.
xmlNodePtr GetFirstElement (xmlNodePtr parent, const std::string &name) const
 Search element named 'name' in 'parent' element child nodes.
xmlNodePtr GetNextElement (xmlNodePtr node, const std::string &name) const
 Find next element named 'name' on same level as 'node'.
bool Exists (const std::string &name) const
 Check that child element with name 'name' exists.
std::map< std::string,
std::string > 
GetNsMap () const
 [prefix] = href
std::map< std::string,
std::string > 
GetNsMapRe () const
 [href] = prefix
const std::string FindProperty (const std::string &propname, bool climb=false) const

Private Attributes

xmlDocPtr m_doc
xmlNodePtr m_current
std::string m_current_name
std::string m_ns_prefix
std::string m_ns_href
std::string m_content
std::string m_lookup_name

Detailed Description

Definition at line 51 of file XmlNode.h.


Constructor & Destructor Documentation

XmlNode::XmlNode (  )  [inline]

Definition at line 54 of file XmlNode.h.

Referenced by operator[]().

00054 {}

XmlNode::XmlNode ( XmlDocument doc  ) 

Definition at line 45 of file XmlNode.cpp.

00046 : m_doc(doc)
00047 , m_current( GetRootElement() )
00048 {
00049 }

XmlNode::XmlNode ( XmlDocument doc,
const std::string &  nodepath 
)

Definition at line 52 of file XmlNode.cpp.

References GetFirstElement(), and SetCurrent().

00053 : m_doc(doc)
00054 , m_current( GetRootElement() )
00055 {
00056         xmlNodePtr p = GetFirstElement( nodepath );
00057         SetCurrent( p );
00058 }

XmlNode::XmlNode ( const XmlNode node,
const std::string &  nodepath 
)

Definition at line 61 of file XmlNode.cpp.

References GetFirstElement(), and SetCurrent().

00062 : m_doc( node.GetDocument() )
00063 , m_current( node )
00064 {
00065         xmlNodePtr p = GetFirstElement( node, nodepath );
00066         SetCurrent( p );
00067 }

XmlNode::XmlNode ( xmlDocPtr  doc,
xmlNodePtr  ptr 
)

Definition at line 77 of file XmlNode.cpp.

00078 : m_doc(doc)
00079 , m_current( ptr )
00080 {
00081 }

XmlNode::XmlNode ( XmlDocument doc,
xmlNodePtr  ptr 
)

Definition at line 70 of file XmlNode.cpp.

00071 : m_doc(doc)
00072 , m_current( ptr )
00073 {
00074 }

XmlNode::~XmlNode (  ) 

Definition at line 84 of file XmlNode.cpp.

00085 {
00086 }


Member Function Documentation

XmlNode::operator xmlNodePtr (  )  const

Definition at line 257 of file XmlNode.cpp.

References m_current.

00258 {
00259         return m_current;
00260 }

XmlNode XmlNode::operator[] ( const std::string &  name  )  const

Definition at line 263 of file XmlNode.cpp.

References GetFirstElement(), m_current, m_doc, SetCurrent(), and XmlNode().

00264 {
00265         xmlNodePtr p0 = m_current;
00266         xmlNodePtr p = GetFirstElement( m_current, name );
00267         SetCurrent( p0 );
00268         if (p)
00269                 return XmlNode( m_doc, p );
00270         throw XmlException("Didn't find node: " + name);
00271 }

XmlNode::operator std::string (  )  const [inline]

Definition at line 64 of file XmlNode.h.

References GetContent().

00064 { return GetContent(); }

void XmlNode::operator++ (  )  const

Definition at line 285 of file XmlNode.cpp.

References GetNextNode(), GetNodeName(), m_current, and m_lookup_name.

00286 {
00287         GetNextNode();
00288         while (m_current)
00289         {
00290                 if (m_lookup_name == GetNodeName())
00291                 {
00292                         return;
00293                 }
00294                 GetNextNode();
00295         }
00296 }

xmlDocPtr XmlNode::GetDocument (  )  const [inline]

Definition at line 67 of file XmlNode.h.

References m_doc.

00067 { return m_doc; }

bool XmlNode::Valid (  )  const [inline]

Definition at line 69 of file XmlNode.h.

References m_current.

00069 { return m_current ? true : false; }

xmlNodePtr XmlNode::GetRootElement (  )  const

Assign document root element to 'current node'.

Returns:
Current Node

Definition at line 89 of file XmlNode.cpp.

References m_current, and m_doc.

Referenced by GetFirstElement().

00090 {
00091         m_current = xmlDocGetRootElement(m_doc);
00092         return m_current;
00093 }

std::string XmlNode::GetProperty ( const std::string &  propname  )  const

Return property 'propname' for 'current node'.

Definition at line 96 of file XmlNode.cpp.

References Utility::FromUtf8(), GetNodeName(), and m_current.

Referenced by FindProperty().

00097 {
00098         xmlChar *p = m_current ? xmlGetProp(m_current, (const xmlChar *) name.c_str() ) : NULL;
00099         if (!p)
00100         {
00101                 throw XmlException( "Property '" + name + "' not found in node: " + GetNodeName() );
00102         }
00103         std::string str = (char *)p;
00104         xmlFree(p);
00105         return Utility::FromUtf8(str);
00106 }

bool XmlNode::PropertyExists ( const std::string &  propname  )  const

Definition at line 109 of file XmlNode.cpp.

References m_current.

Referenced by FindProperty().

00110 {
00111         xmlChar *p = m_current ? xmlGetProp(m_current, (const xmlChar *) name.c_str() ) : NULL;
00112         if (!p)
00113         {
00114                 return false;
00115         }
00116         xmlFree(p);
00117         return true;
00118 }

xmlNodePtr XmlNode::GetChildrenNode (  )  const

Assign first child node of 'current node' to 'current node'.

Returns:
Current Node

Definition at line 121 of file XmlNode.cpp.

References m_current.

Referenced by GetContent(), and GetFirstElement().

00122 {
00123         m_current = m_current ? m_current -> xmlChildrenNode : NULL;
00124         return m_current;
00125 }

xmlNodePtr XmlNode::GetNextNode (  )  const

Assign next non-blank (xmlIsBlankNode) node of 'current node' to 'current node'.

Returns:
Current Node

Definition at line 128 of file XmlNode.cpp.

References m_current.

Referenced by GetFirstElement(), GetNextElement(), and operator++().

00129 {
00130         do
00131         {
00132                 m_current = m_current ? m_current -> next : NULL;
00133         } while (m_current && xmlIsBlankNode( m_current ));
00134         return m_current;
00135 }

const std::string & XmlNode::GetNodeName (  )  const

Return nodename of 'current node'.

Definition at line 138 of file XmlNode.cpp.

References Utility::FromUtf8(), m_current, and m_current_name.

Referenced by GetFirstElement(), GetNextElement(), GetProperty(), and operator++().

00139 {
00140         if (m_current)
00141         {
00142                 m_current_name = Utility::FromUtf8((char *)m_current -> name);
00143         }
00144         else
00145         {
00146                 m_current_name = "";
00147         }
00148         return m_current_name;
00149 }

const std::string & XmlNode::GetContent (  )  const

Return content of 'current node'.

Definition at line 152 of file XmlNode.cpp.

References Utility::FromUtf8(), GetChildrenNode(), m_content, m_current, and SetCurrent().

Referenced by operator std::string().

00153 {
00154         m_content = "";
00155         if (m_current)
00156         {
00157                 xmlNodePtr p = m_current;
00158                 xmlNodePtr p2 = GetChildrenNode();
00159                 if (p2 && p2 -> content)
00160                 {
00161                         m_content = Utility::FromUtf8((char *)p2 -> content);
00162                 }
00163                 SetCurrent(p);
00164         }
00165         return m_content;
00166 }

void XmlNode::SetCurrent ( xmlNodePtr  p  )  const [inline]

Sets 'current node' that the following methods act on: GetProperty GetNodeName GetContent GetNodeNs GetNodeNsPrefix GetNodeNsHref.

Definition at line 100 of file XmlNode.h.

References m_current.

Referenced by Exists(), GetContent(), GetFirstElement(), GetNextElement(), operator[](), and XmlNode().

00100 { m_current = p; }

xmlNsPtr XmlNode::GetNodeNs (  )  const

Returns:
Namespace of 'current node'.

Definition at line 169 of file XmlNode.cpp.

References m_current.

00170 {
00171         if (m_current)
00172                 return m_current -> ns;
00173         return NULL;
00174 }

const std::string & XmlNode::GetNodeNsPrefix (  )  const

Returns:
Namespace prefix of 'current node'.

Definition at line 177 of file XmlNode.cpp.

References Utility::FromUtf8(), m_current, and m_ns_prefix.

00178 {
00179         if (m_current && m_current -> ns && m_current -> ns -> prefix)
00180         {
00181                 m_ns_prefix = Utility::FromUtf8((char *)m_current -> ns -> prefix);
00182         }
00183         else
00184         {
00185                 m_ns_prefix = "";
00186         }
00187         return m_ns_prefix;
00188 }

const std::string & XmlNode::GetNodeNsHref (  )  const

Returns:
Namespace href of 'current node'.

Definition at line 191 of file XmlNode.cpp.

References Utility::FromUtf8(), m_current, and m_ns_href.

00192 {
00193         if (m_current && m_current -> ns && m_current -> ns -> href)
00194         {
00195                 m_ns_href = Utility::FromUtf8((char *)m_current -> ns -> href);
00196         }
00197         else
00198         {
00199                 m_ns_href = "";
00200         }
00201         return m_ns_href;
00202 }

xmlNodePtr XmlNode::GetFirstElement ( const std::string &  name  )  const

Search element named 'name' in Root element child nodes.

Set 'current node' to Root element.

Definition at line 205 of file XmlNode.cpp.

References GetChildrenNode(), GetNextNode(), GetNodeName(), GetRootElement(), and m_lookup_name.

Referenced by Exists(), operator[](), and XmlNode().

00206 {
00207         if (m_lookup_name.empty())
00208                 m_lookup_name = name;
00209         GetRootElement();
00210         xmlNodePtr p = GetChildrenNode();
00211         while (p)
00212         {
00213                 if (name == GetNodeName())
00214                 {
00215                         return p;
00216                 }
00217                 p = GetNextNode();
00218         }
00219         return NULL;
00220 }

xmlNodePtr XmlNode::GetFirstElement ( xmlNodePtr  parent,
const std::string &  name 
) const

Search element named 'name' in 'parent' element child nodes.

Set 'current node' to 'parent'.

Definition at line 223 of file XmlNode.cpp.

References GetChildrenNode(), GetNextNode(), GetNodeName(), m_lookup_name, and SetCurrent().

00224 {
00225         if (m_lookup_name.empty())
00226                 m_lookup_name = name;
00227         SetCurrent(base);
00228         xmlNodePtr p = GetChildrenNode();
00229         while (p)
00230         {
00231                 if (name == GetNodeName())
00232                 {
00233                         return p;
00234                 }
00235                 p = GetNextNode();
00236         }
00237         return NULL;
00238 }

xmlNodePtr XmlNode::GetNextElement ( xmlNodePtr  node,
const std::string &  name 
) const

Find next element named 'name' on same level as 'node'.

Set 'current node' to 'p'.

Definition at line 241 of file XmlNode.cpp.

References GetNextNode(), GetNodeName(), and SetCurrent().

00242 {
00243         SetCurrent(p);
00244         p = GetNextNode();
00245         while (p)
00246         {
00247                 if (name == GetNodeName())
00248                 {
00249                         return p;
00250                 }
00251                 p = GetNextNode();
00252         }
00253         return NULL;
00254 }

bool XmlNode::Exists ( const std::string &  name  )  const

Check that child element with name 'name' exists.

Definition at line 274 of file XmlNode.cpp.

References GetFirstElement(), m_current, and SetCurrent().

00275 {
00276         xmlNodePtr p0 = m_current;
00277         xmlNodePtr p = GetFirstElement( m_current, name );
00278         SetCurrent( p0 );
00279         if (p)
00280                 return true;
00281         return false;
00282 }

std::map< std::string, std::string > XmlNode::GetNsMap (  )  const

[prefix] = href

Definition at line 299 of file XmlNode.cpp.

References Utility::FromUtf8(), m_current, and m_doc.

00300 {
00301         xmlNsPtr *p = xmlGetNsList(m_doc, m_current);
00302         std::map<std::string, std::string> vec;
00303         int i = 0;
00304         while (p[i])
00305         {
00306                 std::string href = Utility::FromUtf8((char *)p[i] -> href);
00307                 std::string prefix = p[i] -> prefix ? Utility::FromUtf8((char *)p[i] -> prefix) : "";
00308                 vec[prefix] = href;
00309                 ++i;
00310         }
00311         return vec;
00312 }

std::map< std::string, std::string > XmlNode::GetNsMapRe (  )  const

[href] = prefix

Definition at line 315 of file XmlNode.cpp.

References Utility::FromUtf8(), m_current, and m_doc.

00316 {
00317         xmlNsPtr *p = xmlGetNsList(m_doc, m_current);
00318         std::map<std::string, std::string> vec;
00319         int i = 0;
00320         while (p[i])
00321         {
00322                 std::string href = Utility::FromUtf8((char *)p[i] -> href);
00323                 std::string prefix = p[i] -> prefix ? Utility::FromUtf8((char *)p[i] -> prefix) : "";
00324                 vec[href] = prefix;
00325                 if (!p[i] -> next)
00326                         break;
00327                 ++i;
00328         }
00329         return vec;
00330 }

const std::string XmlNode::FindProperty ( const std::string &  propname,
bool  climb = false 
) const

Definition at line 333 of file XmlNode.cpp.

References GetProperty(), m_current, and PropertyExists().

00334 {
00335         while (m_current)
00336         {
00337                 if (PropertyExists(propname))
00338                 {
00339                         return GetProperty(propname);
00340                 }
00341                 if (!climb)
00342                 {
00343                         break;
00344                 }
00345                 m_current = m_current -> parent;
00346         }
00347         return "";
00348 }


Member Data Documentation

xmlDocPtr XmlNode::m_doc [private]

Definition at line 133 of file XmlNode.h.

Referenced by GetDocument(), GetNsMap(), GetNsMapRe(), GetRootElement(), and operator[]().

std::string XmlNode::m_current_name [mutable, private]

Definition at line 135 of file XmlNode.h.

Referenced by GetNodeName().

std::string XmlNode::m_ns_prefix [mutable, private]

Definition at line 136 of file XmlNode.h.

Referenced by GetNodeNsPrefix().

std::string XmlNode::m_ns_href [mutable, private]

Definition at line 137 of file XmlNode.h.

Referenced by GetNodeNsHref().

std::string XmlNode::m_content [mutable, private]

Definition at line 138 of file XmlNode.h.

Referenced by GetContent().

std::string XmlNode::m_lookup_name [mutable, private]

Definition at line 139 of file XmlNode.h.

Referenced by GetFirstElement(), and operator++().


The documentation for this class was generated from the following files:
Page, code, and content Copyright (C) 2007 by Anders Hedström
Generated for C++ Sockets by  doxygen 1.4.4