# Cinder

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

# include/cinder/Xml.h

Go to the documentation of this file.

    00001 /*
    00002  Copyright (c) 2010, The Barbarian Group
    00003  All rights reserved.
    00004
    00005  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
    00006  the following conditions are met:
    00007
    00008     * Redistributions of source code must retain the above copyright notice, this list of conditions and
    00009     the following disclaimer.
    00010     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
    00011     the following disclaimer in the documentation and/or other materials provided with the distribution.
    00012
    00013  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
    00014  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
    00015  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
    00016  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
    00017  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    00018  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    00019  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    00020  POSSIBILITY OF SUCH DAMAGE.
    00021 */
    00022
    00023 #pragma once
    00024
    00025 #include <map>
    00026 #include <string>
    00027 #include <vector>
    00028 #include "cinder/Cinder.h"
    00029 #include "cinder/Stream.h"
    00030 #include "cinder/DataSource.h"
    00031
    00032 class TiXmlDocument;
    00033 class TiXmlElement;
    00034
    00035 namespace cinder {
    00036
    00037 class XmlElement;
    00038
    00039 class XmlDocument {
    00040   public:
    00041     XmlDocument();
    00042     XmlDocument( const std::string &filePath );
    00043     XmlDocument( std::istream& is );
    00044     XmlDocument( shared_ptr<cinder::IStream> is );
    00045     XmlDocument( DataSourceRef dataSource );
    00046
    00047     //XmlDocument( const char * str ) : tiDoc( str ) {}
    00048     //XmlDocument( const std::string& str ) : tiDoc( str ) {}
    00049
    00050     friend std::istream& operator>>( std::istream& in, XmlDocument &base );
    00051     friend std::ostream& operator<<( std::ostream& out, const XmlDocument &base );
    00052
    00053     XmlElement rootNode() const;
    00054
    00055     std::vector<XmlElement> xpath( const char *aXpathExpr ) const;
    00056
    00057   private:
    00058     shared_ptr<TiXmlDocument>   tiDoc;
    00059 };
    00060
    00061 class XmlElement {
    00062   public:
    00063     XmlElement() : tiNode( 0 ) {}
    00064     XmlElement( TiXmlElement * aTiNode ) : tiNode( aTiNode ) {}
    00065
    00066     std::string name() const;
    00067
    00068     bool operator==( const XmlElement &rhs ) const
    00069     {
    00070         return ( tiNode == rhs.tiNode );
    00071     }
    00072
    00073     bool operator!=( const XmlElement &rhs ) const
    00074     {
    00075         return ! ( *this == rhs );
    00076     }
    00077
    00078     bool hasChildren() const;
    00079
    00080     std::vector<XmlElement> xpath( const char *aXpathExpr ) const;
    00081     XmlElement findChild( const char *aNodeName ) const;
    00082
    00083     XmlElement parent() const;
    00084     std::vector<XmlElement> children() const;
    00085
    00086     XmlElement firstChild() const;
    00087     XmlElement lastChild() const;
    00088
    00089     XmlElement nextSibling() const;
    00090     XmlElement previousSibling() const;
    00091
    00092     std::string value() const;
    00093
    00094     std::map<std::string,std::string> attributes() const;
    00095
    00096     void setAttribute( const char *attrName, const char *attrValue );
    00097     void addChild( const char *name, const char *value = "" );
    00098
    00099     friend std::ostream& operator<<( std::ostream &out, const XmlElement &base );
    00100   private:
    00101     TiXmlElement *tiNode;
    00102 };
    00103
    00104 } // namespace cinder