Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 5

Qt-interest Archive, February 2008
DOM-XML not understanding


Message 1 in thread

Hi,
I have an xml file that looks like this:
---------
<?xml version="1.0"?>
<Document>
<Elements>
</Elements>
</Document>
----------
the I do in my app:

        QTextStream TextStream(&file);
        QDomElement root = doc.documentElement();
        QDomNode n = root.firstChild();
        n.appendChild(doc.createElement("red"));
        n.appendChild(doc.createElement("white"));
        n.save(TextStream,4);
        file.close();
        
and the result is:
-------
<?xml version="1.0"?>
<Document>
<Elements>
</Elements>
</Document>    <Elements>
        <red/>
        <white/>
    </Elements>
-------

But what I wanted is:
------------
<?xml version="1.0"?>
<Document>
    <Elements>
        <red>
        </red>
        <white>
        </white>
    </Elements>
</Document>
------------

What do I have to change to get the result that I want?

BR
Eckhard

--
 [ signature omitted ] 

Message 2 in thread

Whats wrong with <red/> ?

<red/> is the XML equivalent to <red></red> 

if you don't want those (special case for empty elements)
put something in the elements!

Cheers,
Peter

> -----Ursprüngliche Nachricht-----
> Von: Eckhard Jokisch [mailto:e.jokisch@xxxxxxxxx] 
> Gesendet: Mittwoch, 27. Februar 2008 18:02
> An: qt-interest@xxxxxxxxxxxxx
> Betreff: DOM-XML not understanding
> 
> Hi,
> I have an xml file that looks like this:
> ---------
> <?xml version="1.0"?>
> <Document>
> <Elements>
> </Elements>
> </Document>
> ----------
> the I do in my app:
> 
>         QTextStream TextStream(&file);
>         QDomElement root = doc.documentElement();
>         QDomNode n = root.firstChild();
>         n.appendChild(doc.createElement("red"));
>         n.appendChild(doc.createElement("white"));
>         n.save(TextStream,4);
>         file.close();
>         
> and the result is:
> -------
> <?xml version="1.0"?>
> <Document>
> <Elements>
> </Elements>
> </Document>    <Elements>
>         <red/>
>         <white/>
>     </Elements>
> -------
> 
> But what I wanted is:
> ------------
> <?xml version="1.0"?>
> <Document>
>     <Elements>
>         <red>
>         </red>
>         <white>
>         </white>
>     </Elements>
> </Document>
> ------------
> 
> What do I have to change to get the result that I want?
> 
> BR
> Eckhard
> 
> --
> To unsubscribe - send a mail to 
> qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the 
> subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
> 
> 

--
 [ signature omitted ] 

Message 3 in thread

Hi Peter,
the problem is not the <red/>, my problem is the tree order of the elements.
Or am I totally confused about XML now? I thought that <Document> is the
root element and everything else should live within the root element.

Cheers
Eckhard

Peter Prade wrote:

> Whats wrong with <red/> ?
> 
> <red/> is the XML equivalent to <red></red>
> 
> if you don't want those (special case for empty elements)
> put something in the elements!
> 
> Cheers,
> Peter
> 
>> -----Ursprüngliche Nachricht-----
>> Von: Eckhard Jokisch [mailto:e.jokisch@xxxxxxxxx]
>> Gesendet: Mittwoch, 27. Februar 2008 18:02
>> An: qt-interest@xxxxxxxxxxxxx
>> Betreff: DOM-XML not understanding
>> 
>> Hi,
>> I have an xml file that looks like this:
>> ---------
>> <?xml version="1.0"?>
>> <Document>
>> <Elements>
>> </Elements>
>> </Document>
>> ----------
>> the I do in my app:
>> 
>>         QTextStream TextStream(&file);
>>         QDomElement root = doc.documentElement();
>>         QDomNode n = root.firstChild();
>>         n.appendChild(doc.createElement("red"));
>>         n.appendChild(doc.createElement("white"));
>>         n.save(TextStream,4);
>>         file.close();
>>         
>> and the result is:
>> -------
>> <?xml version="1.0"?>
>> <Document>
>> <Elements>
>> </Elements>
>> </Document>    <Elements>
>>         <red/>
>>         <white/>
>>     </Elements>
>> -------
>> 
>> But what I wanted is:
>> ------------
>> <?xml version="1.0"?>
>> <Document>
>>     <Elements>
>>         <red>
>>         </red>
>>         <white>
>>         </white>
>>     </Elements>
>> </Document>
>> ------------
>> 
>> What do I have to change to get the result that I want?
>> 
>> BR
>> Eckhard
>> 
>> --
>> To unsubscribe - send a mail to
>> qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the
>> subject or the body.
>> List archive and information: http://lists.trolltech.com/qt-interest/
>> 
>> 
> 
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body. List archive and information:
> http://lists.trolltech.com/qt-interest/

