code_junkie

Can you preserve leading and trailing whitespace in XML?

Nov 13th, 2011
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. Dim xml: Set xml = CreateObject("MSXML2.DOMDocument")
  2. xml.async = False
  3. xml.loadxml "<xml>1 2</xml>"
  4. wscript.echo len(xml.documentelement.text)
  5.  
  6. Dim xml: Set xml = CreateObject("MSXML2.DOMDocument")
  7. xml.async = False
  8. xml.loadxml "<xml> 2</xml>"
  9. wscript.echo len(xml.documentelement.text)
  10.  
  11. <xml> 2</xml>
  12.  
  13. Dim xml As MSXML2.DOMDocument
  14. Private Sub Form_Load()
  15. Set xml = CreateObject("MSXML2.DOMDocument")
  16. xml.async = False
  17. xml.loadxml "<xml> 2</xml>"
  18. Dim n
  19. n = Len(xml.documentelement.selectSingleNode("text()").nodeValue)
  20. wscript.echo Len(n)
  21. End Sub
  22.  
  23. wscript.echo Len(n)
  24.  
  25. <xml><![CDATA[ 2]]></xml>
  26.  
  27. % ./whitespace "<foo> </foo>"
  28. Length of " " is 1
  29.  
  30. % ./whitespace "<foo> 2</foo>"
  31. Length of " 2" is 2
  32.  
  33. % ./whitespace "<foo>1 2</foo>"
  34. Length of "1 2" is 3
  35.  
  36. #include <stdio.h>
  37. #include <string.h>
  38. #include <libxml/parser.h>
  39.  
  40. int
  41. main(int argc, char **argv)
  42. {
  43. char *xml;
  44. xmlDoc *doc;
  45. xmlNode *first_child, *node;
  46. if (argc < 2) {
  47. fprintf(stderr, "Usage: %s XML-stringn", argv[0]);
  48. return 1;
  49. }
  50. xml = argv[1];
  51. doc = xmlReadMemory(xml, strlen(xml), "my data", NULL, 0);
  52. first_child = doc->children;
  53. first_child = first_child->children; /* Skip the root */
  54. for (node = first_child; node; node = node->next) {
  55. if (node->type == XML_TEXT_NODE) {
  56. fprintf(stdout, "Length of "%s" is %in", (char *) node->content,
  57. strlen((char *) node->content));
  58. }
  59. }
  60. return 0;
  61. }
Add Comment
Please, Sign In to add comment