xml.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * Copyright (C) 2021 Duowan Inc. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing,
  11. * software distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef __X_PACK_XML_H
  17. #define __X_PACK_XML_H
  18. #include "xml_decoder.h"
  19. #include "xml_encoder.h"
  20. #include "xpack.h"
  21. namespace xpack {
  22. class xml {
  23. public:
  24. template <class T>
  25. static void decode(const std::string &data, T &val) {
  26. XmlDecoder doc(data);
  27. doc.decode(NULL, val, NULL);
  28. }
  29. template <class T>
  30. static void decode_file(const std::string &file_name, T &val) {
  31. XmlDecoder doc(file_name, true);
  32. doc.decode(NULL, val, NULL);
  33. }
  34. template <class T>
  35. static std::string encode(const T &val, const std::string&root) {
  36. XmlEncoder doc(-1);
  37. doc.encode(root.c_str(), val, NULL);
  38. return doc.String();
  39. }
  40. template <class T>
  41. static std::string encode(const T &val, const std::string&root, int flag, int indentCount, char indentChar) {
  42. XmlEncoder doc(indentCount, indentChar);
  43. Extend ext(flag, NULL);
  44. doc.encode(root.c_str(), val, &ext);
  45. return doc.String();
  46. }
  47. };
  48. }
  49. #endif