util.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package util
  2. import (
  3. "strings"
  4. )
  5. func ParseXmlNode(data string) (string, string, string) {
  6. a, b := findString(data, "<", ">")
  7. if a == -1 {
  8. return "", "", data
  9. }
  10. return ParseXmlNameNode(data, data[a:b])
  11. }
  12. func findString(data string, start string, end string) (int, int) {
  13. index1 := strings.Index(data, start)
  14. if index1 < 0 {
  15. return -1, -1
  16. }
  17. index2 := strings.Index(data, end)
  18. if index2 < 0 {
  19. return -1, -1
  20. }
  21. return index1 + len(start), index2
  22. }
  23. func ParseXmlNameNode(data string, name string) (string, string, string) {
  24. start := "<" + name + ">"
  25. end := "</" + name + ">"
  26. a, b := findString(data, start, end)
  27. if a == -1 {
  28. return "", "", data
  29. }
  30. a -= len(start)
  31. b += len(end)
  32. return name, data[a:b], data[b:]
  33. }
  34. func ParseXmlNameData(data string, name []string) string {
  35. var n string
  36. for i := 0; i < len(name); i++ {
  37. n, data, _ = ParseXmlNameNode(data, name[i])
  38. if n == "" {
  39. return ""
  40. }
  41. }
  42. a, b := findString(data, "<" + n + ">", "</" + n + ">")
  43. return data[a:b]
  44. }