2nd attempt, using QDomDocument:
void test2() {
const char* rawXML =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<root>"
" <a>11111</a>"
" <a><b>test 123</b></a>"
"</root>";
xml.setContent(ba);
qDebug("root element has %d childs", rootElement.childNodes().count());
qDebug("root element is %s ", qPrintable(rootElement.nodeName()) );
for (int i=0; i< a.length(); i ++) {
a.at(i).save(ts,0);
qDebug("- [%s]", qPrintable(aContent));
}
}
void test2() {
const char* rawXML =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<root>"
" <a>11111</a>"
" <a><b>test 123</b></a>"
"</root>";
QDomDocument xml("rawXML");
QByteArray ba(rawXML);
xml.setContent(ba);
QDomElement rootElement = xml.documentElement();
qDebug("root element has %d childs", rootElement.childNodes().count());
qDebug("root element is %s ", qPrintable(rootElement.nodeName()) );
QDomNodeList a = rootElement.elementsByTagName("a");
for (int i=0; i< a.length(); i ++) {
QString aContent;
QTextStream ts(&aContent);
a.at(i).save(ts,0);
qDebug("- [%s]", qPrintable(aContent));
QDomNode d = a.at(i);
}
}
To copy to clipboard, switch view to plain text mode
Now, I get not only the content, but the tags as well. This is the output:
root element has 2 childs
root element is root
- [<a>11111</a>
]
- [<a>
<b>test 123</b>
</a>
]
root element has 2 childs
root element is root
- [<a>11111</a>
]
- [<a>
<b>test 123</b>
</a>
]
To copy to clipboard, switch view to plain text mode
I assume I can trim the leading and ending , but this is at the same level of ugliness I am trying to avoid.
Any other idea?
EDIT:
I should be testing more before testing. anda_skoa, I re-read your post and changed:
const char* rawXML = "<root><a>11111</a><a><b>test 123</b></a></root>";
for (int i=0; i< alist.length(); i ++) {
a.firstChild().save(ts,0);
qDebug("- [%s]", qPrintable(aContent));
}
const char* rawXML = "<root><a>11111</a><a><b>test 123</b></a></root>";
for (int i=0; i< alist.length(); i ++) {
QDomNode a = alist.at(i);
QString aContent;
QTextStream ts(&aContent);
a.firstChild().save(ts,0);
qDebug("- [%s]", qPrintable(aContent));
}
To copy to clipboard, switch view to plain text mode
Which is better:
- [11111]
- [<b>test 123</b>
]
Still, I get an extra newline after then
</b>
</b>
To copy to clipboard, switch view to plain text mode
which is bad for me... but its much better then before. It seems that QDomDocument is adding extra newlines when parsing. Am I correct? How can I disable this "feature"?
Bookmarks