--
 [ signature omitted ] 

Message 4 in thread

I see it now. 

What do you expect these 2 lines to do?

QDomElement root = doc.documentElement();
QDomNode n = root.firstChild();

Your document is empty at that point, so you somehow append your elements behind the document.

have a look at the docs:
http://doc.trolltech.com/4.3/qdomdocument.html#details

They do it like this:
QDomElement root = doc.createElement("MyML");
doc.appendChild(root);
QDomElement tag = doc.createElement("Greeting");
root.appendChild(tag);

Cheers,
Peter

> -----Ursprüngliche Nachricht-----
> Von: Eckhard Jokisch [mailto:e.jokisch@xxxxxxxxx] 
> Gesendet: Mittwoch, 27. Februar 2008 18:51
> An: qt-interest@xxxxxxxxxxxxx
> Betreff: Re: AW: DOM-XML not understanding
> 
> Hi Peter,
> the problem is not the <red/>, my problem is the tree order 
> of the elements.
> Or am I totally confused about XML now? I thought that 
> <Document> is the
> root element and everything else should live within the root element.
> 
> Cheers
> Eckhard
> 
> Peter Prade wrote:
> 
> > Whats wrong with <red/> ?
> > 
> > <red/> is the XML equivalent to <red></red>
> > 
> > if you don't want those (special case for empty elements)
> > put something in the elements!
> > 
> > Cheers,
> > Peter
> > 
> >> -----Ursprüngliche Nachricht-----
> >> Von: Eckhard Jokisch [mailto:e.jokisch@xxxxxxxxx]
> >> Gesendet: Mittwoch, 27. Februar 2008 18:02
> >> An: qt-interest@xxxxxxxxxxxxx
> >> Betreff: DOM-XML not understanding
> >> 
> >> Hi,
> >> I have an xml file that looks like this:
> >> ---------
> >> <?xml version="1.0"?>
> >> <Document>
> >> <Elements>
> >> </Elements>
> >> </Document>
> >> ----------
> >> the I do in my app:
> >> 
> >>         QTextStream TextStream(&file);
> >>         QDomElement root = doc.documentElement();
> >>         QDomNode n = root.firstChild();
> >>         n.appendChild(doc.createElement("red"));
> >>         n.appendChild(doc.createElement("white"));
> >>         n.save(TextStream,4);
> >>         file.close();
> >>         
> >> and the result is:
> >> -------
> >> <?xml version="1.0"?>
> >> <Document>
> >> <Elements>
> >> </Elements>
> >> </Document>    <Elements>
> >>         <red/>
> >>         <white/>
> >>     </Elements>
> >> -------
> >> 
> >> But what I wanted is:
> >> ------------
> >> <?xml version="1.0"?>
> >> <Document>
> >>     <Elements>
> >>         <red>
> >>         </red>
> >>         <white>
> >>         </white>
> >>     </Elements>
> >> </Document>
> >> ------------
> >> 
> >> What do I have to change to get the result that I want?
> >> 
> >> BR
> >> Eckhard
> >> 
> >> --
> >> To unsubscribe - send a mail to
> >> qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the
> >> subject or the body.
> >> List archive and information: 
> http://lists.trolltech.com/qt-interest/
> >> 
> >> 
> > 
> > --
> > To unsubscribe - send a mail to 
> qt-interest-request@xxxxxxxxxxxxx with
> > "unsubscribe" in the subject or the body. List archive and 
> information:
> > http://lists.trolltech.com/qt-interest/
> 
> --
> To unsubscribe - send a mail to 
> qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the 
> subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
> 
> 

--
 [ signature omitted ] 

Message 5 in thread

