// XmlElement.h #ifndef XmlElement_H #define XmlElement_H // David Adams // July 2002 // // A simple class to hold data describing an XML element. // An element can hold the following: // other elements (managed locally) // string attributes // a string identifier // string identifiers as references to other elements // All but the first are indexed by name. // // This a partial implementation of the XML standard and does // not provide any capability for parsing or writing files or // for checking consistency with DTD or style sheets. // // Package dataset_xml provides means to read elements from // or write elements to XML files. #include #include #include #include class XmlElement { public: // typedefs typedef std::string String; typedef std::vector ElementList; typedef std::vector StringList; typedef std::map AttMap; private : // data // Error count. // Incremented for each error. mutable int m_err; // Name specifying the type of the element. String m_name; // Identity. // Value is "" if undefined. String m_id; // Child elements. ElementList m_eles; // References to other elements. StringList m_ref_names; AttMap m_refs; // Attributes. StringList m_att_names; AttMap m_atts; // Parsed character data (text). String m_pcdata; public: // Constructors, assignment and destructor. // Constructor. explicit XmlElement(String name =""); // Copy. XmlElement(const XmlElement& rhs); // Assignment. XmlElement& operator=(const XmlElement& rhs); // Destructor. ~XmlElement(); public: // Modifiers // All return 0 for success. // Set the identity. int set_id(String id); // Add a child element. int add_child(const XmlElement*); // Add a reference to another element. int add_reference(String name, String ref); // Add an attribute. int add_attribute(String name, String att); int add_attribute_as_bool(String name, bool att); int add_attribute_as_int(String name, long att); int add_attribute_as_unsigned_int(String name, unsigned long att); // Set the parsed character data. int set_pcdata(String txt); public: // Const methods. // Validity. // Requires nonblank name. bool is_valid() const; // Return the error count; int error_count() const { return m_err; } // Fetch the name. String name() const { return m_name; } // Fetch the identity. String id() const { return m_id; }; // Fetch the list of children. const ElementList& children() const { return m_eles; } // Find all children with a specified name. ElementList children(String name) const; // Return the single child with the specified name. // Returns null if there are zero or more than one matches. const XmlElement* single_child(String name) const; // Return the number of children with a given name. ElementList::size_type has_children(String name) const; // Fetch the references. const StringList& reference_names() const { return m_ref_names; } bool has_reference(String name) const; String reference(String Name) const; // Fetch the attributes. const StringList& attribute_names() const { return m_att_names; } bool has_attribute(String name) const; String attribute(String name) const; bool attribute_as_bool(String name) const; long attribute_as_int(String name) const; unsigned long attribute_as_unsigned_int(String name) const; // Fetch the parsed character data (text). String pcdata() const; // Write to xml text. // If indent = "NOCR", then there is no formattiong with indents // or carriage returns. std::string to_xml_text(std::string indent ="") const; // Size of managed memory in bytes. size_t memsize() const; }; // Output stream. std::ostream& operator<<(std::ostream& lhs, const XmlElement& rhs); #endif