Results 1 to 6 of 6

Thread: XML Namespaces

  1. #1
    Join Date
    Jan 2006
    Location
    Third rock from the sun
    Posts
    106
    Thanks
    17
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default XML Namespaces

    How do I enter an XML namespace using the DOM Documents in Qt4.4? I have sucessfully created an XML file using the QDomDocument. Now I am trying to add the "msp" namespace to my markup.

    Qt Code:
    1. QDomElement root = mOutputDomDoc.createElementNS( "msp", "Report" );
    2. ...
    To copy to clipboard, switch view to plain text mode 

    This produces the XML
    <Report xmlns="msp">...</Report>
    when I am looking for: <msp:Report>...</Report>. This same thing occurs with createAttributeNS( "msp", "Tag" ). Is there something that I am doing wrong with the namespaces?

    Thanks!
    Every little child knows
    if you cant see dreams
    your eyes are blind

    Moxy Fruvous

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: XML Namespaces

    I think you can use .createElement("msp:Report").

  3. #3
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: XML Namespaces

    Quote Originally Posted by wysota View Post
    I think you can use .createElement("msp:Report").
    no... its attributes..

    Qt Code:
    1. QDateTime timer1( QDateTime::currentDateTime() ); /* time root */
    2. QDomElement basexslforoot = wdoc.createElement("fo:root");
    3. basexslforoot.setAttribute ("xmlns:fo","http://www.w3.org/1999/XSL/Format");
    4. basexslforoot.setAttribute ("xmlns:svg","http://www.w3.org/2000/svg");
    5. basexslforoot.setAttribute ("xmlns:cms","http://www.pulitzer.ch/2007/CMSFormat");
    6. basexslforoot.setAttribute ("xmlns:fox","http://xmlgraphics.apache.org/fop/extensions");
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Third rock from the sun
    Posts
    106
    Thanks
    17
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: XML Namespaces

    Quote Originally Posted by wysota View Post
    I think you can use .createElement("msp:Report").
    Quote Originally Posted by patrik08 View Post
    no... its attributes..

    Qt Code:
    1. QDateTime timer1( QDateTime::currentDateTime() ); /* time root */
    2. QDomElement basexslforoot = wdoc.createElement("fo:root");
    3. basexslforoot.setAttribute ("xmlns:fo","http://www.w3.org/1999/XSL/Format");
    4. basexslforoot.setAttribute ("xmlns:svg","http://www.w3.org/2000/svg");
    5. basexslforoot.setAttribute ("xmlns:cms","http://www.pulitzer.ch/2007/CMSFormat");
    6. basexslforoot.setAttribute ("xmlns:fox","http://xmlgraphics.apache.org/fop/extensions");
    To copy to clipboard, switch view to plain text mode 
    Both of these answers worked, for elements and attributes.

    This is more a question for the Trolls, but why are there functions for creating elements (createelementns.html"]createElementNS[/URL]) and attributes (createattributens[/b].html"] createAttributeNS[/URL]) that are in a particular namespace, if they do not place the namespace declaration in the appropriate place? I know full XML support was just added in recent release, but it seems premature to add the namespaces if the just add a new attribute...
    Every little child knows
    if you cant see dreams
    your eyes are blind

    Moxy Fruvous

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: XML Namespaces

    The approach is based on a DOM tree, so it's like building using building blocks - you have to place each block in an appropriate place yourself.

  6. #6
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: XML Namespaces

    If you having many namenspace use;
    QXmlStreamWriter

    you declare all namenspace at construct class

    ( qt 4.5 version code )

    #include <QXmlStreamWriter>
    This class was introduced in Qt 4.3.
    and write faster as dom

    Qt Code:
    1. textNS (QLatin1String("urn:oasis:names:tc:opendocument:xmlns:text:1.0")),
    2. styleNS (QLatin1String("urn:oasis:names:tc:opendocument:xmlns:style:1.0")),
    3. foNS (QLatin1String("urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0")),
    4. tableNS (QLatin1String("urn:oasis:names:tc:opendocument:xmlns:table:1.0")),
    5. drawNS (QLatin1String("urn:oasis:names:tc:opendocument:xmlns:drawing:1.0")),
    6. xlinkNS (QLatin1String("http://www.w3.org/1999/xlink")),
    7. svgNS (QLatin1String("urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0")),
    To copy to clipboard, switch view to plain text mode 


    After you only writer.writeStartElement(drawNS, QString::fromLatin1("frame"));

    this sample draw a image to openoffice format
    Qt Code:
    1. void QTextOdfWriter::writeInlineCharacter(QXmlStreamWriter &writer, const QTextFragment &fragment) const
    2. {
    3. writer.writeStartElement(drawNS, QString::fromLatin1("frame"));
    4. if (m_strategy == 0) {
    5. // don't do anything.
    6. }
    7. else if (fragment.charFormat().isImageFormat()) {
    8. QTextImageFormat imageFormat = fragment.charFormat().toImageFormat();
    9. writer.writeAttribute(drawNS, QString::fromLatin1("name"), imageFormat.name());
    10.  
    11. // vvv Copy pasted mostly from Qt =================
    12. QImage image;
    13. QString name = imageFormat.name();
    14. if (name.startsWith(QLatin1String(":/"))) // auto-detect resources
    15. name.prepend(QLatin1String("qrc"));
    16. QUrl url = QUrl::fromEncoded(name.toUtf8());
    17. const QVariant data = m_document->resource(QTextDocument::ImageResource, url);
    18. if (data.type() == QVariant::Image) {
    19. image = qvariant_cast<QImage>(data);
    20. } else if (data.type() == QVariant::ByteArray) {
    21. image.loadFromData(data.toByteArray());
    22. }
    23.  
    24. if (image.isNull()) {
    25. QString context;
    26. if (QTextImageHandler::externalLoader)
    27. image = QTextImageHandler::externalLoader(name, context);
    28.  
    29. if (image.isNull()) { // try direct loading
    30. name = imageFormat.name(); // remove qrc:/ prefix again
    31. image.load(name);
    32. }
    33. }
    34.  
    35. // ^^^ Copy pasted mostly from Qt =================
    36. if (! image.isNull()) {
    37. QBuffer imageBytes;
    38. QImageWriter imageWriter(&imageBytes, "png");
    39. imageWriter.write(image);
    40. QString filename = m_strategy->createUniqueImageName();
    41. m_strategy->addFile(filename, QString::fromLatin1("image/png"), imageBytes.data());
    42.  
    43. // get the width/height from the format.
    44. qreal width = (imageFormat.hasProperty(QTextFormat::ImageWidth)) ? imageFormat.width() : image.width();
    45. writer.writeAttribute(svgNS, QString::fromLatin1("width"), pixelToPoint(width));
    46. qreal height = (imageFormat.hasProperty(QTextFormat::ImageHeight)) ? imageFormat.height() : image.height();
    47. writer.writeAttribute(svgNS, QString::fromLatin1("height"), pixelToPoint(height));
    48.  
    49. writer.writeStartElement(drawNS, QString::fromLatin1("image"));
    50. writer.writeAttribute(xlinkNS, QString::fromLatin1("href"), filename);
    51. writer.writeEndElement(); // image
    52. }
    53. }
    54.  
    55. writer.writeEndElement(); // frame
    56. }
    To copy to clipboard, switch view to plain text mode 


    have a look on:
    http://fop-miniscribus.googlecode.co...todfwriter.cpp
    it begin at bottom....

Similar Threads

  1. Accessing anonymous namespaces?
    By maverick_pol in forum General Programming
    Replies: 3
    Last Post: 14th April 2008, 07:22
  2. moc with same file names?
    By cjhuitt in forum Qt Programming
    Replies: 9
    Last Post: 27th April 2007, 21:36
  3. namespaces and translations
    By gri in forum Qt Programming
    Replies: 3
    Last Post: 30th March 2007, 10:53
  4. problem with namespaces
    By steve918 in forum General Programming
    Replies: 1
    Last Post: 17th September 2006, 17:55

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.