PDA

View Full Version : QDomImplementation real dom Document;



patrik08
23rd June 2006, 00:57
i wand to write xml doc on QDomDocument not entyti ....

on line to line the doc is so:


QStringList xml;
xml.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
xml.append("<cms:root xmlns:cms=\"http://www.pulitzer.ch/2005/PuliCMS/1.0\">");
xml.append("<cms:page la=\"it\">");
xml.append(desc_it->GetXMLTag());
xml.append("</cms:page>");
xml.append("</cms:root>");
QString stayutf8 = xml.join("\n");
qDebug() << "### xml result1 " << stayutf8;


and on a real QDomDocument.... Manual say so:


QDomDocument doc(QDomImplementation::createDocument(QString("http://www.pulitzer.ch/2005/PuliCMS/1.0/"),
QString("utf8"),(const QDomDocumentType)"xml"));
QDomElement root = doc.createElement("root");
root.setAttribute("xmlns:s","http://www.pulitzer.ch/2005/shop/shema/1.0/");
root.setAttribute("xmlns:cms","http://www.pulitzer.ch/2005/PuliCMS/1.0/");
doc.appendChild(root);
QDomElement page = doc.createElement("cms:page");
page.setAttribute("xmlns:cms","http://www.pulitzer.ch/2005/PuliCMS/1.0/");
page.setAttribute("la","it");
page.appendChild((const QDomDocumentFragment)(desc_it->GetXMLTag()));
root.appendChild(page);
QString xml = doc.toString();
qDebug() << "### xml result2 " << xml;




on php5 and qt all dom function is same but new domdocument no....

php = $dom = new DOMDocument('1.0', 'utf-8') and become a clean first line...

qt no why?

how to become a clean line <?xml version=\"1.0\" encoding=\"utf-8\"?> so tidy xml dont clean?

jacek
23rd June 2006, 15:32
on php5 and qt all dom function is same but new domdocument no....

php = $dom = new DOMDocument('1.0', 'utf-8') and become a clean first line...

