Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QDomDocument - how to create from QString?

Tags:

c++

xml

qt

How to create QDomDocument from QString, what contains many XML nodes?

I know one way - setContent method. But it's not better way - if I need to create XML from string with content like

<food>
    <name>Strawberry Belgian Waffles</name>
    <price>$7.95</price>
    <description>Light Belgian waffles covered with strawberries and whipped cream</description>
    <calories>900</calories>
</food>
<food>
    <name>Berry-Berry Belgian Waffles</name>
    <price>$8.95</price>
    <description>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
    <calories>900</calories>
</food>
<food>
    <name>Homestyle Breakfast</name>
    <price>$6.95</price>
    <description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
    <calories>950</calories>
</food>

I need to pass to the setContent string like

    rhythmdb.setContent("<?xml version='1.0' encoding='UTF-8'?>"
                       " <breakfast_menu>"+res+"</breakfast_menu");

It's not good way - to specify xml version and encoding from the setContent. As I imagine a right decision:

  1. I need to create QDomDocument rhythmdb;

  2. Specify XML version and encoding for rhythmdb

  3. Create child element like QDomElement childElement= rhythmdb.createElement("breakfast_menu");

  4. Append XML content of the breakfast_menu element, like childElement.setContent(res);

But there are no setContent method for QDomElement object.

How I can to program this way?

Thanks.

like image 203
Arthur Avatar asked Mar 27 '26 06:03

Arthur


1 Answers

Surveying the documentation, it does seem you really can only convert a QString into an entire QDomDocument. You can't load it as QDomNode(s) into an existing one.

You can--however--copy loaded nodes between QDomDocuments with QDomDocument::importNode(). So for mixing material from different source QStrings you can setContent() on a temporary document, and then import any of its desired node(s) into your target document. After the import, insert these local copies where you want.

In your case, that won't help you use your string in its current form. It is a series of siblings with no singular root element. And QDomDocument only takes a setContent() from valid XML, as per Wikipedia:

Each XML document has exactly one single root element. It encloses all the other elements and is therefore the sole parent element to all the other elements. ROOT elements are also called PARENT elements.

Thus if setContent() is your only string-to-XML mechanism, you will need to wrap it in a single parent before loading. Prepending it with <breakfast_menu> and appending it with </breakfast_menu> seems fine for getting it in shape with a proper ROOT.

BUT don't worry about the <?xml version='1.0' encoding='UTF-8'?>:

  • The encoding at time of input is only relevant when loading from a byte stream. You're loading from a QString, where the decoding has already been done...(correctly or incorrectly). It's Unicode code points by then and whatever you say the encoding is will have zero effect.

  • If you want the XML declaration on your output, you have to add it manually in Qt anyway. By default it's not included, and has to be specified explicitly with QDomDocument::createProcessingInstruction. That's partially because the spec says it is optional, and partially because there will never be another XML standard, anyway. ;-)

like image 116
HostileFork says dont trust SE Avatar answered Mar 28 '26 18:03

HostileFork says dont trust SE



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!