PDA

View Full Version : XML Namespaces



Rayven
11th September 2008, 21:48
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.



QDomElement root = mOutputDomDoc.createElementNS( "msp", "Report" );
...


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!

wysota
11th September 2008, 21:55
I think you can use .createElement("msp:Report").

patrik08
12th September 2008, 11:07
I think you can use .createElement("msp:Report").

no... its attributes..




QDateTime timer1( QDateTime::currentDateTime() ); /* time root */
QDomElement basexslforoot = wdoc.createElement("fo:root");
basexslforoot.setAttribute ("xmlns:fo","http://www.w3.org/1999/XSL/Format");
basexslforoot.setAttribute ("xmlns:svg","http://www.w3.org/2000/svg");
basexslforoot.setAttribute ("xmlns:cms","http://www.pulitzer.ch/2007/CMSFormat");
basexslforoot.setAttribute ("xmlns:fox","http://xmlgraphics.apache.org/fop/extensions");

Rayven
12th September 2008, 16:22
I think you can use .createElement("msp:Report").


no... its attributes..




QDateTime timer1( QDateTime::currentDateTime() ); /* time root */
QDomElement basexslforoot = wdoc.createElement("fo:root");
basexslforoot.setAttribute ("xmlns:fo","http://www.w3.org/1999/XSL/Format");
basexslforoot.setAttribute ("xmlns:svg","http://www.w3.org/2000/svg");
basexslforoot.setAttribute ("xmlns:cms","http://www.pulitzer.ch/2007/CMSFormat");
basexslforoot.setAttribute ("xmlns:fox","http://xmlgraphics.apache.org/fop/extensions");



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 (http://doc.trolltech.com/latest/createElementNS (http://doc.trolltech.com/4.4/qdomdocument.html#createElementNS)) and attributes (createattributens (http://doc.trolltech.com/latest/ createAttributeNS (http://doc.trolltech.com/4.4/qdomdocument.html#createAttributeNS)) 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...

wysota
12th September 2008, 21:49
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.

patrik08
13th September 2008, 09:00
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



textNS (QLatin1String("urn:oasis:names:tc:opendocument:xmlns:text:1.0")),
styleNS (QLatin1String("urn:oasis:names:tc:opendocument:xmlns:style:1.0")),
foNS (QLatin1String("urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0")),
tableNS (QLatin1String("urn:oasis:names:tc:opendocument:xmlns:table:1.0")),
drawNS (QLatin1String("urn:oasis:names:tc:opendocument:xmlns:drawing:1.0")),
xlinkNS (QLatin1String("http://www.w3.org/1999/xlink")),
svgNS (QLatin1String("urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0")),




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

this sample draw a image to openoffice format


void QTextOdfWriter::writeInlineCharacter(QXmlStreamWri ter &writer, const QTextFragment &fragment) const
{
writer.writeStartElement(drawNS, QString::fromLatin1("frame"));
if (m_strategy == 0) {
// don't do anything.
}
else if (fragment.charFormat().isImageFormat()) {
QTextImageFormat imageFormat = fragment.charFormat().toImageFormat();
writer.writeAttribute(drawNS, QString::fromLatin1("name"), imageFormat.name());

// vvv Copy pasted mostly from Qt =================
QImage image;
QString name = imageFormat.name();
if (name.startsWith(QLatin1String(":/"))) // auto-detect resources
name.prepend(QLatin1String("qrc"));
QUrl url = QUrl::fromEncoded(name.toUtf8());
const QVariant data = m_document->resource(QTextDocument::ImageResource, url);
if (data.type() == QVariant::Image) {
image = qvariant_cast<QImage>(data);
} else if (data.type() == QVariant::ByteArray) {
image.loadFromData(data.toByteArray());
}

if (image.isNull()) {
QString context;
if (QTextImageHandler::externalLoader)
image = QTextImageHandler::externalLoader(name, context);

if (image.isNull()) { // try direct loading
name = imageFormat.name(); // remove qrc:/ prefix again
image.load(name);
}
}

// ^^^ Copy pasted mostly from Qt =================
if (! image.isNull()) {
QBuffer imageBytes;
QImageWriter imageWriter(&imageBytes, "png");
imageWriter.write(image);
QString filename = m_strategy->createUniqueImageName();
m_strategy->addFile(filename, QString::fromLatin1("image/png"), imageBytes.data());

// get the width/height from the format.
qreal width = (imageFormat.hasProperty(QTextFormat::ImageWidth)) ? imageFormat.width() : image.width();
writer.writeAttribute(svgNS, QString::fromLatin1("width"), pixelToPoint(width));
qreal height = (imageFormat.hasProperty(QTextFormat::ImageHeight) ) ? imageFormat.height() : image.height();
writer.writeAttribute(svgNS, QString::fromLatin1("height"), pixelToPoint(height));

writer.writeStartElement(drawNS, QString::fromLatin1("image"));
writer.writeAttribute(xlinkNS, QString::fromLatin1("href"), filename);
writer.writeEndElement(); // image
}
}

writer.writeEndElement(); // frame
}





have a look on:
http://fop-miniscribus.googlecode.com/svn/trunk/doc/docdiff/qtextodfwriter.cpp
it begin at bottom....