qt no why?
Because DOM Level 2 specification does not specify the constructor of Document (since it's an interface, not a class).


how to become a clean line <?xml version=\"1.0\" encoding=\"utf-8\"?> so tidy xml dont clean?
As usual in Qt --- with only a few lines of code:
#include <QDomDocument>

#include <QtDebug>

int main()
{
QDomDocument doc;

QDomProcessingInstruction header = doc.createProcessingInstruction( "xml", "version=\"1.0\"" );
doc.appendChild( header );

QDomElement root = doc.createElement( "test" );
doc.appendChild( root );

qDebug() << doc.toString();

return 0;
}

$ ./xml
"<?xml version="1.0"?>
<test/>
"

patrik08
23rd June 2006, 18:00
and QDomDocumentFragment so helpfull is the same?


QDomElement page = doc.createElement("cms:page");
page.setAttribute("xmlns:cms","http://www.pulitzer.ch/2005/PuliCMS/1.0/");
page.setAttribute("la","it");
page.appendChild((const QDomDocumentFragment)(desc_it->GetXMLTag()));
root.appendChild(page);

jacek
23rd June 2006, 19:52
and QDomDocumentFragment so helpfull is the same?
I'm not sure what you mean.


(const QDomDocumentFragment)(desc_it->GetXMLTag())
What does GetXMLTag() return? It seems that you are doing something dangerous here.

patrik08
24th June 2006, 01:26
I'm not sure what you mean.


What does GetXMLTag() return? It seems that you are doing something dangerous here.


Fragments of xml return.... tree or only </nocode>
I wand to attach piece of fragment to other... to build on file...

Is a imb db2 to -> mysql transfer file and back 2MB adress rysinc...

jacek
24th June 2006, 13:16
Fragments of xml return....
If that method returns QDomDocumentFragment, then why do you need a cast?

patrik08
24th June 2006, 16:57
If that method returns QDomDocumentFragment, then why do you need a cast?


why? is a existing file ... trasformed to qstring and replace <? xml...>

wenn i open the file on QDomDocument ... on error.... i must return a empyty dom?
on qstring on error read i return a <nullnode/> qstring...
qt code


if(!xmlfile.open( QIODevice::ReadOnly ) ) {
out....
}
QString errorStr;
int errorLine;
int errorColumn;

QDomDocument doc("http://www.pulitzer.ch/2005/PuliCMS/1.0");
if (!doc.setContent(&xmlfile,true, &errorStr, &errorLine, &errorColumn)) {
QString error = (QString("Parse error at line %1, column %2:\n%3")
.arg(errorLine)
.arg(errorColumn)
.arg(errorStr) );
ErrorConfig(error);
xmlfile.close();
}

the best way i think to make a similar php class on qt...

php code
/* Aggregate moore xml file to one or insert xml fragment */

class XML extends DOMDocument {
function __construct () {
parent::__construct ();
}
function __destruct() {
parent::__destruct();
}
/* function similar from createDocumentFragment but enter xml file or pure xml frags */
function appendXML_or_File($node,$frag) {

if (is_file($frag)) {
$frag = @file_get_contents($frag);
}
$tmpdoc = new self('1.0', 'utf-8');
$tmpdoc->loadXML("<dummyroot>".$this->Remove_Version($frag)."</dummyroot>")
or Error_Manager::msg('Error on fragment not possibel to insert xml Fragment!'.htmlentities($frag, ENT_QUOTES),__FILE__,__LINE__);
$newnode = $node->ownerDocument->importNode($tmpdoc->documentElement,true);
$child = $newnode->firstChild;
while ($child) {
$nextChild = $child->nextSibling;
$node->appendChild($child);
$child = $nextChild;
}
}
/* remove php code or xml header ..... */
function Remove_Version($string) {
return preg_replace_callback("/(<\?php|<\?)(.*?)\?>/si",create_function("","return '';"),$string);
}
function utf($string) {
return Multi_Language::utf8_to_unicode($string);
}

}



but can i open qtextstreams on dom?


if (! doc.setContent(qtextstreams...) ) {

jacek
24th June 2006, 17:09
why? is a existing file ... trasformed to qstring and replace <? xml...>
I was asking about this:
(const QDomDocumentFragment)(desc_it->GetXMLTag())
Either you don't need it or you do something Bad.


but can i open qtextstreams on dom?
Not directly, but you can try:
doc.setContent( stream.readAll() );

patrik08
24th June 2006, 18:38
The fragment is not fragment!

Qtextstream "<samplenode/>" readAll() return a false QDomDocument

and fragment give a false isDocumentFragment() why?






#include <QtDebug>
#include <QDebug>
#include <QObject>
#include <QSettings>
#include <QErrorMessage>
#include <QProcess>
#include <QDomDocument>
#include <QDomElement>
#include <QStringList>
#include <QDir>
#include <QDomDocumentFragment>
#include <QDomImplementation>
#include <QMessageBox>

int main(int argc, char *argv[]) {

QDomDocument dom_external;
QString filexmleternal = "./qtpage.xml";
bool is_fi = false;

QFile xmlfile(filexmleternal);
if(!xmlfile.open( QIODevice::ReadOnly ) ) {
/* stay false ! */
}
if (dom_external.setContent(&xmlfile)) {
is_fi = true;
}
if (!is_fi) {
qDebug() << "External file not loadet!";
} else {
qDebug() << "External file is super!! loadet!";
QDomDocumentFragment frag;
frag.appendChild(dom_external);
if (!frag.isDocumentFragment()) {
qDebug() << "Is not a fragment !!!";
}
}

/* external file end */
/* new qdom to insert external ... */
QDomDocument doc;
QDomProcessingInstruction header = doc.createProcessingInstruction( "xml", "version=\"1.0\"" );
doc.appendChild( header );
QDomElement root = doc.createElement( "root" );
doc.appendChild( root );
QDomElement big = doc.createElement( "big" );
if (is_fi) {
//////big.appendChild(frag); /* not insert */
}
root.appendChild( big );
/////QDomNode::isDocumentFragment ()
qDebug() << doc.toString();
return 0;
};


pro file...


TEMPLATE = app console
TARGET +=
DEPENDPATH += .
INCLUDEPATH += .

win32:debug { CONFIG = console }
CONFIG += qt warn_on release
QT += xml

# Input
SOURCES += main.cpp

jacek
24th June 2006, 19:26
fragment give a false isDocumentFragment() why?
Probably it's a bug, use QDomDocument::createDocumentFragment() to create document fragments.

PS. If you post code, please, post only relevant part. 3/4 of your example has nothing to do with QDomDocumentFragment.

patrik08
24th June 2006, 19:46
is solved so.... and fragment not run!

make a node from external root and .toElement(); after big.appendChild(extase);
to other node....




int main(int argc, char *argv[]) {

QDomDocument dom_external;
QString filexmleternal = "./qtpage.xml";
bool is_fi = false;

QFile xmlfile(filexmleternal);
if(!xmlfile.open( QIODevice::ReadOnly ) ) {
/* stay false ! */
}
is_fi = dom_external.setContent(&xmlfile);
QDomElement root_extern = dom_external.documentElement();
QDomNode externxml = root_extern.firstChild();
QDomNode externxml_2 = dom_external.importNode(externxml,true);
QDomElement extase = externxml_2.toElement();

if (!is_fi) {
qDebug() << "External file not loadet!";
} else {
qDebug() << "External file is super!! loadet!";
}

QDomDocument doc;
QDomProcessingInstruction header = doc.createProcessingInstruction( "xml", "version=\"1.0\"" );
doc.appendChild( header );
QDomElement root = doc.createElement( "root" );
doc.appendChild( root );
QDomElement big = doc.createElement( "big" );
big.appendChild(extase); /* the external file not from fragment */
root.appendChild( big );



qDebug() << doc.toString();
return 0;
};

stella1016
18th January 2011, 18:38
Because DOM Level 2 specification does not specify the constructor of Document (since it's an interface, not a class).


As usual in Qt --- with only a few lines of code:
#include <QDomDocument>

#include <QtDebug>

int main()
{
QDomDocument doc;

QDomProcessingInstruction header = doc.createProcessingInstruction( "xml", "version=\"1.0\"" );
doc.appendChild( header );

QDomElement root = doc.createElement( "test" );
doc.appendChild( root );

qDebug() << doc.toString();

return 0;
}

Thanks, this helps me!