/** Convenience function for easy access to the text inside an element. Although easy * and concise, GetText() is limited compared to getting the TiXmlText child * and accessing it directly. * * If the first child of 'this' is a TiXmlText, the GetText() * returns the character string of the Text node, else null is returned. * * This is a convenient method for getting the text of simple contained text: * @verbatim * <foo>This is text</foo> * const char* str = fooElement.GetText(); * @endverbatim * * 'str' will be a pointer to "This is text". * * Note that this function can be misleading. If the element foo was created from * this XML: * @verbatim * <foo><b>This is text</b></foo> * @endverbatim * * then the value of str would be null. The first child node isn't a text node, it is * another element. From this XML: * @verbatim * <foo>This is <b>text</b></foo> * @endverbatim * GetText() will return "This is ". * * WARNING: GetText() accesses a child node - don't become confused with the * similarly named TiXmlHandle::Text() and TiXmlNode::ToText() which are * safe type casts on the referenced node. */ public string GetText() { TiXmlNode child = this.FirstChild(); if (child != null) { TiXmlText childText = child.ToText(); if (childText != null) { return(childText.Value()); } } return(null); }
/// <summary> /// Print the Element to a FILE stream. /// </summary> public override void Print(StringBuilder cfile, int depth) { //assert( cfile ); for (int i = 0; i < depth; i++) { //fprintf( cfile, " " ); cfile.Append(" "); } cfile.Append("<"); cfile.Append(value); //fprintf( cfile, "<%s", value.c_str() ); for (TiXmlAttribute attrib = attributeSet.First(); attrib != null; attrib = attrib.Next()) { cfile.Append(" "); //fprintf(cfile, " "); attrib.Print(cfile, depth); } // There are 3 different formatting approaches: // 1) An element without children is printed as a <foo /> node // 2) An element with only a text child is printed as <foo> text </foo> // 3) An element with children is printed on multiple lines. if (firstChild == null) { //fprintf(cfile, " />"); cfile.Append(" />"); } else if (firstChild == lastChild && firstChild.ToText() != null) { //fprintf(cfile, ">"); cfile.Append(">"); firstChild.Print(cfile, depth + 1); //fprintf(cfile, "</%s>", value.c_str()); cfile.Append("</"); cfile.Append(value); cfile.Append(">"); } else { //fprintf(cfile, ">"); cfile.Append(">"); for (TiXmlNode node = firstChild; node != null; node = node.NextSibling()) { if (node.ToText() == null) { //fprintf(cfile, "\n"); cfile.Append("\n"); } node.Print(cfile, depth + 1); } //fprintf(cfile, "\n"); cfile.Append("\n"); for (int i = 0; i < depth; ++i) { //fprintf(cfile, " "); cfile.Append(" "); } //fprintf(cfile, "</%s>", value.c_str()); cfile.Append("</"); cfile.Append(value); cfile.Append(">"); } }
/// <summary> /// Return the handle as a TiXmlText. This may return null. /// </summary> TiXmlText ToText() { return((node != null && node.ToText() != null) ? node.ToText() : null); }