[gnote] Add new functions to sharp/xml



commit 16d59887ae862fb63bf514b40fba3489d73080d2
Author: Aurimas Äernius <aurisc4 gmail com>
Date:   Sun Jan 22 15:57:06 2012 +0200

    Add new functions to sharp/xml
    
    * xml_node_xpath_find_single
    * xml_node_xpath_find_single_node
    * xml_node_get_attribute

 src/sharp/xml.cpp |   48 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/sharp/xml.hpp |   11 +++++++++++
 2 files changed, 59 insertions(+), 0 deletions(-)
---
diff --git a/src/sharp/xml.cpp b/src/sharp/xml.cpp
index ee2d750..eac6d85 100644
--- a/src/sharp/xml.cpp
+++ b/src/sharp/xml.cpp
@@ -1,6 +1,7 @@
 /*
  * gnote
  *
+ * Copyright (C) 2012 Aurimas Cernius
  * Copyright (C) 2009 Hubert Figuiere
  * 
  * Permission is hereby granted, free of charge, to any person obtaining a
@@ -64,4 +65,51 @@ namespace sharp {
     return nodes;
   }
 
+
+  std::string xml_node_xpath_find_single(const xmlNodePtr node,
+                                         const char * xpath)
+  {
+    xmlNodePtr n = xml_node_xpath_find_single_node(node, xpath);
+    if(!n) {
+      return "";
+    }
+
+    return reinterpret_cast<char*>(XML_GET_CONTENT(n));
+  }
+
+
+  xmlNodePtr xml_node_xpath_find_single_node(const xmlNodePtr node,
+                                             const char * xpath)
+  {
+    xmlXPathContext* ctxt = xmlXPathNewContext(node->doc);
+    ctxt->node = node;
+
+    xmlXPathObject* result = xmlXPathEval((const xmlChar*)xpath, ctxt);
+
+    xmlNodePtr result_node = NULL;
+    if(result && (result->type == XPATH_NODESET)) {
+      xmlNodeSetPtr nodeset = result->nodesetval;
+
+      if(nodeset && nodeset->nodeNr) {
+          result_node = nodeset->nodeTab[0];
+      }
+    }
+
+    if(result) {
+      xmlXPathFreeObject(result);
+    }
+    if(ctxt) {
+      xmlXPathFreeContext(ctxt);
+    }
+
+    return result_node;
+  }
+
+
+  std::string xml_node_get_attribute(const xmlNodePtr node,
+                                     const char * attr_name)
+  {
+    return reinterpret_cast<char*>(xmlGetProp(node, reinterpret_cast<const xmlChar*>(attr_name)));
+  }
+
 }
diff --git a/src/sharp/xml.hpp b/src/sharp/xml.hpp
index d021c8f..1ac6dfe 100644
--- a/src/sharp/xml.hpp
+++ b/src/sharp/xml.hpp
@@ -1,6 +1,7 @@
 /*
  * gnote
  *
+ * Copyright (C) 2012 Aurimas Cernius
  * Copyright (C) 2009 Hubert Figuiere
  * 
  * Permission is hereby granted, free of charge, to any person obtaining a
@@ -28,6 +29,7 @@
 #ifndef __SHARP_XML_HPP_
 #define __SHARP_XML_HPP_
 
+#include <string>
 #include <vector>
 
 #include <libxml/tree.h>
@@ -40,6 +42,15 @@ namespace sharp {
   XmlNodeSet xml_node_xpath_find(const xmlNodePtr node,
                                  const char * xpath);
 
+  std::string xml_node_xpath_find_single(const xmlNodePtr node,
+                                         const char * xpath);
+
+  xmlNodePtr xml_node_xpath_find_single_node(const xmlNodePtr node,
+                                             const char * xpath);
+
+  std::string xml_node_get_attribute(const xmlNodePtr node,
+                                     const char * attr_name);
+
 }
 
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]