Results 1 to 6 of 6

Thread: Can't get child of qDomNode

  1. #1
    Join Date
    Sep 2010
    Location
    Denmark
    Posts
    28
    Thanks
    10
    Thanked 3 Times in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default 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.
    Qt Code:
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <project>
    3. <info project_name="matt2" project_path="C:/Users/david/matt2" pad_count="4" pad_width="800" pad_height="600"/>
    4. <storyboard folder="sc1">
    5. <0 timing="50" count="36">
    6. <0 p1x="285" p1y="79" p2x="286" p2y="79" rgb="4293958640"/>
    7. ...
    8. <35 p1x="326" p1y="175" p2x="326" p2y="175" rgb="4293958640"/>
    9. </0>
    10. <1 timing="75" count="30">
    11. <0 p1x="319" p1y="95" p2x="320" p2y="95" rgb="4293958640"/>
    12. ...
    13. <29 p1x="292" p1y="113" p2x="292" p2y="113" rgb="4287102865"/>
    14. </1>
    15. <2 timing="50" count="26">
    16. <0 p1x="170" p1y="53" p2x="175" p2y="53" rgb="4287102865"/>
    17. ...
    18. <25 p1x="299" p1y="65" p2x="299" p2y="65" rgb="4291326770"/>
    19. </2>
    20. <3 timing="50" count="12">
    21. <0 p1x="298" p1y="43" p2x="298" p2y="44" rgb="4291326770"/>
    22. ...
    23. <11 p1x="269" p1y="73" p2x="269" p2y="73" rgb="4291326770"/>
    24. </3>
    25. </storyboard>
    26. </project>
    To copy to clipboard, switch view to plain text mode 
    After opening the file I have this code, that works
    Qt Code:
    1. doc.setContent(&file);
    2. file.close();
    3. QDomElement root = doc.documentElement();
    4.  
    5. QDomNode n = root.firstChild();
    6. if (n.nodeName() == "info")
    7. {
    8. QDomElement e = n.toElement();
    To copy to clipboard, switch view to plain text mode 
    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.
    Qt Code:
    1. // now load Storyboards...
    2. n = n.nextSibling();
    3. while (!n.isNull())
    4. {
    5. QDomElement s1 = n.toElement();
    6. if (n.nodeName() == "storyboard")
    7. {
    8. mActiveStoryboard = s1.attribute("folder", "");
    9. qDebug() << "storyboard: " << mActiveStoryboard;
    10. QDomNode pad = n.firstChild();
    11. while (!pad.isNull())
    12. {
    13. QDomElement s2 = pad.toElement();
    14. qDebug() << "pad: " << pad.nodeName();
    15. pad = pad.nextSibling();
    To copy to clipboard, switch view to plain text mode 
    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

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    503
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Can't get child of qDomNode

    Hi, from https://www.w3schools.com/xml/xml_elements.asp:

    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

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Can't get child of qDomNode

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  4. #4
    Join Date
    Sep 2010
    Location
    Denmark
    Posts
    28
    Thanks
    10
    Thanked 3 Times in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default 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!

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default 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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. #6
    Join Date
    Sep 2010
    Location
    Denmark
    Posts
    28
    Thanks
    10
    Thanked 3 Times in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Can't get child of qDomNode

    I've got it working. Right now, the beginning of the file looks like this
    Qt Code:
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <project>
    3. <info project_name="mattMathilde" project_path="C:/Users/gldalar5/mattMathilde" pad_width="800" pad_height="600"/>
    4. <storyboard padCount="2" folder="epi1">
    5. <pad timing="50">
    6. <line p1x="79" p1y="68" p2x="81" p2y="68" rgb="4293958640" width="11"/>
    7. <line p1x="81" p1y="68" p2x="83" p2y="67" rgb="4293958640" width="11"/>
    8. <line p1x="83" p1y="67" p2x="87" p2y="66" rgb="4293958640" width="11"/>
    9. <line p1x="87" p1y="66" p2x="91" p2y="64" rgb="4293958640" width="11"/>
    10. <line p1x="91" p1y="64" p2x="97" p2y="63" rgb="4293958640" width="11"/>
    To copy to clipboard, switch view to plain text mode 
    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!

Similar Threads

  1. QDomNode delete children
    By trallallero in forum Qt Programming
    Replies: 9
    Last Post: 11th December 2013, 01:13
  2. QDomNode replaceChild is not work
    By lucasbemo in forum Newbie
    Replies: 1
    Last Post: 6th December 2010, 18:03
  3. Problem with Deleting QDomNode
    By metdos in forum Qt Programming
    Replies: 6
    Last Post: 16th July 2010, 19:58
  4. QDomNode
    By sabeesh in forum Qt Programming
    Replies: 1
    Last Post: 14th September 2007, 07:11
  5. QDomNode
    By sabeesh in forum Qt Programming
    Replies: 1
    Last Post: 6th September 2007, 13:18

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.