[Kde-games-devel] Parsing and translating XML game-data
Burkhard Lehner
Burkhard.Lehner at gmx.de
Sat Dec 2 14:31:22 CET 2006
Hello Ian,
2006/12/2, Ian Wadham <ianw2 at optusnet.com.au>:
>
> 2006/12/1, Ian Wadham wrote:
> > > Burkhard, it is "QDomElement::text()" that drops lower-level
> > > tags (as documented), not ".toUtf8()". They never get into the
> > > QString that QDomElement generates.
> Burkhard Lehner wrote:
> > Ah, I see the point! You have no way to store the characters '>'
> > and '<' within XML text. But I thought these characters can be
> > escaped with ">" and "<", or am I wrong here? Sorry, I'm
> > not an XML expert, but at least HTML does it that way.
> >
> Um, no ... a QString can have "rich text" in it, which is a subset
> of HTML but also looks like the inner levels of some XML docs.
> See Qt 4.2 doc home page and follow the path "Key Technologies,
> Rich Text Processing, Supported HTML Subset".
>
> For example, in my game-data in XML I might write:
>
> <Hint>Do <b>not</b> go up the right hand ladder.</Hint>
>
> If I display the QString "Do <b>not</b> go up ..." in a QLabel or
> message box, the word "not" appears in bold font. But when
> QDomDocument parses the XML it treats the "<b>not</b>" as an
> inner-level XML node (a QDomElement). If you ask for the text()
> of the <Hint> element, Qt omits the <b> and </b>, so you lose the
> bold font effect.
Why don't you just escape the special characters '<' and '>', so the rich
text formatting tags no longer look like XML tags?
So, your example would look like that:
<Hint>Do <b>not</b> go up the right hand ladder.</Hint>
Now this is simple text, QDom* doesn't recognize any bold tags as XML tags,
and converting it back is simply replacing every "<" with '<' and every
">" with '>'.
Qt already has functions for doing the conversion in either direction. The
function "escape" converts a string that contains rich text to a string that
has all the characters '<', '>' and '&' escaped by "<", ">" and
"&" respectively. The function "convertFromPlainText" does the opposite
and transforms the string back to rich text.
In Qt 3.* these functions are static functions of the class "QStyleSheet",
in Qt 4.* they are global functions in the namespace "Qt".
I hope this provides a solution to the problem.
Bye,
Burkhard
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.kde.org/pipermail/kde-games-devel/attachments/20061202/f18756b7/attachment.html
More information about the kde-games-devel
mailing list