List Info

Thread: Parsing and translating XML game-data




Parsing and translating XML game-data
user name
2006-12-02 13:31:22
Hello Ian,

2006/12/2, Ian Wadham < ianw2optusnet.com.au">ianw2optusnet.com.au>:
2006/12/1, Ian Wadham wrote:
>; > Burkhard, it is "QDomElement::text()" that drops lower-level
> > tags (as documented), not ".toUtf8()". &nbsp;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 "&;gt;" and "&;lt;", 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&quot;.

For example, in my game-data in XML I might write:

&lt;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&quot; appears in bold font. ; But when
QDomDocument parses the XML it treats the "<b>not<;/b>&quot; as an
inner-level XML node (a QDomElement). &nbsp;If you ask for the text()
of the <Hint&gt; 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 &lt;b&amp;gt;not&amp;lt;/b&amp;gt; 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 "&;lt;" with '<' and every "&;gt;" 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 "&;lt;", "&;gt;" and "&;amp;" 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&quot;, in Qt 4.* they are global functions in the namespace "Qt&quot;.

I hope this provides a solution to the problem.

Bye,
Burkhard
Parsing and translating XML game-data
user name
2006-12-02 15:59:28
> > <Hint>Do <b>not</b> go up the
right hand ladder.</Hint>

> 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 &lt;b&gt;not&lt;/b&gt;
go up the right hand ladder.</Hint>

As an alternative, a CDATA section can also be used to embed
markup in
an XML document. See QDomDocument::createCDATASection().
_______________________________________________
kde-games-devel mailing list
kde-games-develkde.org
https://mail.kde.org/mailman/listinfo/kde-games-devel
[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )