XML DOM empty
Hi all,
it must be a stupid error but I cannot see it:
- QDomDocument xmlDocument;
- "version='1.0' encoding='UTF-8'");
- xmlDocument.appendChild( procInstr );
- xmlDocument.appendChild( rolesElement );
- for( roleIterator = roles.constBegin(); roleIterator != roles.constEnd(); roleIterator++ ){
- const Role& currentRole = *roleIterator;
- rolesElement.appendChild( currentRoleElement );
- roleIDElement.appendChild( roleIDText );
- currentRoleElement.appendChild( roleIDElement );
- }
The above XML should create a dump of a custom object Role, and the iteration is working fine, but the end result is an empty XML document with only the processing instruction. It seems to me that all elements are parented right, but I guess it is not. Any suggestion?
9 replies
You’re trying to put multiple elements on the level of the root node.
- Create your processing instruction and add it to the document (like you’re doing already).
- Create a root element and add it to the document
All further elements must be added not to the document, but to the root element (or below). You can retrieve the root element (the one you’ve created previously) via xmlDocument.documentElement().
i.e you’re trying this:
- <?xml version="1.0" encoding="UTF-8"?>
- <element1>
- <element2>
when you should do:
- <?xml version="1.0" encoding="UTF-8"?>
- <myRootElement>
- <element1>
- <element2>
- </myRootElement>
All further elements must be added not to the document, but to the root element (or below). You can retrieve the root element (the one you’ve created previously) via xmlDocument.documentElement().
Uhm..sorry, I still don’t get it. I’ve rewritten the code in order to make it clear what is the root element and I’ve removed the processing instruction since at the moment I don’t need it:
- rootElement.setAttribute( "count", roles.size() );
- // iterate on each role
- for( roleIterator = roles.constBegin(); roleIterator != roles.constEnd(); roleIterator++ ){
- const Role& currentRole = *roleIterator;
- roleIDElement.appendChild( roleIDText );
- currentRoleElement.appendChild( roleIDElement );
- rootElement.appendChild( currentRoleElement );
- }
- xmlDocument.appendChild( rootElement );
So each child element “currentRoleElement” is actually appended to the root element and the latter to the document. I don’t see where I’m trying to add all elements as root elements of the document.
The hierarchy should be that:
- rootElement
- currentRoleElement
- roleIDElement
- roleIDText
Sorry, it seems I’ve overlooked something in your code, I thought you were adding more than one element directly to the document instead of the root node.
However, this works for me:
- QDomDocument xmlDocument;
- rootElement.setAttribute( "count", 5 );
- roleIDElement.appendChild( roleIDText );
- currentRoleElement.appendChild( roleIDElement );
- rootElement.appendChild( currentRoleElement );
- xmlDocument.appendChild( rootElement );
- xmlDocument.save(stream, 2);
it produces as expected
- <root count="5">
- <subelement>
- <subelement>subelement</subelement>
- </subelement>
- </root>
So my guess is there’s something wrong with your “Role” mechanism.
I found a possible problem when creating the text node, that seem to break the tree.
If I do the following:
no node is created at all.
If I do for instance
the node is created with both the string “Hello” concat with the id() result.
If I do:
nothing is printed out.
The id member function is defined as follows:
Any idea?
An other update, that solves my problem but leaves me with a doubt.
The following is the way I was testing the output document, thru which I report the above problems:
and the following is a way that prints out the document into a file that works:
- xmlDocument.save( stream, 2);
So seems to me that the document is suffering some way of “flushing” when the toString is called?
That sure is a bit strange. I’m not aware of such buffering making toString and save behave differently, and it doesn’t happen when I test it.
What does currentRole.id() typically contain? Invalid tag characters? Numbers only? In this case, the XML becomes invalid, so when your InvalidDataPolicy (see QDomImplementation::setInvalidDataPolicy) is DropInvalidCharacters or ReturnNullNode, you might get an empty XML document. However, this would cause some debug output along the lines of “Calling appendChild() on a null node does nothing.”
I’ve just looked through the code of qdom.cpp [qt.gitorious.org] and there are no signs of such buffering. Both QDomDocument::save and toString internally call the save functions recursively on the document nodes. In fact, toString just creates a QTextStream on the returned string and writes its content with save itself. So this definetly isn’t the cause.
You must log in to post a reply. Not a member yet? Register here!