Hi Peter,
thanks for that hint. Somehow I never saw this part of the documentation
regarding creation of a document and tried to invent the wheel myselves....

Cheers
Eckhard

Peter Prade wrote:

> I see it now.
> 
> What do you expect these 2 lines to do?
> 
> QDomElement root = doc.documentElement();
> QDomNode n = root.firstChild();
> 
> Your document is empty at that point, so you somehow append your elements
> behind the document.
> 
> have a look at the docs:
> http://doc.trolltech.com/4.3/qdomdocument.html#details
> 
> They do it like this:
> QDomElement root = doc.createElement("MyML");
> doc.appendChild(root);
> QDomElement tag = doc.createElement("Greeting");
> root.appendChild(tag);
> 
> Cheers,
> Peter
> 
>> -----Ursprüngliche Nachricht-----
>> Von: Eckhard Jokisch [mailto:e.jokisch@xxxxxxxxx]
>> Gesendet: Mittwoch, 27. Februar 2008 18:51
>> An: qt-interest@xxxxxxxxxxxxx
>> Betreff: Re: AW: DOM-XML not understanding
>> 
>> Hi Peter,
>> the problem is not the <red/>, my problem is the tree order
>> of the elements.
>> Or am I totally confused about XML now? I thought that
>> <Document> is the
>> root element and everything else should live within the root element.
>> 
>> Cheers
>> Eckhard
>> 
>> Peter Prade wrote:
>> 
>> > Whats wrong with <red/> ?
>> > 
>> > <red/> is the XML equivalent to <red></red>
>> > 
>> > if you don't want those (special case for empty elements)
>> > put something in the elements!
>> > 
>> > Cheers,
>> > Peter
>> > 
>> >> -----Ursprüngliche Nachricht-----
>> >> Von: Eckhard Jokisch [mailto:e.jokisch@xxxxxxxxx]
>> >> Gesendet: Mittwoch, 27. Februar 2008 18:02
>> >> An: qt-interest@xxxxxxxxxxxxx
>> >> Betreff: DOM-XML not understanding
>> >> 
>> >> Hi,
>> >> I have an xml file that looks like this:
>> >> ---------
>> >> <?xml version="1.0"?>
>> >> <Document>
>> >> <Elements>
>> >> </Elements>
>> >> </Document>
>> >> ----------
>> >> the I do in my app:
>> >> 
>> >>         QTextStream TextStream(&file);
>> >>         QDomElement root = doc.documentElement();
>> >>         QDomNode n = root.firstChild();
>> >>         n.appendChild(doc.createElement("red"));
>> >>         n.appendChild(doc.createElement("white"));
>> >>         n.save(TextStream,4);
>> >>         file.close();
>> >>         
>> >> and the result is:
>> >> -------
>> >> <?xml version="1.0"?>
>> >> <Document>
>> >> <Elements>
>> >> </Elements>
>> >> </Document>    <Elements>
>> >>         <red/>
>> >>         <white/>
>> >>     </Elements>
>> >> -------
>> >> 
>> >> But what I wanted is:
>> >> ------------
>> >> <?xml version="1.0"?>
>> >> <Document>
>> >>     <Elements>
>> >>         <red>
>> >>         </red>
>> >>         <white>
>> >>         </white>
>> >>     </Elements>
>> >> </Document>
>> >> ------------
>> >> 
>> >> What do I have to change to get the result that I want?
>> >> 
>> >> BR
>> >> Eckhard
>> >> 
>> >> --
>> >> To unsubscribe - send a mail to
>> >> qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the
>> >> subject or the body.
>> >> List archive and information:
>> http://lists.trolltech.com/qt-interest/
>> >> 
>> >> 
>> > 
>> > --
>> > To unsubscribe - send a mail to
>> qt-interest-request@xxxxxxxxxxxxx with
>> > "unsubscribe" in the subject or the body. List archive and
>> information:
>> > http://lists.trolltech.com/qt-interest/
>> 
>> --
>> To unsubscribe - send a mail to
>> qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the
>> subject or the body.
>> List archive and information: http://lists.trolltech.com/qt-interest/
>> 
>> 
> 
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body. List archive and information:
> http://lists.trolltech.com/qt-interest/

--
 [ signature omitted ]