Source code of Windows XP (NT5)
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.

84 lines
1.8 KiB

  1. /*
  2. Copyright 1999 Microsoft Corporation
  3. Simplified XML "DOM"
  4. Walter Smith (wsmith)
  5. */
  6. #pragma once
  7. using namespace std;
  8. struct wstring_less {
  9. bool operator()(const wstring& a, const wstring& b) const
  10. {
  11. return (lstrcmpW(a.c_str(), b.c_str()) < 0);
  12. }
  13. };
  14. struct SimpleXMLNode {
  15. wstring tag;
  16. wstring text;
  17. typedef map<wstring, wstring, wstring_less> attrs_t;
  18. attrs_t attrs;
  19. typedef vector< auto_ptr<SimpleXMLNode> > children_t;
  20. children_t children;
  21. void SetAttribute(wstring& name, wstring& value)
  22. {
  23. pair<attrs_t::iterator, bool> pair = attrs.insert(attrs_t::value_type(name, value));
  24. if (!pair.second)
  25. (*pair.first).second = value;
  26. }
  27. const wstring* GetAttribute(wstring& name) const
  28. {
  29. attrs_t::const_iterator it = attrs.find(name);
  30. if (it == attrs.end())
  31. return NULL;
  32. else
  33. return &(*it).second;
  34. }
  35. SimpleXMLNode* AppendChild(wstring& tag)
  36. {
  37. auto_ptr<SimpleXMLNode> pNode(new SimpleXMLNode);
  38. pNode->tag = tag;
  39. children.push_back(pNode);
  40. return pNode.get();
  41. }
  42. const SimpleXMLNode* GetChildByTag(wstring& tag) const
  43. {
  44. for (children_t::const_iterator it = children.begin();
  45. it != children.end();
  46. it++) {
  47. if (lstrcmpW((*it)->tag.c_str(), tag.c_str()) == 0)
  48. return (*it).get();
  49. }
  50. return NULL;
  51. }
  52. };
  53. class SimpleXMLDocument {
  54. public:
  55. SimpleXMLDocument()
  56. : m_pTopNode(new SimpleXMLNode)
  57. {
  58. }
  59. ~SimpleXMLDocument() { }
  60. void ParseFile(LPCTSTR szFilename);
  61. void SaveFile(LPCTSTR szFilename) const;
  62. SimpleXMLNode* GetTopNode() const
  63. {
  64. return m_pTopNode.get();
  65. }
  66. private:
  67. auto_ptr<SimpleXMLNode> m_pTopNode;
  68. };