2 Copyright (c) 2005-2009, John Hurst
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
8 1. Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 3. The name of the author may not be used to endorse or promote products
14 derived from this software without specific prior written permission.
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 // Return true if the given string contains an XML document (or the start of one).
45 bool StringIsXML(const char* document, ui32_t len = 0);
55 typedef std::list<NVPair> AttributeList;
56 typedef AttributeList::const_iterator Attr_i;
57 typedef std::list<XMLElement*> ElementList;
58 typedef ElementList::const_iterator Elem_i;
66 KM_NO_COPY_CONSTRUCT(XMLNamespace);
70 XMLNamespace(const char* prefix, const char* name) : m_Prefix(prefix), m_Name(name) {}
73 inline const std::string& Prefix() const { return m_Prefix; }
74 inline const std::string& Name() const { return m_Name; }
80 KM_NO_COPY_CONSTRUCT(XMLElement);
84 AttributeList m_AttrList;
85 ElementList m_ChildList;
86 const XMLNamespace* m_Namespace;
87 void* m_NamespaceOwner;
93 XMLElement(const char* name);
96 inline const XMLNamespace* Namespace() const { return m_Namespace; }
97 inline void SetNamespace(const XMLNamespace* ns) { assert(ns); m_Namespace = ns; }
99 bool ParseString(const std::string& document);
102 void SetName(const char* name);
103 void SetBody(const std::string& value);
104 void AppendBody(const std::string& value);
105 void SetAttr(const char* name, const char* value);
106 XMLElement* AddChild(XMLElement* element);
107 XMLElement* AddChild(const char* name);
108 XMLElement* AddChildWithContent(const char* name, const char* value);
109 XMLElement* AddChildWithContent(const char* name, const std::string& value);
110 XMLElement* AddChildWithPrefixedContent(const char* name, const char* prefix, const char* value);
111 void AddComment(const char* value);
112 void Render(std::string&) const;
113 void RenderElement(std::string& outbuf, ui32_t depth) const;
116 inline const std::string& GetBody() const { return m_Body; }
117 inline const ElementList& GetChildren() const { return m_ChildList; }
118 inline const std::string& GetName() const { return m_Name; }
119 inline const AttributeList& GetAttributes() const { return m_AttrList; }
120 const char* GetAttrWithName(const char* name) const;
121 XMLElement* GetChildWithName(const char* name) const;
122 const ElementList& GetChildrenWithName(const char* name, ElementList& outList) const;
123 bool HasName(const char* name) const;
126 void DeleteAttributes();
127 void DeleteAttrWithName(const char* name);
128 void DeleteChildren();
129 void DeleteChild(const XMLElement* element);
130 void ForgetChild(const XMLElement* element);