Can't get child of qDomNode
I have this xml file, for a storyboard program.
First I retrieve the general info about the storyboard, such as number of pads, size etc.
Then I retrieve the storyboard folder. Here I keep pixmaps in the folder 'sc1', but they are created at runtime, and not saved.
Number of pads in this example are 4. Each pad starts with an entry with timing and count, and inside this tag, you'll find points and pen color for drawing lines on a QGraphicsScene.
Code:
<?xml version="1.0" encoding="UTF-8"?>
<project>
<info project_name="matt2" project_path="C:/Users/david/matt2" pad_count="4" pad_width="800" pad_height="600"/>
<storyboard folder="sc1">
<0 timing="50" count="36">
<0 p1x="285" p1y="79" p2x="286" p2y="79" rgb="4293958640"/>
...
<35 p1x="326" p1y="175" p2x="326" p2y="175" rgb="4293958640"/>
</0>
<1 timing="75" count="30">
<0 p1x="319" p1y="95" p2x="320" p2y="95" rgb="4293958640"/>
...
<29 p1x="292" p1y="113" p2x="292" p2y="113" rgb="4287102865"/>
</1>
<2 timing="50" count="26">
<0 p1x="170" p1y="53" p2x="175" p2y="53" rgb="4287102865"/>
...
<25 p1x="299" p1y="65" p2x="299" p2y="65" rgb="4291326770"/>
</2>
<3 timing="50" count="12">
<0 p1x="298" p1y="43" p2x="298" p2y="44" rgb="4291326770"/>
...
<11 p1x="269" p1y="73" p2x="269" p2y="73" rgb="4291326770"/>
</3>
</storyboard>
</project>
After opening the file I have this code, that works
Code:
doc.setContent(&file);
file.close();
if (n.nodeName() == "info")
{
Here comes the part where it goes wrong.
I can get the folder of the storyboard, but when I write: "QDomNode pad = n.firstChild();", pad is NULL. I can't get to the actual pad information.
Code:
// now load Storyboards...
n = n.nextSibling();
while (!n.isNull())
{
if (n.nodeName() == "storyboard")
{
mActiveStoryboard = s1.attribute("folder", "");
qDebug() << "storyboard: " << mActiveStoryboard;
while (!pad.isNull())
{
qDebug() << "pad: " << pad.nodeName();
pad = pad.nextSibling();
Can anyone please help me retrive "<0 timing="50" count="36">"?
I feel I have tried everything, and it must be n.firstChild(), or...? Please enlighten me :)
Re: Can't get child of qDomNode
Hi, from https://www.w3schools.com/xml/xml_elements.asp:
Quote:
XML Naming Rules
XML elements must follow these naming rules:
...
Element names must start with a letter or underscore
...
Element names can contain letters, digits, hyphens, underscores, and periods
...
So I think your node names "0" or "29" are not allowed, and you should rename them. Since they all seem to carry the same information I would also give them the same node name.
Best regards,
Ginsengelf
Re: Can't get child of qDomNode
Quote:
Since they all seem to carry the same information I would also give them the same node name.
Agreed. "keyframe" or something like that, with "sequence='0'" as an attribute to replace your numerical node names. Likewise for the children of these nodes - "step" and "stepNo" for their node names and step number, respectively.
If these are documents over which you have no control, then you could try making a call to QDomImplementation::setInvalidDataPolicy() with the argument QDomImplementation::AcceptInvalidData before creating any instance of QDomDocument. I am not sure if this works for both reading and writing with QDomDocument; the docs show examples only for creating documents.
But if you are the one creating these XML documents, then you should fix it to comply with the standard. Otherwise, your files may be unreadable by other programs your users might want to use to browse or display the contents.
Re: Can't get child of qDomNode
Thanks!
i must admit that I didn't know that elements should start with letter or underscore.
I create the xml document in the same software, so it should be easily fixed.
Thanks again!
Re: Can't get child of qDomNode
Using the same name for each child element will also make it a bit easier to traverse the hierarchy - instead of going sibling by sibling and testing for null, you can simply ask for all child nodes with a given tag name (into a QDomNodeList) and just iterate over it.
Re: Can't get child of qDomNode
I've got it working. Right now, the beginning of the file looks like this
Code:
<?xml version="1.0" encoding="UTF-8"?>
<project>
<info project_name="mattMathilde" project_path="C:/Users/gldalar5/mattMathilde" pad_width="800" pad_height="600"/>
<storyboard padCount="2" folder="epi1">
<pad timing="50">
<line p1x="79" p1y="68" p2x="81" p2y="68" rgb="4293958640" width="11"/>
<line p1x="81" p1y="68" p2x="83" p2y="67" rgb="4293958640" width="11"/>
<line p1x="83" p1y="67" p2x="87" p2y="66" rgb="4293958640" width="11"/>
<line p1x="87" p1y="66" p2x="91" p2y="64" rgb="4293958640" width="11"/>
<line p1x="91" p1y="64" p2x="97" p2y="63" rgb="4293958640" width="11"/>
Thanks to your suggestions, the xml is much better and precise now.
The 'padCount' value was moved from the 'project' to the 'storyboard' tag, where it belongs. A project can have many storyboards, and each storyboard must know how many pads there are to the storyboard. My mistake.
The 'count' value in the 'pad' tag has been removed, since I don't use the count, and just iterate over lines in each pad.
Each line has a color AND a width, so I added a 'width' value. Another mistake.
And - finally - the idea of calling the pad-node 'pad' and the line-node 'line', is so obvious, that it almost hurts.
Thankyou for helping!