You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
3.1 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /******************************************************************
  2. * $Id: soap-xml.c,v 1.11 2006/07/09 16:24:19 snowdrop Exp $
  3. *
  4. * CSOAP Project: A SOAP client/server library in C
  5. * Copyright (C) 2003 Ferhat Ayaz
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Library General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Library General Public
  18. * License along with this library; if not, write to the
  19. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20. * Boston, MA 02111-1307, USA.
  21. *
  22. * Email: ayaz@jprogrammer.net
  23. ******************************************************************/
  24. #ifdef HAVE_CONFIG_H
  25. #include <config.h>
  26. #endif
  27. #include <nanohttp/nanohttp-logging.h>
  28. #include "soap-xml.h"
  29. xmlNodePtr
  30. soap_xml_get_children(xmlNodePtr param)
  31. {
  32. xmlNodePtr children;
  33. if (param == NULL)
  34. {
  35. log_error1("Invalid parameter 'param' (null)");
  36. return NULL;
  37. }
  38. children = param->xmlChildrenNode;
  39. while (children != NULL)
  40. {
  41. if (children->type != XML_ELEMENT_NODE)
  42. children = children->next;
  43. else
  44. break;
  45. }
  46. return children;
  47. }
  48. xmlNodePtr
  49. soap_xml_get_next(xmlNodePtr param)
  50. {
  51. xmlNodePtr node = param->next;
  52. while (node != NULL)
  53. {
  54. if (node->type != XML_ELEMENT_NODE)
  55. node = node->next;
  56. else
  57. break;
  58. }
  59. return node;
  60. }
  61. xmlXPathObjectPtr
  62. soap_xpath_eval(xmlDocPtr doc, const char *xpath)
  63. {
  64. xmlXPathContextPtr context;
  65. xmlXPathObjectPtr result;
  66. context = xmlXPathNewContext(doc);
  67. result = xmlXPathEvalExpression(BAD_CAST xpath, context);
  68. if (xmlXPathNodeSetIsEmpty(result->nodesetval))
  69. {
  70. /* no result */
  71. return NULL;
  72. }
  73. xmlXPathFreeContext(context);
  74. return result;
  75. }
  76. int
  77. soap_xpath_foreach(xmlDocPtr doc, const char *xpath,
  78. soap_xmlnode_callback cb, void *userdata)
  79. {
  80. int i = 0;
  81. xmlNodeSetPtr nodeset;
  82. xmlXPathObjectPtr xpathobj;
  83. xpathobj = soap_xpath_eval(doc, xpath);
  84. if (!xpathobj)
  85. return 0;
  86. nodeset = xpathobj->nodesetval;
  87. if (!nodeset)
  88. return 0;
  89. for (i = 0; i < nodeset->nodeNr; i++)
  90. {
  91. if (!cb(nodeset->nodeTab[i], userdata))
  92. break;
  93. }
  94. xmlXPathFreeObject((xmlXPathObjectPtr) nodeset);
  95. return i;
  96. }
  97. void
  98. soap_xml_doc_print(xmlDocPtr doc)
  99. {
  100. xmlBufferPtr buffer;
  101. xmlNodePtr root;
  102. if (doc == NULL)
  103. {
  104. puts("xmlDocPtr is NULL!");
  105. return;
  106. }
  107. root = xmlDocGetRootElement(doc);
  108. if (root == NULL)
  109. {
  110. puts("Empty document!");
  111. return;
  112. }
  113. buffer = xmlBufferCreate();
  114. xmlNodeDump(buffer, doc, root, 1, 0);
  115. puts((const char *) xmlBufferContent(buffer));
  116. xmlBufferFree(buffer);
  117. }
  118. char *
  119. soap_xml_get_text(xmlNodePtr node)
  120. {
  121. return (char *) xmlNodeListGetString(node->doc, node->xmlChildrenNode, 1);
  122. }